Welcome › Forums › General PowerShell Q&A › Bulk set ManagedBy; Can’t find object in AD
- This topic has 4 replies, 3 voices, and was last updated 4 months, 1 week ago by
Participant.
-
AuthorPosts
-
-
September 4, 2020 at 7:47 pm #254690
I tried some code and consulted Help for Set-ADGroup but still getting error .
$Groups = Get-Content ‘c:\temp\scripts\list.txt’
$newMgr = ‘CN=ofNewManager’foreach ($group in $groups){
Set-ADGroup “$($group.ManagedBy)” -ManagedBy $newMgr -WhatIf
}error:
Set-ADGroup : Cannot find an object with identity: ” under: ‘DC=my,DC=domain’.
-
September 4, 2020 at 9:31 pm #254705
$group.ManagedBy is not something you can reference with your code. When using Get-Content alone, you are just reading in strings. They will not have any custom properties. If it is a file with common delimiters between columns or a file with a single column, you can use Import-Csv. By default, Import-Csv expects a header to be present at the top of each column. If it is not present, you can add it with the -Header parameter.
$groups = Import-Csv c:\temp\scripts\list.txt # If you have a ManagedBy header
$groups = Import-Csv c:\temp\scripts\list.txt -Header ManagedBy # If you do not have headers
-
September 7, 2020 at 4:24 pm #255119
AdminOfThings45 I created that csv with this format:
Group,ManagedBy
Note: all the groups have different names (format of Name); the ManagedBy is the same person in the format of a ‘DN’.
and modified the code as well to this:
$groups = Import-Csv c:\temp\scripts\list.txt
foreach ($group in $groups){
Set-ADGroup $group.group -ManagedBy $group.ManagedBy
}…and it worked, thank you.
If I wanted to first backup the values contained I’m about to replace, I try this code:
$Localpath = ‘C:\temp\scripts\groupBackup.csv’
$groups = Import-Csv c:\temp\scripts\list.txtforeach ($group in $groups){
Get-ADGroup $group.group -Properties ManagedBy | Select-Object Name,ManagedBy | Export-Csv -Path $LocalPath -NoTypeInformation
}…but what I end up with is only one group in my export. If I take out the Export-csv, I see displayed in the console all of my groups and ManagedBy’s.
Finally, How did you format your examples? I couldn’t figure out the toolbar for doing it completely, so I used Block quotes.
-
This reply was modified 4 months, 1 week ago by
Jeff Taylor.
-
This reply was modified 4 months, 1 week ago by
Jeff Taylor.
-
This reply was modified 4 months, 1 week ago by
-
September 8, 2020 at 1:51 am #255194
Hi Jeff,
For your Export-CSV you’ll need to add -Append as at the moment it’s looping through and each loop is overwriting the previous one. If you look at the list of groups in $groups you’re probably ending up with the last group as the final entry in the csv
-
September 8, 2020 at 12:28 pm #255293
Alex,
true enough and thanks.
-
-
AuthorPosts
- The topic ‘Bulk set ManagedBy; Can’t find object in AD’ is closed to new replies.