简体   繁体   中英

Set-ADUser address attribute using PowerShell

I have a simple script that I use to create new AD users. The basic script works great and gets me to where I need to be when I create a new user. There is one part that is missing that I would like to have automated so that I wont have to update the info manually. I need to add the address and phone number for a user based on their location. From the code below, I added the if elseif statement. What I have below gives the error - The term 'Austin' is not recognized as the name of a cmdlet, function, script file, or operable program. Obviously the variable $location doesn't work with the script, however, I am not as familar with PowerShell to see where to correct this or if there should be better way to write this out. Should I add Get-ADUser in front of it to pull the user info once its been added? Or add the if, elseif at the end of the script?

#Prompt for user information
$first = Read-Host "First name"
$last = Read-Host "Last name"
$title = Read-Host "Title"
$location = Read-Host "Location (location1,location2)"
$department = Read-Host "Business Practice"
$password = read-host -assecurestring "Password"

if($location = location1) {
    Set-ADUser -street "StreetName" -city "City" -state "State" -postalcode "Zip" -officephone "Phone"
}
elseif($location = location2) {
    Set-ADUser -street "StreetName" -city "City" -state "State" -postalcode "Zip" -officephone "Phone"
}



#Create new user

$Attributes = @{
                    
   Enabled = $true
   ChangePasswordAtLogon = $false
   UserPrincipalName = $first.substring(0,1)+$last+"@domain.com"
   Name = $first + " " + $last
   GivenName = $first
   Surname = $last
   DisplayName = $first + " " + $last
   Office = $location
   Department = $department
   Title = $title
   samAccountName = $first.substring(0,1)+$last
   emailaddress = $first.substring(0,1)+$last+"@domain.com"
   AccountPassword = $password
   Path = "OU Path"
   

}

New-ADUser @Attributes
  1. Set-ADUser needs to know the user for which these properties need to be set through its Identity parameter which is missing
  2. You can simply add these properties to the $Attributes hashtable you use for the New-ADUser cmdlet.
    You just need to define some logic as to where these properties come from.

Something like using a switch:

# with '$location'  just ask for the city:
$location = Read-Host "City"

# I'm making this up of course
switch ($location) {
    'Austin' { 
        $city        = 'Austin'
        $state       = 'Texas'
        $postalcode  = '73301'
        $officephone = '555-123456'
        break
    }
    'Salt Lake City' {
        $city        = 'Salt Lake City'
        $state       = 'Utah'
        $postalcode  = '84044'
        $officephone = '555-654321'
        break
    }
    default { $city = $null }
}

if ($city) {
    $Attributes = @{
       Enabled                             = $true
       ChangePasswordAtLogon               = $false
       UserPrincipalName                   = $first.substring(0,1)+$last+"@domain.com"
       Name                                = $first + " " + $last
       GivenName                           = $first
       Surname                             = $last
       DisplayName                         = $first + " " + $last
       Office                              = $location
       Department                          = $department
       Title                               = $title
       samAccountName                      = $first.substring(0,1)+$last
       emailaddress                        = $first.substring(0,1)+$last+"@domain.com"
       AccountPassword                     = $password
       Path                                = "OU Path"
       # these are from the switch() above
       City                                = $city
       State                               = $state
       PostalCode                          = $postalcode
       OfficePhone                         = $officephone
    }

    New-ADUser @Attributes
}

Or you can create a CSV file like

City,State,PostalCode,OfficePhone
Austin,Texas,73301,555-123456
Salt Lake City,Utah,84044,555-654321

and read that using

$locations = Import-Csv -Path '<PathToTheLocations.csv>'

# with '$location'  just ask for the city:
$location = Read-Host "City"

# then get the address info from there based on the given city
$address = $locations | Where-Object { $_.City -eq $location }

if ($address) {
    $Attributes = @{
       Enabled                  = $true
       ChangePasswordAtLogon    = $false
       UserPrincipalName        = $first.substring(0,1)+$last+"@domain.com"
       Name                     = $first + " " + $last
       GivenName                = $first
       Surname                  = $last
       DisplayName              = $first + " " + $last
       Office                   = $location
       Department               = $department
       Title                    = $title
       samAccountName           = $first.substring(0,1)+$last
       emailaddress             = $first.substring(0,1)+$last+"@domain.com"
       AccountPassword          = $password
       Path                     = "OU Path"
       # these are from the CSV
       City                     = $address.City
       State                    = $address.State
       PostalCode               = $address.PostalCode
       OfficePhone              = $address.OfficePhone
    }

    New-ADUser @Attributes
}

Of course, NEVER trust user input from Read-Host and build in some validation checks before creating a new user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM