write-host "Importing GetPassword.ps1" . "\\server\share\GetPassword.ps1" $csv = Import-Csv \\server\share\Parents2AD.csv $csv $Expiry = ((Get-Date -day 01).AddMonths(1).AddDays(1)) $Expiry = Get-Date $Expiry -Format D write-host "Account Expiries will be set to: " $Expiry Import-Module ActiveDirectory function trimit($inStr) { return ($inStr -replace(' ','') -replace('-','') -replace('-','')) } write-host "Looping through CSV" $csv | ForEach { $DisplayName = trimit($_.givenName + "." + $_.sn) #$DisplayName if ($_.sAMAccountName -eq " ") { $AcName = trimit($_.givenName + '.' + $_.sn) } else { $AcName = $_.sAMAccountName } if ($AcName.Length -gt 20) { $AcName = $AcName.Substring(0,20) } #$AcName $PassWord = Create-RandomPassword #$PassWord $Email = $_.mail #$Email $testcount = (Get-ADUser -filter {name -eq $AcName}).count if ($testcount -lt 1) { write-host "Creating user:" $AcName " - " $DisplayName New-ADUser -Name $AcName -DisplayName $DisplayName -GivenName $_.givenName -Surname $_.sn -SamAccountName $AcName -UserPrincipalName $AcName -EmailAddress $_.mail -Path "OU=ParentUsers,DC=xyz,DC=abc" -PasswordNeverExpires $True -AccountPassword (ConvertTo-SecureString $PassWord -AsPlainText -force) -Enabled $True -AccountExpirationDate $Expiry write-host "Emailing:" $Email #Send-MailMessage -SmtpServer "192.168.5.15" -From "ParentAccounts@southcraven.org" -To $Email -Subject "South Craven Parent Account" -Body $("Dear Parent/Carer,`r`n`r`nYour South Craven parent account has been created.`r`n`r`nYour username in most cases is the same as it was before.`r`n`r`nYour password is: $PassWord`r`n`r`nWe recommend you use our Self Service portal to a) change your password to something you know, and b) set some security questions so you can automatically reset your password if you forget it in the future.`r`n`r`nYou can find our Self Service portal at: http://selfservice.southcraven.org.`r`n`r`nPlease login with your username and the password above and follow the prompts to enroll.`r`n`r`nThe Parent Portal will continue using your existing credentials until Sunday 21st June. It will switch over to your new credentials on Monday 22nd June.`r`n`r`nIf you have any problems or queries, please contact the IT Office on ITSupport@southcraven.org or 01535 632861.`r`n`r`nIT Support.") Send-MailMessage -SmtpServer "192.168.5.15" -From "ParentAccounts@southcraven.org" -To $Email -Subject "South Craven Parent Account" -Body $("Dear Parent/Carer,`r`n`r`nYour South Craven parent account has been created.`r`n`r`nYour password is: $PassWord`r`n`r`nYour username in most cases is Forename.Surname (don't forget the dot) - if this doesn't work, please reply to this email.`r`n`r`n(If you commonly use a shortened version of your forename, the system will probably have used your full forename)`r`n`r`nWe recommend you use our Self Service portal to a) change your password to something you know, and b) set some security questions so you can automatically reset your password if you forget it in the future.`r`n`r`nYou can find our Self Service portal at: http://selfservice.southcraven.org`r`n`r`nPlease login with your username and the password above and follow the prompts to enroll.`r`n`r`nOnce you have set your own password, you can use it to login to the Parent Portal and the Homework planner as well as the Self Service portal.`r`n`r`nPlease note that ParentPay (where you pay for school dinners or trips) is currently a separate system with separate login details.`r`n`r`nThe Parent Portal is available via our school website and clicking 'Parent Portal' on the menu.`r`n`r`nThe Homework planner is available at http://homework.southcraven.org or you can download the South Craven app on the Apple or Android app stores.`r`n`r`nIf you have any problems or queries, please contact the IT Office on ITSupport@southcraven.org or 01535 632861.`r`n`r`nIT Support.`r`n`r`n---") write-host "Emailing: P.Howarth@southcraven.org" Send-MailMessage -SmtpServer "192.168.5.15" -From "ParentAccounts@southcraven.org" -To $("P.Howarth@southcraven.org") -Subject "South Craven Parent Account: $AcName" -Body $("Dear Parent/Carer,`r`n`r`nYour South Craven parent account has been created.`r`n`r`nYour password is: $PassWord`r`n`r`nYour username in most cases is Forename.Surname - if this doesn't work, please reply to this email.`r`n`r`n(If you commonly use a shortened version of your forename, the system will probably have used your full forename)`r`n`r`nWe recommend you use our Self Service portal to a) change your password to something you know, and b) set some security questions so you can automatically reset your password if you forget it in the future.`r`n`r`nYou can find our Self Service portal at: http://selfservice.southcraven.org`r`n`r`nPlease login with your username and the password above and follow the prompts to enroll.`r`n`r`nOnce you have set your own password, you can use it to login to the Parent Portal and the Homework planner as well as the Self Service portal.`r`n`r`nPlease note that ParentPay (where you pay for school dinners or trips) is currently a separate system with separate login details.`r`n`r`nThe Parent Portal is available via our school website and clicking 'Parent Portal' on the menu.`r`n`r`nThe Homework planner is available at http://homework.southcraven.org or you can download the South Craven app on the Apple or Android app stores.`r`n`r`nIf you have any problems or queries, please contact the IT Office on ITSupport@southcraven.org or 01535 632861.`r`n`r`nIT Support.`r`n`r`n---") } else { $UserMail = (Get-ADUser $AcName -Properties EmailAddress | select EmailAddress).EmailAddress #$UserMail if ($UserMail -eq $Email) { #write-host "Record exists and matches ($Email)" #Therefore just update expiry Set-ADUser $AcName -AccountExpirationDate $Expiry } else { write-host "Username $AcName exists but doesn't match ($UserMail <> $Email)" #Therefore send a warning email to ITSupport Send-MailMessage -SmtpServer "192.168.5.15" -From "ParentAccounts@southcraven.org" -To $("P.Howarth@southcraven.org") -Subject "Duplicate Parent Account" -Body $("Duplicate parent account found! `r`n`r`nUsername: $AcName`r`nCurrent Email: $UserMail`r`nNew Email: $Email`r`n`r`nPlease check whether this is a changed email address or a duplicate account.`r`n`r`nIT Support.") } } }