I have edited my question based on responses in the comments below.
I am writing a script that updates Active Directory from a csv file. The file contains many columns and some of them are blank which means I have to skip those columns. Problem is I have no idea ahead of time which columns are blank and which are not and it changes from file to file. The script will run every half an hour pulling data from a HR system to update the AD record with.
The column names in the CSV file do not match the property names in AD so to accommodate for that I created a custom object where I basically define properties whose names do match AD property names and assign the values of each column to that object's properties. This should allow me to use splatting to update AD.
The AD object to update is contained in a variable called $CurrentADUser.
The custom object is getting it's values from the CSV record which is read from the CSV file and stored in $CSVUserData - not all the records are here , only one record is in this object at a time so i do not need to loop through every record in the file at this point as this script is written in a different function which is called as the CSV file loops through records.
I cannot seem to get it to work. The script generate no errors but the object's properties do not seem to be recognized by the For each loop that follows it and it just skips the for each loop basically after trying once.
The code i am using is as follows hopefully someone can spot my error for me
The Description variable is set by combining the values of $CSVUserData.JobTitle and $CSVUserData.Department and since AD requires a AD object to insert into the Manager property and not a string this is also manipulated outside of this declaration. the rest come straight from the CSV record
$ADObjectToUpdate = [PSCustomobject]@{
GivenName = $CSVUserData.FirstName
Surname = $CSVUserData.LastName
Description = $DescriptionString
Title =$CSVUserData.JobTitle
Department = $CSVUserData.Department
Manager = $ADManager **<- this is an AD object**
Company = $CSVUserData.Company
EmployeeID = $CSVUserData.EmployeeNumber
StreetAddress = $CSVUserData.WorkAddress
City = $CSVUserData.City
State = $CSVUserData.State
PostalCode = $CSVUserData.Zip
Country = $CSVUserData.Country
Office = $CSVUserData.Location
LogonWorkstations = $CSVUserData.WorkstationLocation
EmailAddress = $CSVUserData.EmailAddress
}
$setSplat = @{}
ForEach($_ in $ADObjectToUpdate.PSObject.Properties)
{
If(-not([string]::IsnullOrWhiteSpace($_.Value)))
{
$setSplat[$_.Name] = $_.Value
}
}
Set-ADUser -Identity $CurrentADUser @setSplat
I want to say thanks to everyone who left me a comment, some were extremely useful for setting me on this approach
If you want to take your script a step further, you could set up a hash table that can be used to correlate the CSV columns with the Set-ADUser
paramaters, on the left-hand side (the hash table Keys) you can put the CSV column names and in the right-hand side (the hash table Values) you would put the parameter name. For instance:
$map = @{
FirstName = 'GivenName'
LastName = 'Surname'
Department = 'Department'
JobTitle = 'Title'
Company = 'Company'
EmployeeNumber = 'EmployeeID'
WorkAddress = 'StreetAddress'
City = 'City'
State = 'State'
Zip = 'PostalCode'
Country = 'Country'
Location = 'Office'
WorkstationLocation = 'LogonWorkstations'
EmailAddress = 'EmailAddress'
}
This hash table would be at the top of your code, outside the loop, then you can use it's keys to iterate over the object's properties and check if their value is not null or empty:
foreach($ADObjectToUpdate in Import-Csv path\to\csv) {
$setSplat = @{}
# for each key in our correlation table
foreach($key in $map.Keys) {
# here we get the value of each property
# of the csv
$value = $ADObjectToUpdate.$key
# then we can check if its not null or empty
if(-not [string]::IsNullOrWhiteSpace($value)) {
# and if its not, we can assign it to the splatting hash
$setSplat[$map[$key]] = $value
}
}
# unclear how these 2 are obtained but here,
# you would need an additional condition for them
if($DescriptionString) {
$setSplat['Description'] = $DescriptionString
}
if($ADManager) {
$setSplat['Manager'] = $ADManager
}
# now you need to be certain that there is something to update
# for this user, for this, check if the `$setSplat` has something in it
if($setSplat.Keys.Count) {
# unclear how `$CurrentADUser` is obtained here
Set-ADUser -Identity $CurrentADUser @setSplat
}
}