I have a function where i define all the variables from an import-CSV and pipe them to a function that creates new users.
Function newaduser {
[CmdletBinding()]
Param (
$Userspassword = “Hellow0rld45684654@[!”,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$Username,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$FirstName,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$surname,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$dispname,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$desc,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$title,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$depar,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$telnumber,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$SamName
)
process
{
$CreateSecure =(ConvertTo-SecureString $Userspassword -AsPlainText -Force)
New-aduser -AccountPassword $CreateSecure -changepasswordatlogon $false -enabled $true -Name $Username -GivenName $firstname -surname $surname -DisplayName $dispname -EmailAddress "$SamName@ourcompany.com" -SamAccountName $SamName -userprincipalname $SamName -Description $desc -Title $title
-Department $depar -OfficePhone $telnumber -Company iPipelineUK -City Cheltenham -State Gloucestershire -PostalCode GL501TA
}
}
import-csv -Path C:\UserInput.csv | newaduser
I then have some error handling to see if the users were created successfully, if they are then another set of functions are called that send emails to various people asking them to create an account for each new user account that has been created.
if($error.count -eq 0)
{
Write-Host “”
Write-Host -BackgroundColor Yellow -ForegroundColor Black " Users Succesfully Created "
Write-Host “---------------------------------------------------------------”
SendSuccess-Email
SendWebEx-Email
SendPIPEDomain-Email
SendKapta-Email
SendProjector-Email
}
Else
{
Write-Host “”
Write-Host -BackgroundColor red -ForegroundColor Black " Users Failed to be Created"
Write-Host “--------------------------------------------------------------”
SendFailure-Email
}
What i want to do is call the variables $FirstName and $surname from my newaduser function and include them in the body of the emails i will be sending to each person.
Function SendProjector-Email {
Write-Host “”
Write-Host " Projector account request email sent."
Write-Host “---------------------------------------------------------------”
$emailFrom = “jjones@ourcompany.com”
######Use commas for multiple addresses
$emailTo = “jjones@ourcompany.com”
$subject = “Projector account for new user”
$body = "Hi Helpful Person,
Please could you create a Projetor account for $FirstName $surname. Please let me know if you need any additional information.
Kind regards,
The Guy Who Setup The Accounts"
$smtpServer = “smtprelay.ourcompany.co.uk”
######creating the sendobject
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
######sending the email
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
When i try this the emails don’t show anything for the $FirstName and $surname variables, its just blank. So i know the variables from the newaduser aren’t being passed to these functions. Is there a way to do this? Also it would be ideal if i could do this FOR EACH user. I dont know if this would already happen because at present the newaduser function creates each user from the CSV without me explicitly doing some kind of For Each statement.
Thanks in advance!