0

您好我有以下代码,具有三个功能

function Search_User_File { foreach  ($User in $FILE) { Search_User_AD} }

function Search_User_AD {
    $Ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=COMPANY,DC=COMPANYcolo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
    $Search = $Ad | ? {$_.sn -eq $User.sn}
    If ($Search) {
        Foreach ($AD_User in $Search)  {
              Write-Host "=============CSV User==============="
              Write-Host ""
              Write-Host "=============AD User==============="
              Write-Host ""
              $RightWrong = Read-Host 'Is this the RIGHT EMPLOYEE we are working (Y/N)'
              If ($RightWrong -eq "Y") {
        write-host ""
        $Correct = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In AD CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N)'
        If (($RightWrong -eq "Y") -and ($Correct -eq "N")){
            Edit_AD_User
        }
        Elseif (($RightWrong -eq "Y") -and ($Correct -eq "Y"))  {
                            clear-host

在这里,我如何在 SEARCH_USER_FILE 上 MOVE.NEXT ?如果我不这样做,那么 Search_User_File 将重新开始。

                            Search_User_File
        }
   }
}

function Edit_User_AD {

}

$File = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Search_User_File

谢谢你的帮助,

4

1 回答 1

0

这是非常低效的,因为您每次都检索 AD 中的每个人,然后搜索姓氏。如果可能,请获取能够唯一标识该 CSV 文件中的每个用户的内容,以便您可以在Get-ADUsercmdlet 中进行过滤,而不是事后进行过滤。

如果您有 2 个同姓 ( sn) 的用户会怎样?

通常,您想要做的是在 AD 中搜索用户,找到他们,将该对象分配给一个变量,然后执行您的编辑并保存更改。你在这里有点蹦蹦跳跳。

$File = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber

foreach ($user in $file) {
  Search_User_AD $user.sn
}

function Search_User_AD {
param(
 [string]$sn
)
    $Ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=COMPANY,DC=COMPANYcolo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
    $Search = $Ad | ? {$sn -eq $User.sn}
    If ($Search) {
        Foreach ($AD_User in $Search)  {
              Write-Host "=============CSV User==============="
              Write-Host ""
              Write-Host "=============AD User==============="
              Write-Host ""
              $RightWrong = Read-Host 'Is this the RIGHT EMPLOYEE we are working (Y/N)'
              If ($RightWrong -eq "Y") {
        write-host ""
        $Correct = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In AD CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N)'
        If (($RightWrong -eq "Y") -and ($Correct -eq "N")){
            Edit_AD_User
        }
        Elseif (($RightWrong -eq "Y") -and ($Correct -eq "Y"))  {
                            clear-host
        }
     }
   }
于 2013-01-11T21:09:47.230 回答