Help with PowerShell AD Script
Import-Module ActiveDirectory

$users = get-aduser -f {physicalDeliveryOfficeName -eq '07-129'} 
$i = 0
foreach($users in $users){
$i++
Write-Host $users

The code above is what I am currently working on.

What I am trying to do is find any entry in AD where "physicalDeliveryOfficeName" is equal to a number and NOT a letter. Then I am trying to take the "description" attribute and replace the "physicalDeliveryOfficeName" with the "description" and possibly store the previous "physicalDeliveryOfficeName" in another variable.

Any help would be greatly appreciated!

July 1st, 2015 2:38pm

The attribute "physicalDeliveryOfficeName " is a string it is not a number.  --eq requires an exact match so the attribute must have those exact charcters to work.

Start by reviewing how this works.

get-aduser -f {physicalDeliveryOfficeName -eq '07-129'}-Properties description,physicalDeliveryOfficeName  |
     select name,
description,physicalDeliveryOfficeName 


Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 2:51pm

Well see that is my problem, while it is a string some of the strings that have been entered are numbers similar to what is shown above and I am trying to locate those to replace.

Like when I run the code above I get a user but his physicalDeliveryOfficeName = "07-129" instead of a location like "Florida", that is why I need to return the results of those that are not letters but numbers (0-9).

What I was initially trying to do was grab everyone and then store it into a variable ($i) and then sift through the array of users to sort out the ones that were incorrectly entered.
July 1st, 2015 3:01pm

Try this, I suck at regular expressions so not sure if it will work

$users = Get-ADUSer -Filter * -Prop PhysicalDeliveryOfficeName | Where {
  $_.PhysicalDeliveryOfficeName -match "[0-9]{2}-[0-9]{3}"
}
That should match if the string is 00-000, 2 numbers a dash followed by 3 numbers
Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 3:06pm

Scripting Guys have some great info here:
http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/21/use-the-powershell-ad-provider-to-modify-user-attributes.aspx
July 1st, 2015 3:11pm

I saw that, that is what helped me to initially create the above code. But that requires a specific value, I still need to be able to tell whether or not the location has an integer or string and replace them.

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 3:18pm

That code runs without an error but it does not display any results when I try to sift through it using:

foreach($users in $users){
$i++
Write-Host $users
}

July 1st, 2015 3:27pm

Try it again, I had to update the script
Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 3:27pm

You need to learn how to use PowerShell first then you can start adding the AD bits.

get-aduser -filter * -Properties description,physicalDeliveryOfficeName  |
    Where{ $_,physicalDeliveryOfficeName -match '\d+-\d+'}

     select name,
description,physicalDeliveryOfficeName 

Once you see how this works it can be easily turned into a fix tool.

July 1st, 2015 3:32pm

That code runs without an error but it does not display any results when I try to sift through it using:

foreach($users in $users){
$i++
Write-Host $users
}

help foreach - read all of the help to learn how to use the syntax.

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 3:34pm

Thanks clayman2 that lets me locate all of the users who do have a number in the office listing! Now I just need to figure out how to take their description and place it in their physicalDeliveryOfficeName.
July 1st, 2015 3:35pm

I built the following for you because I had some time. It will go through every account in AD and check the description field for a number. If one is found it places the value of the description field into the physicalDeliveryOfficeName attribute field.
$users = get-aduser -Properties * -Filter *
foreach($user in $users)
{
$CheckNum = $user.Description
if($CheckNum -match "[0-9]")
{
Set-ADUser $user.SamAccountName.ToString() -Office $CheckNum.ToString()
}
else
{
}
}

Be careful.... this will alter all users in AD.
Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 3:37pm

You need to learn how to use PowerShell first then you can start adding the AD bits.

get-aduser -filter * -Properties description,physicalDeliveryOfficeName  |
    Where{ $_,physicalDeliveryOfficeName -match '\d+-\d+'}

     select name,
description,physicalDeliveryOfficeName 

Once you see how this works it can be easily turned into a fix tool.

July 1st, 2015 3:44pm

Thanks clayman2 that lets me locate all of the users who do have a number in the office listing! Now I just need to figure out how to take their description and place it in their physicalDeliveryOfficeName.

This is up to you, as jrv mentions you should learn the basic powershell stuff then add the AD stuff once you have some of the basics down. Here is a sample

foreach($user in $user)
{
  $desc = $user.Description
  $params = @{physicalOfficeDeliveryName=$desc}
  Set-ADUser $user -Replace $params
}

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 3:47pm

I built the following for you because I had some time. It will go through every account in AD and check the description field for a number. If one is found it places the value of the description field into the physicalDeliveryOfficeName attribute field.
$users = get-aduser -Properties * -Filter *
foreach($user in $users)
{
$CheckNum = $user.Description
if($CheckNum -match "[0-9]")
{
Set-ADUser $user.SamAccountName.ToString() -Office $CheckNum.ToString()
}
else
{
}
}

Be careful.... this will alter all us
July 1st, 2015 3:49pm

Thanks for all the help! I will dive further into this and see where I come out but I feel as though this is a good start. 

Also I cannot user the replace function for some reason but I can research that :)

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 3:58pm

If network traffic and CPU time aren't an issue, as in my environment, bring back the data allows you to play with the data. You can always filter out what you need when you have finalized the script. And this is similar to what he was asking for.
July 1st, 2015 4:01pm

Because this will be faster and match more cases:

get-aduser -filter * -Properties description,physicalDeliveryOfficeName  |
     Where{ $_,physicalDeliveryOfficeName -match '\d+-\d+'}
     ForEach-Object{
          $_,physicalDeliveryOfficeName=$_.description
          Set-AdUser -Instance $_ -PassThru
     } |
Select *

Hey!  If you have PowerShell why not use it.

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 4:04pm

Well see that is my problem, while it is a string some of the strings that have been entered are numbers similar to what is shown above and I am trying to locate those to replace.

Like when I run the code above I get a user but his physicalDeliveryOfficeName = "07-129" instead of a location like "Florida", that is why I need to return the results of those that are not letters but numbers (0-9).

What I was initially trying to do was grab everyone and then store it into a variable ($i) and then sift through the array of users to sort out the ones that were incorrectly entered.
  • Edited by Chuck_1987 Wednesday, July 01, 2015 7:01 PM
July 1st, 2015 6:56pm

Well see that is my problem, while it is a string some of the strings that have been entered are numbers similar to what is shown above and I am trying to locate those to replace.

Like when I run the code above I get a user but his physicalDeliveryOfficeName = "07-129" instead of a location like "Florida", that is why I need to return the results of those that are not letters but numbers (0-9).

What I was initially trying to do was grab everyone and then store it into a variable ($i) and then sift through the array of users to sort out the ones that were incorrectly entered.
  • Edited by Chuck_1987 Wednesday, July 01, 2015 7:01 PM
Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 6:56pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics