1

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.

6
  • Why not use Get-Content -Path $Path | ConvertFrom-CSV?
    – Colyn1337
    Nov 17, 2015 at 16:09
  • Where would I put that? Nov 17, 2015 at 16:14
  • inplace of the import-CSV.... Just comment out that first line and try assigning $Users with the new code. Then return the output on the console and match it to your csv. the data should be correct, but look for white space in the supervisor property
    – Colyn1337
    Nov 17, 2015 at 16:18
  • Well my first note would be that you're declaring the headers for your CSV but there are also headers in it - so it will probably be interpreting the headers as data. Shouldn't be causing the problem you're getting but still can't be helping. Nov 17, 2015 at 16:20
  • So instead of Import-Csv "C:\PSScripts\Create\users.csv It would be Get-Content -Path "C:\psscripts\create\users.csv" | ConvertFrom-CSV? Nov 17, 2015 at 16:50

1 Answer 1

0

The issue is you assign to $SAM after you refer to it in your assignment to $body.

$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"  

    #moved before $body= .... $SAM ...
    $SAM =  $UserFirstname + "." + $UserLastname 

    $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"
    Send-MailMessage -to [email protected] -Subject $subject -body $body -SmtpServer $smtp -from [email protected]
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .