IF Statement Confusion

Hello,
Having a problem with this IF statement. Using debugging, I can see the variables as they are tested.

I am expecting that if one of the pipeline vars in the below statement checks to be blank, then it should go to the ‘else’ and not execute the next line etc.

If (($_.givenname -ne "") -or ($_.sn -ne "") -or ($_.samaccountname -ne "") -or ($_.password -ne ""))
{
do some stuff
}
else
{
error message here to say the var was blank
}

Even though when I set and check that $_.givenname is blank (during debugging), the IF statement still passes it as true and DOESNT execute ‘else’. I was expecting it to evaluate a blank givenname as false.

Please could someone assist my blunder

Many Thanks

You may want to check what the type is for e.g. $.givenname.
If it’s “blank” most likely it’s actually a null “value”.
So if you give the following condition a try: ($
.givenname -ne $null)
Does it work as expected?

If you are uncertain you can always use both statements in the if-conditions.

how about the below,

this is IF $Var = anything but null, do stuff
Else, do other stuff

If (($_.givenname) -or ($_.sn) -or ($_.samaccountname) -or ($_.password))
{
do some stuff
}
else
{
error message here to say the var was blank
}

I tried this, didnt seem to work. I need it to process the IF only when the contacts of the vars are populated with something.

The type of checks you do depend on the validity of your data, here are some common checks that I use to make sure a value exists:

#Nothing, space, tab
$tests = @{
    "Null"    = $null
    "Nothing" = "" 
    "Space"   = " "
    "Tab"     ="`t"
}

foreach ($test in $tests.GetEnumerator()) {
    if ($test.Value) {"[Powershell Null Check] Do Something for {0}" -f $test.Name}
    if (![string]::IsNullOrEmpty($test.Value)) {"[IsNullOrEmpty] Do Something for {0}" -f $test.Name}
    if (![string]::IsNullOrWhiteSpace($test.Value)){"[IsNullOrWhiteSpace] Do Something for {0}" -f $test.Name}
}

Output:

[Powershell Null Check] Do Something for Tab
[IsNullOrEmpty] Do Something for Tab
[Powershell Null Check] Do Something for Space
[IsNullOrEmpty] Do Something for Space

You see that “” or $Null aren’t valid for any check, however, a space or tab will be passed without the IsNullOrWhiteSpace. Second, you are using -OR when you should be using -AND. All of those attributes have to be valid before you want to do something (e.g Create a user). With -OR, one of the values has to have a valid value. Lastly, you can do a lot of this work by simply creating a function and passing parameters. Parameters can be defined to mandatory and not null and you’ll get an error regarding the exact parameter that didn’t match versus checking 4 parameters and giving a generic error message. Also, keep in mind that the Powershell command (New-ADUser) will also fail and tell you a parameter cannot be null because it is formed like a function and defines what values must exist to execute the command.

Hello,

I checked the type:

IsPublic IsSerial Name BaseType


True True String System.Object

Says its type is string (when its empty and same when popualted with a string value)

I tried your suggestion, but still execiting the first loop instead of executing the ELSE when one of the pipeline vars is blank.

If (($_.givenName -ne $null) -or ($_.sn -ne $null) -or ($_.samaccountname -ne $null) -or ($_.password -ne $null) )
{
stuff it shouldnt be doing it ANY of the above vars are $null or blank.
}
Else
{
Should be doing this stuff but it isnt!!
}

Then I realised this doenst work either!

If (($_.givenName -ne $null)

So I tried this, which does work:

If (($_.givenName -ne "") )

But when I try with multiple conditions, it doesnt work! Just using -or then select next condition as above.

really useful, thanks so much for taking the time to write all that. Will test this shortly.

Well as you add more conditions using -or each of those conditions have to be validated.
Meaning you need to check the type and/or content to make sure the condition you evaluate with, actually match the content.

As Rob states, you have multiple possible scenarios where blank is not really blank.
“” and $null is in my experience the most common ones.

As an alternative to test all the different “blank” scenario and you don’t need to make difference if it’s a tab or space etc.
Then you could use trim to remove spaces and tabs.
Only caveat is to test the $null value first as you’ll get an error if you try to trim an value that is null.

e.g.

if(($_.givenName -eq $null) -or ($_.givenName.Trim() -eq "")) #then expand with all your other parameters as needed.
{
   some stuff
}
else
{
   some other stuff
}

Guys,

Ive been really silly and gotten confused between -and -or. Soon as I changed this to -and then it now works.

Just to mention, I have written this as a powershell module with CSV file as the mandatory param. The function will import from CSV, that is why I wanted a section to check for blanks.

Also, later on, so that New-Aduser doesnt get confused when the hash table passes blank (blanks that werent tested in the start) ive done this:

#HASH table designed to only present values that are not blank
            $hash = [ordered]@{}        
            if ($_.Description) {$hash.Add("Description", ($_).description) }
            if ($_.employeeID) {$hash.Add("employeeID", ($_).employeeID) }
            if ($_.givenname) {$hash.Add("givenname", ($_).givenname) }
            if ($_.sn) {$hash.Add("Surname", ($_).sn) }
            if ($_.name) {$hash.Add("name", ($_).name) }
            if ($_.mail) {$hash.Add("emailaddress", ($_).mail) }
            if ($_.title) {$hash.Add("title", ($_).title) }
            if ($_.department) {$hash.Add("department", ($_).department) }
            if ($_.streetaddress) {$hash.Add("streetaddress", ($_).streetaddress) }
            if ($_.l) {$hash.Add("City", ($_).l) }
            if ($_.postalcode) {$hash.Add("postalcode", ($_).postalcode) }
            if ($_.telephonenumber) {$hash.Add("OfficePhone", ($_).telephonenumber) }
            if ($_.mobile) {$hash.Add("mobilephone", ($_).mobile) }
            if ($_.samaccountname) {$hash.Add("samaccountname", ($_).samaccountname) }
            if ($_.physicalDeliveryOfficeName)  {$hash.Add("Office", ($_).physicalDeliveryOfficeName) }
            $hash.Add("enabled", $($enabled))
            $hash.Add("PasswordNeverExpires", $($expires))
            $hash.Add("ChangePasswordAtLogon", $($CPANL))
            $hash.add("UserPrincipalName" , ($_).mail)

So it builds the hash table and only posits values if they exist.

Interested to hear if you woudl suggest a better way of course.

Thank you, it is working now, but I will bear this in mind, appreciated. Here is the final line:

 If (($_.givenName -ne "") -and ($_.sn -ne "") -and ($_.samaccountname -ne "") -and ($_.password -ne ""))

I was using the wrong operator, -or when I should have been using -and (amoungst other mistakes). If I have problems I may bring in the trim() example.

Thanks Guys, really helpful

I find this handy when I don’t know and don’t care if its null or an empty string.

if (-not [string]::IsNullOrEmpty("test")) {"Not Null or Empty"}
if ([string]::IsNullOrEmpty("")) {"Null or Empty"}
if ([string]::IsNullOrEmpty($Null)) {"Null or Empty"}

Addeed the trim() thanks thats a good addition