ForEach Move Next特殊情况

时间:2013-01-11 18:38:25

标签: powershell-v2.0 powershell-v3.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上移动.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

感谢您的帮助,

1 个答案:

答案 0 :(得分:0)

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

如果您有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
        }
     }
   }