Author Posts

March 9, 2018 at 2:56 pm

Every department we have has a departmentnumber, so IF HR has departmentnumber 60, then a user in the HR department should have 60 set in the departmentnumber attribute. In our organisation, departmentnames and numbers will not change.
If somebody suggest a Hashtable instead, I would like a small demo to build from, because I am not that skilled with Hashtables but I can understand they sometimes can make your life easier in powershell.

Param (
[String]$Username ,
[String]$HRDepartment = "HR" ,
[int]$HR = "60"
)
Get-ADUser -Identity $Username
IF ( $_.$Username.Department -eq $HRdepartment ) {Set-ADUser $Username -departmentnumber $HR }

Then I get this error:

Get-ADUser : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.
At C:\PS\GetTest2.ps1:11 char:22
+ Get-ADUser -Identity $Username
+                      ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

March 9, 2018 at 3:00 pm

Edited so I get email notification.

March 9, 2018 at 3:03 pm

It's telling you that $Username is empty. When you run your script, you need to provide a value for it.

Also, -DepartmentNumber is not a parameter of Set-ADUser – you can see that in the help. The help also has examples if how to set properties for which there isn't a parameter. From the docs:

Set-ADUser -Identity GlenJohn -Replace @{title="director";mail="glenjohn@fabrikam.com"}

That @ thing is the hash table. It's just a list of property=value pairs.

March 10, 2018 at 12:39 pm

Hi Mr. Jones
I found this example:

$user = Get-ADUser GlenJohn -Properties mail,department
$user.mail = "glen@fabrikam.com"
$user.department = "Accounting"
Set-ADUser -instance $user

And then added the IF like this:

$user = Get-ADUser testuser -Properties department,departmentnumber
$HRnr="10"
$ITnr="12"
$Salesnr="90"

IF ( $user.department -eq "HR" ) 
{
$user.departmentnumber = $HRnr
Set-ADUser -instance $User
}
IF ( $user.department -eq "IT" ) 
{
$user.departmentnumber = $ITnr
Set-ADUser -instance $User
}
IF ( $user.department -eq "Sales" ) 
{
$user.departmentnumber = $Salesnr
Set-ADUser -instance $User
}

I have thought that I need to add
Param(
And add a variable for the Username since it should run automated.
)
I dont mind to hardcode a lot of IF's sentences. It is more in the area of, when I need to add an extra attribute value.
So after department,departmentnumber there will also be Division.
How do I smarten this up?