I am writing a user creation script and just working through my variables at this point. So i have the script importing a csv file with user info. Based on that user info it creates a series of variables three of which i am using the switch command to choose the string that is associated with another string. that all seems to work fine however when the message to type the $dept variable. the read-host is showing the $upn of the first user to go through not the user that it is actually setting the variable for. like i said the variables seem to be setting properly just the output in the shell shows the wrong upn for what i am updating.. code and output below:
function TFX-CreateNewUsers{
[CmdletBinding()]
param(
[string]$logfile = 'c:\PowerShell_Logs\UserCreationErrors.txt',
[Switch]$LogErrors,
[Parameter(Mandatory = $true,
valuefrompipeline = $true,
HelpMessage = "Please provide valid path and file name to the CSV file with the user information.")]
[Alias('csv')]
[string]$FilePath
)
$date = Get-Date -Format g
$addn = (Get-ADDomain).DistinguishedName
$dnsroot = (Get-ADDomain).DNSRoot
$userlist = Import-Csv $FilePath
$userlist | ForEach-Object{
If (($_.GivenName -eq '') -Or ($_.LastName -eq ''))
{Write-Host 'ERROR: Please provide valid GivenName, LastName and Initials. Processing stopped' -ForegroundColor Red
"$date ERROR: Please provide valid GivenName, LastName and Initials. Processing stopped" | Out-File $logfile -Append
Break
}
Else {
$upn="$first.$last@company.com"
$site=$_.site
switch ($site) {
Arl {$homed = "\\share\global\home\medical\na\arl"}
Ash {$homed ="\\share2\user"}
}
switch ($site) {
Arl {$HDrive = "H:"}
Ash {$HDrive ="U:"}
}
switch ($Site) {
Arl {$ou="OU=Users,OU=ARL,OU=NA,OU=company,DC=company,dc=global,dc=pvt"}
Ash {$dept = Read-Host "Enter Asheboro Department for $upn. Accounting, Engineering, Human Resources, Information Technology, Manufacturing, or Quality Assurance"
$ou="OU=$dept,OU=Users,OU=ARL,OU=NA,OU=company,DC=company,dc=global,dc=pvt"}
}
$First=$_.GivenName
$last=$_.LastName
$sam=$first.substring(0,1) + $last
$Office=$_.OfficeName
$description=$_.Description
$street=$_.StreetAddress
$city=$_.City
$zip=$_.postalcode
$state=$_.state
$Company=$_.Company
$Dept=$_.Department
$Title=$_.title
$mail=$upn
$phone=$_.phone
$manager=$_.manager
$Location=$_.Country
$pass=$_.Password
$expires=$_.passwordneverexpires
$Enabled=$_.Enabled
$setpass = ConvertTo-SecureString -AsPlainText $pass -force
$display="$last, $first"
Write-Verbose "$sam,$HomeD,$HDrive,$OU,$phone,$site"
}
}
}
Output when showing my variables and asking for the dept
VERBOSE: SRogers3,\\share\global\home\medical\na\arl,H:,OU=Users,OU=ARL,OU=NA,OU=company,DC=company,dc=global,dc=pvt,ARLEnter Asheboro Department for Steve.Rogers3@company.com (This should be the user below). Accounting, Engineering, Human Resources, Information Technology, Manufacturing, or Quality Assurance: asdf
VERBOSE: EScott,\\share2\user,U:,OU=asdf,OU=Users,OU=ARL,OU=NA,OU=company,DC=company,dc=global,dc=pvt,,ASH