none
how do i loop this? RRS feed

  • Question

  • i'm trying to set the mailing address for an OU. I managed to get how for City, StreetName, PostalCode (zip), and Country like so:

    get-aduser -searchbase "OU=LAB,OU=Texas,DC=ACME,DC=COM" -Filter 'samAccountName -like bugs' | Set-ADUser -replace @{c="US";co="United States";countrycode=1}
    get-aduser -searchbase "OU=LAB,OU=Texas,DC=ACME,DC=COM" -Filter 'samAccountName -like bugs' | Set-ADUser -City Boston
    get-aduser -searchbase "OU=LAB,OU=Texas,DC=ACME,DC=COM" -Filter 'samAccountName -like bugs' | Set-ADUser -PostalCode 11383

    for each user. however, I cannot seem to wrap my head on how to do a for loop. I can read the output of OU into a variable $result but after that i'm stumped! (or maybe the solution is just staring at me already)

    Sunday, October 25, 2015 1:35 PM

Answers

  • A pipeline is a loop.  THis wil alter every user in that OU.

    get-aduser -searchbase "OU=LAB,OU=Texas,DC=ACME,DC=COM" -Filter * |
        Set-ADUser -replace @{c="US";co="United States";countrycode=1} -City Boston -PostalCode 11383


    \_(ツ)_/

    • Marked as answer by Reno Mardo Sunday, October 25, 2015 4:04 PM
    Sunday, October 25, 2015 2:37 PM

All replies

  • A pipeline is a loop.  THis wil alter every user in that OU.

    get-aduser -searchbase "OU=LAB,OU=Texas,DC=ACME,DC=COM" -Filter * |
        Set-ADUser -replace @{c="US";co="United States";countrycode=1} -City Boston -PostalCode 11383


    \_(ツ)_/

    • Marked as answer by Reno Mardo Sunday, October 25, 2015 4:04 PM
    Sunday, October 25, 2015 2:37 PM
  • thanks. i edited it to suit my requirements so for anyone looking for the same info, here it is:

    get-aduser -searchbase "OU=LAB,,DC=ACME,DC=com" -Filter * -Properties * | Set-ADUser -replace @{c="US";co="United States";countrycode=1} -Company "Acme Sporting Goods Ltd." -City "Texas" -PostalCode 11383 -StreetAddress "GTA City,`r`nP.O Box: 12345"

    note how to set the country settings. all must be filled.

    Sunday, October 25, 2015 4:04 PM
  • We do not put this all on one line for many reasons.  We also do not use -Properties * when doing this.  It will only make things slower and load up your network.

    get-aduser -searchbase 'OU=LAB,,DC=ACME,DC=com' -Filter * |
         Set-ADUser -replace @{c='US';co='United States';countrycode=1} -Company 'Acme Sporting Goods Ltd.' -City Texas -PostalCode 11383 -StreetAddress "GTA City,`r`nP.O Box: 12345"


    \_(ツ)_/

    Sunday, October 25, 2015 4:09 PM
  • This method makes it much easier to read and edit as well as debug:

    $replace = @{
    	c = 'US'
    	co = 'United States'
    	countrycode = 1
    	Company = 'Acme Sporting Goods Ltd.'
    	st = 'Texas'
    	l = 'Dallas'
    	PostalCode = '11383'
    	PostOfficeBox='12345'
    	StreetAddress = 'GTA City'
    }
    
    get-aduser -Filter * -searchbase 'OU=LAB,,DC=ACME,DC=com' |
    	Set-ADUser -replace $replace
    

    This is why the "Replace" and  "Add" take a hash.  It is so the hash can be included as a block and can actually be loaded from a file.


    \_(ツ)_/




    • Edited by jrv Sunday, October 25, 2015 4:47 PM
    Sunday, October 25, 2015 4:34 PM