Bulk set ManagedBy; Can’t find object in AD

Welcome Forums General PowerShell Q&A Bulk set ManagedBy; Can’t find object in AD

Viewing 4 reply threads
  • Author
    Posts
    • #254690
      Participant
      Topics: 125
      Replies: 270
      Points: 316
      Rank: Contributor

      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’.

       

    • #254705
      Participant
      Topics: 0
      Replies: 81
      Points: 362
      Helping Hand
      Rank: Contributor

      $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

    • #255119
      Participant
      Topics: 125
      Replies: 270
      Points: 316
      Rank: Contributor

      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.txt

      foreach ($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 2 weeks, 4 days ago by Jeff Taylor.
      • This reply was modified 2 weeks, 3 days ago by Jeff Taylor.
    • #255194
      Participant
      Topics: 6
      Replies: 120
      Points: 127
      Helping Hand
      Rank: Participant

      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

    • #255293
      Participant
      Topics: 125
      Replies: 270
      Points: 316
      Rank: Contributor

      Alex,

      true enough and thanks.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.