Benutzer mit den meisten Antworten
Powershell: Get-QADGroup die Ausgabe DN in eine Variable schreiben und später mit Add-QADGroupMember User zur Grouppe hinzufügen wie!?

Frage
-
Hallo liebe Leute,
Folgender script liegt vor, bisher läuft das ganze so ab:
$impcsvfile | ForEach-Object { #checking if group allready exists, if not the group is going to be created Write-Host "Step 3: checking if group allready exists, if not the group is going to be created" -ForegroundColor Green -BackgroundColor DarkGreen #region checkg #checking group existens $checkg = [ADSI]::Exists("LDAP://CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int") #or? #$checkg = [ADSI]::Exists("LDAP://CN=$($_.GROUPNAME),DC=kl,DC=int") #becouse the group has the be searcht over the whole container not just in the specific OU, becouse that OU was newly created for the script...!? #endregion #region if group exists #if group exists if ($checkg -eq $false) { #creating new group Write-Host "Step 3.1: creating new group" -ForegroundColor Blue -BackgroundColor DarkGreen New-QADGroup -Name $_.GROUPNAME -SamAccountName $_.GROUPNAME -DisplayName $_.GROUPNAME -Description $_.GROUPDESCRIPTION -GroupScope Global -GroupType Security -ParentContainer "OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int" -ErrorAction Continue #| tee -FilePath $tee\$teename #(get-QADGroup 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name } else { #continueing if group allready exists Write-Error -Message "group exists continueing" -ErrorAction SilentlyContinue -ErrorVariable $Error3 #(get-QADGroup 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name } #show info group #(get-QADGroup 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name | Write-Host #endregion #endregion #---------------------------------------------------------------- #region check if user disbaled + add user #checking if user is deactivated, if not the user gets a member of the group, if, he is removed Write-Host "Step 4: checking if user is deactivated, if not the user gets a member of the group, if, he is getting removed" -ForegroundColor Black -BackgroundColor Yellow #region disable check variables #variables (with directorysearcher) $ds = New-Object System.DirectoryServices.DirectorySearcher $ds.Filter = "(&(objectCategory=Person)(sAMAccountname=$($_.USER))(!userAccountControl:1.2.840.113556.1.4.803:=2))" $du = $ds.FindOne() $de = $du.GetDirectoryEntry() #endregion #region if user is disabled #checking if user is deactivated if ($du = $ds.FindOne()) { #adding user to the group Write-Host "Step 4.1 adding user to group" -ForegroundColor Blue -BackgroundColor Yellow Add-Qadgroupmember -Identity "CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int" -Member $_.USER -ErrorAction Continue #| tee -FilePath $tee\$teename #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name } else { #removing and error if user is deactivated Write-Host "Step 4.?(2) removing user from group" -ForegroundColor Red -BackgroundColor Yellow Remove-QADGroupMember -Identity "CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int" -Member $_.USER -ErrorAction SilentlyContinue Write-Error -Message "user is disabled, next step" -ErrorAction SilentlyContinue -ErrorVariable $error4 #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name } #show info group user/s #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name | Write-Host } #endregion #endregion
in Zukunft soll das ganze eher so Aussehen:
try { get-qadgroup "$_.GROUPNAME" } catch { #creating new group Write-Host "Step 3.1: creating new group" -ForegroundColor Blue -BackgroundColor DarkGreen New-QADGroup -Name $_.GROUPNAME -SamAccountName $_.GROUPNAME -DisplayName $_.GROUPNAME -Description $_.GROUPDESCRIPTION -GroupScope Global -GroupType Security -ParentContainer "OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int" -ErrorAction Continue }
weil ich mit [ADSI] nicht in der gesamten Directory überprüfen kann ob die Gruppe existiert soll das ganze mit Get-QADGroup geprüft werden und der ausgabewert "DN" von:
Name Type DN ---- ---- -- testt group CN=testt,OU=Test-OU-Temp,DC=kl,DC=int
für das hinzufügen von neuen GroupMember benutzt werden also z.B:
Add-Qadgroupmember -Identity "$AUSGABEWERT VON GET_QADGROUP (SPALTE DN)" -Member $_.USER -ErrorAction Continue #
also meine Frage: Wie bekomme ich die Ausagen "DN" in eine Variable die ich später verwenden kann??
Domenicc TechNet Foren Thanks
Antworten
-
Hier sind gleich mehrere Fehler
1. Get-QADGroup verursacht KEINEN Error wenn die Gruppe nicht existiert!
Deshalb ist dein Try{} Catch{} Block sinnlos!
2. Try{} Catch{} Funktioniert nur wenn man die -ErrorAction auf Stop stellt!
3. Du versuchst deine Variable zu befüllen BEVOR du testest ob es die Gruppe gibt, das ist Unsinn!
Deine Variable ist dann leer wenn es die Gruppe noch nicht gibt !
4. Warum führst du 2 Mal $du = $ds.FindOne() aus ?
Die IF($ds.FindOne()) Abfrage reicht aus!
Tipps:
A. Man sollte möglichst Konsequent eine Technik benutzen! Du mischst hier die QAD Cmdlets mit ADSI das ist nicht optimal!
B. Du verwendest Import-CSV in der Pipeline mit ForEach-Object falsch ($impcsvfile entfernt)!
C. Dir ForEach-Object Zeile gehört nicht in die Region mit der Group!
D,. Anstatt mit so einer riesigen ForEach Schleife zu arbeiten sollte man das in mehrere Funktionen verpacken.
E. Schau mal was ich bei # endregion gemacht habe
F. benutze bitte selbsterklärenden Variablen Namen $du ist nicht selbstsprechend !
G. Variablen sollte man nur benutzen wenn man einen Wert mehrmals braucht!
Ich habe die Variablen $de und $impcsvfile entfernt, da Sie nie ein zweites Mal benutzt werden!
Hier der bereinigte Code (ungetestet!):#region ForEach-Object Loop #doing Step 3 and 4 for every line in the CSV Import-CSV -path $csv | ForEach-Object { #---------------------------------------------------------------- #region group #checking if group allready exists, if not the group is going to be created Write-Host "Step 3: checking if group allready exists, if not the group is going to be created" -ForegroundColor Green -BackgroundColor DarkGreen # Gruppe anlegen wenn sie nicht existiert IF (-not (Get-QADGroup $_.GROUPNAME)) { # Gruppe existiert NICHT #creating new group Write-Host "Step 3.1: creating new group" -ForegroundColor Blue -BackgroundColor DarkGreen New-QADGroup -Name $_.GROUPNAME -SamAccountName $_.GROUPNAME -DisplayName $_.GROUPNAME -Description $_.GROUPDESCRIPTION -GroupScope Global -GroupType Security -ParentContainer "OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int" -ErrorAction Continue } # Die Gruppe existiert nun #Group Path (for QAD-GroupMember) $OUGROUPPATH = Get-QADGroup $_.GROUPNAME | Select-Object -ExpandProperty DN #endregion group #---------------------------------------------------------------- #region check if user disbaled + add user #checking if user is deactivated, if not the user gets a member of the group, if, he is removed Write-Host "Step 4: checking if user is deactivated, if not the user gets a member of the group, if, he is getting removed" -ForegroundColor Black -BackgroundColor Yellow #region disable check variables #variables (with directorysearcher) $ds = New-Object System.DirectoryServices.DirectorySearcher $ds.Filter = "(&(objectCategory=Person)(sAMAccountname=$($_.USER))(!userAccountControl:1.2.840.113556.1.4.803:=2))" #endregion disable check variables #region if user is disabled #checking if user is deactivated if ($ds.FindOne()) { #adding user to the group Write-Host "Step 4.1 adding user to group" -ForegroundColor Blue -BackgroundColor Yellow Add-Qadgroupmember -Identity $OUGROUPPATH -Member $_.USER -ErrorAction Continue #| tee -FilePath $tee\$teename #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name } else { #removing and error if user is deactivated Write-Host "Step 4.?(2) removing user from group" -ForegroundColor Red -BackgroundColor Yellow Remove-QADGroupMember -Identity $OUGROUPPATH -Member $_.USER -ErrorAction SilentlyContinue Write-Error -Message "user is disabled, next step" -ErrorAction SilentlyContinue -ErrorVariable $error4 #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name } #show info group user/s #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name | Write-Host #endregion if user is disabled #endregion check if user disbaled + add user } # end ForEach-Object #endregion ForEach-Object Loop
Please click “Mark as Answer” if my post answers your question and click “Vote As Helpful” if my Post helps you.
Bitte markiere hilfreiche Beiträge von mir als “Als Hilfreich bewerten” und Beiträge die deine Frage ganz oder teilweise beantwortet haben als “Als Antwort markieren”.
My PowerShell Blog http://www.admin-source.info
[string](0..21|%{[char][int]([int]("{0:d}" -f 0x28)+('755964655967-86965747271757624-8796158066061').substring(($_*2),2))})-replace' '
German ? Come to German PowerShell Forum!
- Bearbeitet Peter Kriegel Freitag, 31. Mai 2013 12:32
- Als Antwort markiert Domeniccc Montag, 3. Juni 2013 06:06
Alle Antworten
-
Du kannst mit ADSI das gesamte AD durchsuchen ! Dafür nimmt man den [ADSIsearcher] !
Der [ADSISearcher] ist das gleiche wie System.Directory.DirectorySearcher !
Siehe: http://www.powershellpraxis.de/index.php/active-directory/queries
Nun zu du deiner Frage:
Um mit Get-QADGroup die Ausgabe DN in eine Variable zu schreiben gibt es mehrere Möglichkeiten
$GroupDN = Get-QADGroup $_.GROUPNAME | Select-Object -ExpandProperty DN # oder $GroupDN = (Get-QADGroup $_.GROUPNAME).DN
Please click “Mark as Answer” if my post answers your question and click “Vote As Helpful” if my Post helps you.
Bitte markiere hilfreiche Beiträge von mir als “Als Hilfreich bewerten” und Beiträge die deine Frage ganz oder teilweise beantwortet haben als “Als Antwort markieren”.
My PowerShell Blog http://www.admin-source.info
[string](0..21|%{[char][int]([int]("{0:d}" -f 0x28)+('755964655967-86965747271757624-8796158066061').substring(($_*2),2))})-replace' '
German ? Come to German PowerShell Forum!
- Bearbeitet Peter Kriegel Dienstag, 11. Juni 2013 12:51 Typo
-
Gib doch einfach mal den Befehl ohne Script in einer PowerShell ein!
(Quest AD Snapin muss geladen sein!)
Get-QadGroup <Name der Gruppe>
und dann nochmal
Get-QADGroup <Name der Gruppe> | Select-Object -ExpandProperty DN
Please click “Mark as Answer” if my post answers your question and click “Vote As Helpful” if my Post helps you.
Bitte markiere hilfreiche Beiträge von mir als “Als Hilfreich bewerten” und Beiträge die deine Frage ganz oder teilweise beantwortet haben als “Als Antwort markieren”.
My PowerShell Blog http://www.admin-source.info
[string](0..21|%{[char][int]([int]("{0:d}" -f 0x28)+('755964655967-86965747271757624-8796158066061').substring(($_*2),2))})-replace' '
German ? Come to German PowerShell Forum! -
das funktioniert auch super
nur als variable eben nicht diese bleibt bei dem wert NULL
hier nochmal der gesamte script:
#region set import-csv variable #importing csv Write-Host "Step 2: set import-csv variable (filelocation)" -ForegroundColor Black -BackgroundColor Green #path to the .csv file + import $impcsvfile = import-csv -path $csv #use $_. to acces the csv data ## #endregion #---------------------------------------------------------------- #region group #doing Step 3 and 4 for every line in the CSV $impcsvfile | ForEach-Object { #checking if group allready exists, if not the group is going to be created Write-Host "Step 3: checking if group allready exists, if not the group is going to be created" -ForegroundColor Green -BackgroundColor DarkGreen #Group Path (for QAD-GroupMember) $OUGROUPPATH = Get-QADGroup $_.GROUPNAME | Select-Object -ExpandProperty DN #region try,catch try { #check if group exists (if no respons -> catch = new group) get-qadgroup $_.GROUPNAME } catch { #creating new group Write-Host "Step 3.1: creating new group" -ForegroundColor Blue -BackgroundColor DarkGreen New-QADGroup -Name $_.GROUPNAME -SamAccountName $_.GROUPNAME -DisplayName $_.GROUPNAME -Description $_.GROUPDESCRIPTION -GroupScope Global -GroupType Security -ParentContainer "OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int" -ErrorAction Continue } #endregion #endregion #---------------------------------------------------------------- #region check if user disbaled + add user #checking if user is deactivated, if not the user gets a member of the group, if, he is removed Write-Host "Step 4: checking if user is deactivated, if not the user gets a member of the group, if, he is getting removed" -ForegroundColor Black -BackgroundColor Yellow #region disable check variables #variables (with directorysearcher) $ds = New-Object System.DirectoryServices.DirectorySearcher $ds.Filter = "(&(objectCategory=Person)(sAMAccountname=$($_.USER))(!userAccountControl:1.2.840.113556.1.4.803:=2))" $du = $ds.FindOne() $de = $du.GetDirectoryEntry() #endregion #region if user is disabled #checking if user is deactivated if ($du = $ds.FindOne()) { #adding user to the group Write-Host "Step 4.1 adding user to group" -ForegroundColor Blue -BackgroundColor Yellow Add-Qadgroupmember -Identity $OUGROUPPATH -Member $_.USER -ErrorAction Continue #| tee -FilePath $tee\$teename #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name } else { #removing and error if user is deactivated Write-Host "Step 4.?(2) removing user from group" -ForegroundColor Red -BackgroundColor Yellow Remove-QADGroupMember -Identity $OUGROUPPATH -Member $_.USER -ErrorAction SilentlyContinue Write-Error -Message "user is disabled, next step" -ErrorAction SilentlyContinue -ErrorVariable $error4 #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name } #show info group user/s #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name | Write-Host } #endregion #endregion
Domenicc TechNet Foren Thanks
- Bearbeitet Domeniccc Freitag, 31. Mai 2013 09:37
-
Hier sind gleich mehrere Fehler
1. Get-QADGroup verursacht KEINEN Error wenn die Gruppe nicht existiert!
Deshalb ist dein Try{} Catch{} Block sinnlos!
2. Try{} Catch{} Funktioniert nur wenn man die -ErrorAction auf Stop stellt!
3. Du versuchst deine Variable zu befüllen BEVOR du testest ob es die Gruppe gibt, das ist Unsinn!
Deine Variable ist dann leer wenn es die Gruppe noch nicht gibt !
4. Warum führst du 2 Mal $du = $ds.FindOne() aus ?
Die IF($ds.FindOne()) Abfrage reicht aus!
Tipps:
A. Man sollte möglichst Konsequent eine Technik benutzen! Du mischst hier die QAD Cmdlets mit ADSI das ist nicht optimal!
B. Du verwendest Import-CSV in der Pipeline mit ForEach-Object falsch ($impcsvfile entfernt)!
C. Dir ForEach-Object Zeile gehört nicht in die Region mit der Group!
D,. Anstatt mit so einer riesigen ForEach Schleife zu arbeiten sollte man das in mehrere Funktionen verpacken.
E. Schau mal was ich bei # endregion gemacht habe
F. benutze bitte selbsterklärenden Variablen Namen $du ist nicht selbstsprechend !
G. Variablen sollte man nur benutzen wenn man einen Wert mehrmals braucht!
Ich habe die Variablen $de und $impcsvfile entfernt, da Sie nie ein zweites Mal benutzt werden!
Hier der bereinigte Code (ungetestet!):#region ForEach-Object Loop #doing Step 3 and 4 for every line in the CSV Import-CSV -path $csv | ForEach-Object { #---------------------------------------------------------------- #region group #checking if group allready exists, if not the group is going to be created Write-Host "Step 3: checking if group allready exists, if not the group is going to be created" -ForegroundColor Green -BackgroundColor DarkGreen # Gruppe anlegen wenn sie nicht existiert IF (-not (Get-QADGroup $_.GROUPNAME)) { # Gruppe existiert NICHT #creating new group Write-Host "Step 3.1: creating new group" -ForegroundColor Blue -BackgroundColor DarkGreen New-QADGroup -Name $_.GROUPNAME -SamAccountName $_.GROUPNAME -DisplayName $_.GROUPNAME -Description $_.GROUPDESCRIPTION -GroupScope Global -GroupType Security -ParentContainer "OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int" -ErrorAction Continue } # Die Gruppe existiert nun #Group Path (for QAD-GroupMember) $OUGROUPPATH = Get-QADGroup $_.GROUPNAME | Select-Object -ExpandProperty DN #endregion group #---------------------------------------------------------------- #region check if user disbaled + add user #checking if user is deactivated, if not the user gets a member of the group, if, he is removed Write-Host "Step 4: checking if user is deactivated, if not the user gets a member of the group, if, he is getting removed" -ForegroundColor Black -BackgroundColor Yellow #region disable check variables #variables (with directorysearcher) $ds = New-Object System.DirectoryServices.DirectorySearcher $ds.Filter = "(&(objectCategory=Person)(sAMAccountname=$($_.USER))(!userAccountControl:1.2.840.113556.1.4.803:=2))" #endregion disable check variables #region if user is disabled #checking if user is deactivated if ($ds.FindOne()) { #adding user to the group Write-Host "Step 4.1 adding user to group" -ForegroundColor Blue -BackgroundColor Yellow Add-Qadgroupmember -Identity $OUGROUPPATH -Member $_.USER -ErrorAction Continue #| tee -FilePath $tee\$teename #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name } else { #removing and error if user is deactivated Write-Host "Step 4.?(2) removing user from group" -ForegroundColor Red -BackgroundColor Yellow Remove-QADGroupMember -Identity $OUGROUPPATH -Member $_.USER -ErrorAction SilentlyContinue Write-Error -Message "user is disabled, next step" -ErrorAction SilentlyContinue -ErrorVariable $error4 #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name } #show info group user/s #(Get-QADGroupMember 'CN=$($_.GROUPNAME),OU=$($_.OU),OU=Gruppen,OU=Users,OU=KL,OU=D,OU=KL-Group,DC=kl,DC=int').DirectoryEntry.name | Write-Host #endregion if user is disabled #endregion check if user disbaled + add user } # end ForEach-Object #endregion ForEach-Object Loop
Please click “Mark as Answer” if my post answers your question and click “Vote As Helpful” if my Post helps you.
Bitte markiere hilfreiche Beiträge von mir als “Als Hilfreich bewerten” und Beiträge die deine Frage ganz oder teilweise beantwortet haben als “Als Antwort markieren”.
My PowerShell Blog http://www.admin-source.info
[string](0..21|%{[char][int]([int]("{0:d}" -f 0x28)+('755964655967-86965747271757624-8796158066061').substring(($_*2),2))})-replace' '
German ? Come to German PowerShell Forum!
- Bearbeitet Peter Kriegel Freitag, 31. Mai 2013 12:32
- Als Antwort markiert Domeniccc Montag, 3. Juni 2013 06:06