Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Splatting Query - Null Values

$
0
0

I'm looking for recommendations on the best way to deal with null values with splatting.

Take the example of updating title, department and telephonenumber on an aduser.

The following looks tidier, but will cause errors with null values (I don't want to see errors in the logs).

$splat=@{
  Title=$_.Title
  Department=$_.Department
  TelephoneNumber=$_.TelephoneNumber
}

Set-ADUser @Splat

Or one property at a time with IF statments

IF ($_.Title)
        {
        Set-ADUser $_.UserPrincipalName -Add @{Title=$_.Title}
        }

IF ($_.department)
        {
        Set-ADUser $_.UserPrincipalName -Add @{Department=$_.department}
        }

IF ($_.TelephoneNumber)
        {
        Set-ADUser $_.UserPrincipalName -Add @{TelephoneNumber=$_.TelephoneNumber}
        }

Any guidance on dealing with null values would be appreciated.

How would you code this.

I'm pretty new to Powershell, and I'm trying to get a handle on writing better code.  I can mostly achieve what I need now, but the code doesn't always look pretty.


Viewing all articles
Browse latest Browse all 21975

Trending Articles