I'm adding a new powershell piece into my user creation, which is to e-mail their supervisor with an alert that they have been created. However, for some reason, it sends the credentials to the next supervisor down in the list, instead of the supervisor in that row, and the first supervisor listed is getting headers as information. The CSV is constructed like so:
1.Lastn | Firstn | Program | Password. | Supervisor
2.Duck | Daffy | ........ 998 | ChangeMe | Bob.Dole
3.Bunny| Buggs|.........878| ChangeMe | Elmer.Fudd
The script is:
$Users = Import-Csv "C:\PSScripts\Create\users.csv" -header("lastname","firstname","program","password","supervisor")
foreach ($User in $Users)
{
$OU = "OU=users,DC=Contoso,DC=local"
$UserFirstname = $User.Firstname
$UserLastname = $User.LastName
$Supervisor = $User.Supervisor
$subject = "New Account Creation Completed"
$body = "Good Morning! This e-mail is to alert you that your new staff person $SAM has been granted Contoso.org e-mail access. To log in to outlook web application, their username is $SAM and their temporary password is ChangeMe . They will be prompted to change it upon successful login."
$smtp = "10.10.1.79"
$SAM = $UserFirstname + "." + $UserLastname
Send-MailMessage -to [email protected] -Subject $subject -body $body -SmtpServer $smtp -from [email protected]
}
However, the supervisor 'Bob.Dole' is being e-mailed 'Buggs.Bunny' credentials, and supervisor Elmer.Fudd is being e-mailed 'Firstname.Lastname', which is how the $Sam is constructed. So the credentials are going to the supervisor down one row, and the last supervisor is being e-mailed 'firstname.lastname' rather than a person. Additionally, I'm recieving undeliverable e-mails saying that the email account [email protected] is not valid, so it seems to be misinterpreting something, though it executed the comand to send to the $supervisor after it does so. If I insert a blank cell, under the 'Supervisor' header, so the supervisor column is down shifted by one, it spits an error at me that a recipient must be specified BUT will then send out the credentials to the CORRECT supervisor, once the are downshifted.
Get-Content -Path $Path | ConvertFrom-CSV
?Import-Csv "C:\PSScripts\Create\users.csv
It would beGet-Content -Path "C:\psscripts\create\users.csv" | ConvertFrom-CSV
?