Clearing The Managers field within AD

Welcome Forums General PowerShell Q&A Clearing The Managers field within AD

Viewing 0 reply threads
  • Author
    Posts
    • #6195
      Member
      Topics: 1562
      Replies: 0
      Points: 0
      Rank: Member

      by mattdevs at 2012-10-19 00:52:44

      Hi All,

      I have the company's Managers fields to populate which i'll be doing via Import-Csv. Here's the code:

      $users = import-csv manager.csv
      foreach($row in $users)
      {
      $dn = $row.dn
      $user=[ADSI]"LDAP://$dn"
      $man = $row.manager
      $user.put("manager", $man)
      $user.SetInfo()
      }

      So, i have a CSV with two headers, User & Manager and this part works fine. The problem i run into is when a user's manager's field is to be removed. I've tried leaving the manager field in the CSV and it errors so i was hoping for some error handling. If $manager = $null then remove current entry. Is this possible?

      by Klaas at 2012-10-19 01:06:08

      Can you give an example of such a row in the .csv?
      If the 'manager' attribute is left out, it might be solved by putting empty quotes instead:
      'user1' 'boss1'
      'user2' "
      or do I read this wrong?

      by mattdevs at 2012-10-19 03:03:15

      Ok:

      User Manager
      CN=John Doe,OU=Dept,OU=Users,DC=contoso,DC=local CN=Jane Smith,OU=Dept,OU=Users,DC=contoso,DC=local

      If ran manager with "" i receive:

      Exception calling "SetInfo" with "0" argument(s): "An invalid dn syntax has been specified.

      by Klaas at 2012-10-19 03:38:40

      What is the delimiter in the .csv? There are multiple spaces and commas, so it must be semicolon or tab,...?

      by Klaas at 2012-10-19 05:16:11

      You could write your variables to the screen to check what's in there.
      You assign $dn=$row.dn , but i think $row should have only .user and .manager properties.
      $users = import-csv manager.csv
      $users
      foreach($row in $users)
      {
      $row
      $dn = $row.dn
      $dn
      $user=[ADSI]"LDAP://$dn"
      $user
      $man = $row.manager
      $man
      $user.put("manager", $man)
      $user.SetInfo()
      }

      if you try this with only a few rows in the csv, you can easily see if you get what you're expecting and if not in which step the error is introduced.
      If you use the debug function in the ISE you can follow the variable contents there.

      by mikefrobbins at 2012-10-23 17:16:11

      Ok:

      User Manager
      CN=John Doe,OU=Dept,OU=Users,DC=contoso,DC=local CN=Jane Smith,OU=Dept,OU=Users,DC=contoso,DC=local

      If ran manager with "" i receive:

      Exception calling "SetInfo" with "0" argument(s): "An invalid dn syntax has been specified.

      Set the manager field to $null to clear it.

      Set-ADUser jdoe -manager $null

Viewing 0 reply threads
  • The topic ‘Clearing The Managers field within AD’ is closed to new replies.