2017-04-14 63 views
-1

此腳本旨在幫助從一個域遷移到另一個域。我正在尋找所有AD安全組,然後我拉出每個組的成員,並創建一個文本文件,其中包含該組的名稱和該組的成員。PowerShell中的最後一個foreach循環失敗,但之前的循環正常工作

然後我想要創建一組新的文本文件,以包含每個組的用戶MailNickName字段中的信息,這稱爲它們的NID(新ID)。我使用這個MailNickName來存儲它們的NID作爲新域名,所以我可以將NID推送到新域中的適當組中。

在下面的腳本中,除了最後一個foreach循環外,其他所有的東西都可以工作,不知道爲什麼;它看起來是正確的,我已經在互聯網上搜索答案,但沒有理由爲什麼這不應該工作。

我沒有收到任何錯誤,但$b變量最終與$iGroup變量的值相同。此外,NID的新文本文件從不創建。

$iScript = "GetADGroups" 
$StrPath = "C:\Temp\$iScript" 
$StrFile = "$StrPath\$iScript.txt" 
if(!(Test-Path $StrPath)){New-Item -ItemType directory -Path $StrPath} 

$OU1 = "OU=Users,OU=OC,DC=domain,DC=com" 
$OU2 = "OU=Users,OU=AMC,DC=domain,DC=com" 
$OU3 = "OU=Users,OU=FI,DC=domain,DC=com" 

Get-ADGroup -Filter 'GroupCategory -eq "Security"' -SearchBase $OU1 | ft name -HideTableHeaders | Out-File "$strFile" 
Get-ADGroup -Filter 'GroupCategory -eq "Security"' -SearchBase $OU2 | ft name -HideTableHeaders | Out-File "$StrFile" -Append 
Get-ADGroup -Filter 'GroupCategory -eq "Security"' -SearchBase $OU3 | ft name -HideTableHeaders | Out-File "$StrFile" -Append 

(GC $StrFile) | ? {$_.trim() -ne ""} | Set-Content $StrFile 
$Groups = (GC $StrFile) 
$Groups | foreach{$_.TrimEnd()} | Set-Content $StrFile 
$Groups = (GC $StrFile) 

foreach($Member in $Groups){ 
    foreach($a in $Member){ 
     if(Test-Path "$StrPath\$a.Members.txt"){Remove-Item -Path "$StrPath\$a.Members.txt" -Force} 
     Write-Host $iMember 
     $iMember = Get-ADGroupMember "$a" | Select-Object sAMAccountName 
     $iMember | ft -HideTableHeaders | Out-File "$StrPath\$a.Members.txt" -Append 
     (GC "$StrPath\$a.Members.txt") | ? {$_.trim() -ne ""} | set-content "$StrPath\$a.Members.txt" 
     (GC "$StrPath\$a.Members.txt") | foreach{$_.TrimEnd()} | Set-Content "$StrPath\$a.Members.txt" 
     GCI "$StrPath\$a.Members.txt" | where {$_.Length -lt 1} | Remove-Item 
     } 
    } 

$GroupName = (GCI "$StrPath\*.Members.txt" -Name) 
foreach($iGroup in $GroupName){ 
    foreach($b in $iGroup){ 
     if(Test-Path "$StrPath\$b.NID.txt"){Remove-Item -Path "$StrPath\$b.NID.txt" -Force} 
     Write-Host $b 
     Get-ADUser -filter {sAMAccountName -eq "$b"} -Properties MailNickName | Select-Object MailNickName | Set-Content "$StrPath\$b.NID.txt" 
     } 
    } 
+0

我想我會寫出來的源變量(或至少$ iGroup)到文本文件,以直觀地檢查您開始的內容。在我的經驗中,像這樣的文字怪異通常是由於一些意想不到的價值。如果不是,你能提供什麼輸入到最後一組循環是? –

回答

1
  1. foreach($b in $iGroup)循環中的單個項目,他們將永遠是平等的。演示:

    $GroupName = "Group1.Members.txt","Group2.Members.txt","Group3.Members.txt" 
    foreach($iGroup in $GroupName){ 
        foreach($b in $iGroup){ 
         Write-Host $b 
        } 
    } 
    Group1.Members.txt 
    Group2.Members.txt 
    Group3.Members.txt 
    

    確定你不想讀取文件?防爆。

    foreach($Group in (GCI "$StrPath\*.Members.txt")){ 
        Write-Host $Group.Name 
        foreach($User in (Get-Content -Path $Group.FullName)){ 
         Write-Host $User 
        } 
    } 
    
  2. 在出口國和進口對象,你很少想使用Format-* -cmdlets,因爲它們都是爲了在控制檯中顯示的數據,並打破了對象。因此,你最終不得不每次清理你的文本文件。我強烈建議使用Export-CSVImport-CSV或自定義文本文件(如果是這樣,請使用Select-Object -ExpandProperty Name僅顯示組名)使用CSV文件。

  3. 您正在浪費時間閱讀,修改和刪除文件。我將它改寫爲這樣的:

    $iScript = "GetADGroups" 
    $StrPath = "C:\Temp\$iScript" 
    $StrFile = "$StrPath\$iScript.txt" 
    #-Force to also create missing parents-folders 
    if(!(Test-Path $StrPath)){New-Item -ItemType Directory -Path $StrPath -Force} 
    
    $OUs = "OU=Users,OU=OC,DC=domain,DC=com", "OU=Users,OU=AMC,DC=domain,DC=com", "OU=Users,OU=FI,DC=domain,DC=com" 
    $Groups = $OUs | Foreach-Object { Get-ADGroup -Filter 'GroupCategory -eq "Security"' -SearchBase $_ } 
    #Save groupnames to file if you need them 
    $Groups | Select-Object -ExpandProperty Name | Set-Content -Path $StrFile 
    
    foreach($group in $Groups){ 
        Write-Host $group 
        $members = @(Get-ADGroupMember -Identity $group) 
    
        #If group contains members 
        if($members.Count -gt 0) { 
         #Create list of members 
         $members | Select-Object -ExpandProperty sAMAccountName | Set-Content -Path "$StrPath\$group.Members.txt" 
    
         #Create list with new IDs (is the other list necessary?) 
         $members | Get-ADUser -Properties MailNickName | Select-Object -ExpandProperty MailNickName | Set-Content -Path "$StrPath\$group.NID.txt" 
        } 
    } 
    

    ,最好的成員和NID列表合併到用戶名和的mailNickname使用的是單排連接一個CSV文件。例如:

    #If group contains members 
    if($members.Count -gt 0) { 
        #Create csv-list of members with current username (samaccountname) and new id (mailnickname) 
        $members | Get-ADUser -Properties MailNickName | Select-Object -Property sAMAccountName, MailNickName | Export-CSV -Path "$StrPath\$group.csv" -NoTypeInformation 
    } 
    

    或者更進一步,只需一個csv文件和所有組成員資格。

    $iScript = "GetADGroups" 
    $StrPath = "C:\Temp\$iScript" 
    $StrFile = "$StrPath\$iScript.txt" 
    
    $OUs = "OU=Users,OU=OC,DC=domain,DC=com", "OU=Users,OU=AMC,DC=domain,DC=com", "OU=Users,OU=FI,DC=domain,DC=com" 
    $Groups = $OUs | Foreach-Object { Get-ADGroup -Filter 'GroupCategory -eq "Security"' -SearchBase $_ } 
    #Save groupnames to file if you need them (if you're migrating empty groups) 
    $Groups | Select-Object -ExpandProperty Name | Set-Content -Path $StrFile 
    
    $Groups | Foreach-Object { 
        $group = $_ 
        Write-Host $group 
        Get-ADGroupMember -Identity $group | 
        #Getting ADUser object to get missing property 
        Get-ADUser -Properties MailNickName | 
        Select-Object -Property @{n="Group";e={$group}}, sAMAccountName, MailNickName 
    } | Export-CSV -Path "$StrPath\GroupMembers.csv" -NoTypeInformation 
    

    您的CSV是這樣的,它可以很容易地進口的,後來隨着Import-CSV分組等添加成員到新的組時:

    Group,sAMAccountName,MailNickName 
    Group1,user1,mailnickforuser1 
    Group2,user1,mailnickforuser1 
    Group2,user2,mailnickforuser2 
    
+0

哇,你讓它變得如此簡單。我喜歡上一個CSV文件,但我不得不向Export-CSV行添加「-append」,否則我最終只有一個入口組。 – Jas

+0

糟糕。忘了將foreach-loop更改爲pipeline-foreach。現在更新csv解決方案。:-) –

+0

我想我有一些組中有一些錯誤,所以我將 -ErrorAction SilentlyContinue 添加到Get-ADGroupMember和Get-ADUser行。 – Jas