Author Posts

May 21, 2018 at 1:14 pm

Greetings,

I'm trying to create a script to bulk add new users by importing them from a csv file, and need to include the Pager field, located on the Telephone tab in the AD user's properties. I already have a script that works for everything else, but I need to figure out a way to include the pager field on my current script, becuase the pager field is used for our Key Fob IDs.

I've spent hours reading on the subject and realize there's similar threads out there, but I can't figure out how to incorporate what I've found into my existing script. I'm very new to PowerShell and would be grateful for any help you could provide.

Someone on another forum suggested that I use the OtherAttributes parameter of New-ADUser and pass a hash table to it. For example:

-OtherAttributes @{pager="123-456-6789"}

...but I don't know how to do that so I can create a pager header in the csv file and import the value into the script.

Here is the code I have put together so far.

Thanks a million for your help, in advance.

# Import the AD Module
Import-Module ActiveDirectory

# Import the CSV file
$users = Import-Csv C:\ADScripts\NewUsers\NewUsers.csv

# Complete an action for each user in the CSV file
ForEach ($user in $users) {
# Do this for each user
$accountNumber = verifyUsername($user.'First Name'[0] + $user.'Last Name')
$username = ($user.'First Name'[0] + $user.'Last Name' + $accountNumber)

New-ADUser `
-Name ($user.'First Name' + " " + $user.'Last Name' + " " + $accountNumber) `
-GivenName $user.'First Name' `
-Surname $user.'Last Name' `
-UserPrincipalName $username `
-SamAccountName $username `
-AccountPassword (ConvertTo-SecureString "P@$$w0rd123" -AsPlainText -Force) `
-Description $user.Description `
-EmailAddress $user.'Email Address' `
-Title $user.'Job Title' `
-OfficePhone $user.'Office Phone' `
-Path $user.'Organizational Unit' `
-ChangePasswordAtLogon 1 `
-Enabled ([System.Convert]::ToBoolean($user.Enabled))
}

# See if a username is already in use. If it is, then return the number that should be appended
# the end of the name. If not, then return an empty string (example: jsmith, jsmith1, jsmith2 etc...)
function verifyUsername($username) {
$i = 1

# See if username is taken (or in use)
if (userNameTaken($username) -eq $True) {
while (userNameTaken($username + $i) -eq $True) {
$i++
}
} else {
return ""
}
return $i
}

# Check to see if username already exists
function userNameTaken($username) {
$test1 = Get-ADUser -Filter { userPrincipalName -eq $username }
$test2 = Get-ADUser -Filter { samAccountName -eq $username }

if($test1 -eq $Null -and $test2 -eq $Null) {
return $False
} else {
return $True
}
}
Here is some other things I found poking around other threads:

Add @{"pager" = $_.pager}

if ($_.PropertyNames.Contains("pager")){ #use -Replace}
else {# use -Add }

Get-ADUser -Filter * -Properties sAMAccountName, pager | Select sAMAccountName, pager | Export-Csv .\UserPagers.csv -NoTypeInformation

I have no idea how to get this to work. Thank you, once again.

May 21, 2018 at 3:07 pm

I actually got this answered on another forum and posted the answer below, in case someone runs into the same issue.

Thank you for taking a look at my post.

-OtherAttributes @{pager=$User.pager}