0

I wrote a script to move our service accounts first names to last names and keep it up to date as service accounts are created in Active Directory. It works and logs the changes, except the else part of the script. For some reason, it will not execute the else statement when the if isn't true. Please keep in mind that I am still a beginner to PowerShell...

$runStartTime = Get-Date -Format g
$workingDir = "c:\bin\"
$logfile = $workingDir +" LastNameChg.txt"

Add-Content $logfile "-----|   LogFile: $logfile"
Add-Content $logfile "-----|   Users last names changed in Active Directory on $runStartTime :"
Import-Module ActiveDirectory
$users = Get-ADUser -searchbase "OU=Testing,OU=Service Accounts,DC=test,DC=907,DC=local" -LDAPFilter {(&(objectCategory=user)(objectClass=user)(mail=*)(!(sn=*)))} 
foreach ($user in $users){
  $logdata = $user.sAMAccountName
   if ($user.GivenName -ne $null){
    Get-ADUser $user | Set-ADUser -surname $($user.givenName) -givenname $()
    Add-Content $logfile "-----|   $logdata" 
   }
   else{
   Add-Content $logfile "-----|    No changes made to Active Directory"
   }
   }   
    Add-Content $logfile ""
11
  • 1
    Try if ($user.GivenName -ne ""){ - maybe the GivenName is either some text or an empty string, and is never $null. Just guessing. Commented Jan 7, 2016 at 17:36
  • $user.GivenName is most likely "" (an empty string) and not $null. Remove -ne $null from the if statement and it'll work Commented Jan 7, 2016 at 17:38
  • I tried it both ways, the else statement is still not working, any other ideas? Commented Jan 7, 2016 at 17:44
  • 3
    I would actually use if(-not [string]::IsNullOrWhitespace($user.GivenName)) which catches null, whitespace, or empty Commented Jan 7, 2016 at 17:44
  • All of these work in the if statement perfectly, but the else statement is still not doing anything. Could it be a powershell version issue?PSVersion 4.0 Commented Jan 7, 2016 at 17:54

2 Answers 2

1

Look over this writeup of the four different kinds of nulls in PS.

http://www.codykonior.com/2013/10/17/checking-for-null-in-powershell/

Some of these tests give couterintuitive results. Maybe your variable is a string, in which case PS assigns an empty string to the variable. Then when the test is done against $null, it's always not equal, and the else is never chosen.

I'm not saying this is your problem, but it's a real gotcha, so it's worth checking out.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the info. I read through it and it doesn't seem to be my problem from what I can see, I even tried a few examples. I actually removed the $null from the script to see if that would fix the issue and so far no go.
It was possibly an answer, if his problem was caused by any of the gotchas in the referenced page. It turned out not to get him out of the woods, but it was not intended as a critique of the question. I provided a comment on the question for that purpose.
0

Thanks everyone who has responded, I figured out the problem with my logic. The if statement needed to go before the loop

if ($users){
foreach ($user in $users){
$logdata = $user.sAMAccountName
Get-ADUser $user | Set-ADUser -surname $($user.givenName) -givenname $()
Add-Content $logfile "-----|   $logdata" 
  } 
  }else{Add-Content $logfile "-----|   No Changes Made in Active Directory"}
  Add-Content $logfile ""

1 Comment

Ah. I (and perhaps others) misunderstood your intent, based on the code you provided. The logic error is not specific to Powershell. It could have been made in anything from Basic to Java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.