Set-ADUser which may contain blank variables
hey all,
I have a ps script that will be setting AD attributes. These will be passing over from ServiceNow, and not all the fields will be mandatory - which means that some will be passed over with no values.
I have done some research, and came up with the following:
$AccountName = "test16"
$MiddleName ="Michael"
$Title = ""
$Department = "IT"
$Location = "Bravo St"
$Manager = "jroland"
$ManagerUserName = Get-ADUser -identity $Manager
$PhoneNumber = "96978896"
$EmployeeNumber = "7878"
$EmployeeType = "Staff"
$Info = "RITM2322323"
$hash = @{} if(!($Title -eq "")){$hash.title = $Title;}
if(!($MiddleName -eq "")){$hash.MiddleName = $MiddleName;}
if(!($Department -eq "")){$hash.department = $Department;}
if(!($Location -eq "")){$hash.physicalDeliveryOfficeName = $Location;}
if(!($Manager -eq "")){$hash.manager = $ManagerUserName;}
if(!($PhoneNumber -eq "")){$hash.telephoneNumber = $PhoneNumber;}
if(!($EmployeeNumber -eq "")){$hash.employeeNumber = $EmployeeNumber;}
if(!($EmployeeType -eq "")){$hash.employeeType = $EmployeeType;}
if(!($Info -eq "")){$hash.Info = $Info;}
Set-ADUser $AccountName -Replace $hash
I get the error: PS C:\Scripts> & '.\Ad Attributes.ps1' Set-ADUser : Invalid type 'Microsoft.ActiveDirectory.Management.ADUser'. Parameter name: manager At C:\Scripts\Ad Attributes.ps1:34 char:1
- Set-ADUser $AccountName -Replace $hash
-
+ CategoryInfo : InvalidArgument: (brhalliday:ADUser) [Set-ADUser], ArgumentException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Comm ands.SetADUser
When commenting out the manager, it works well.
I am not sure what is happening 100%, however, i feel it is to do with me getting the manager via get-aduser and it not being a string?
Another thing to note is that the manager is 100% mandatory, so will always be passed over. so it does not need to be in this format.
Thanks in advance :)
Posted Mar 26, 2018, 10:44 PM UTC · edited
In this line your setting $ManagerUserName to an AD object, but AD expects a distinguished name, do you might try $ManagerUserName.distinguishedname in the hash. I get what you're doing with the splatting, but it's honestly hard to read in this case.
$ManagerUserName = Get-ADUser -identity $Manager $PhoneNumber = "96978896"
cheers mate,
yeah that should be 2 lines;
$ManagerUserName = Get-ADUser -identity $Manager
$PhoneNumber = "96978896"
Just missed it. it is hard with the reddit formatting lol. Not so hard to read in notepad++
I tested with:
if(!($ManagerUserName -eq "")){$hash.manager = $ManagerUserName.distinguishedname;}
and it worked! Thanks so much :) I thought my inkling was right, just my terminology is waaaay off lol.
Yep that makes sense, but you do understand that even this is setting that variable to an object and not a string like a distinguished name?
$ManagerUserName = Get-ADUser -identity $Manager
$ManagerUserName.gettype()
You should see an object, it's something like an AD powershell object.
$ManagerUserName.DistinguishedName.gettype()
you should see a string