Archive for February, 2011

PowerShell – Multiple (-and) (-or) within a single If statement

I had a hard time finding examples on the web on how or if you could code multiple If’s into a single If statement, so I thought I would just quick post up an example once I figured out how to do it. Perhaps I wasn’t looking in the right places but all the examples I saw were just a single (-and) or (-or) in the code.

So based on being able to code an If statement in either of the following fashions:

If ($someAttribute -eq $false -or $someAttribute -eq $Null){

}

or the following:

If (($someAttribute -eq $false) -or ($someAttribute -eq $Null)){

}

Then we should be able to do a combined effort:

If (($someAttribute -eq $false -or $someAttribute -eq $Null) -and ($someUser -eq “UserID123″)){
       Write-Host “Switching to True”
       Set-QADUser $someUser -Service “MyDomain.org” -ObjectAttributes @{$someAttribute = $True}
       If (!$?) {Write-Host “Error: $($error[0])”}
}

So if $someAttribute is either $false or $Null AND #someUser equalsUserID123″ then do something… in this case I switch the Attribute someAttribute to $true and do some error checking.

~ITNotes

Tags: , , , , ,

Friday, February 25th, 2011 Active Directory, Powershell, Scripting 2 Comments