Hi Guys,
you might have seen this before but I’ve already spent too much trying to figure this one by myself so here it is:
$csv = Import-Csv -Path “C:\New_O365_Users*.csv”
if(!($csv)){
Send-Mailmessage -to $to -from $from -Subject “No Users created” -SmtpServer $smtp -body “Nothing to enable. No users created.”
}
else{
foreach($line in $csv){
$chkmbx = Get-RemoteMailbox -resultsize Unlimited $line.samaccountname | select IsValid
if($chkmbx.Isvalid -ne $true){
$msdomain = "@domainname.mail.onmicrosoft.com"
$winddomain = "@domainname.com"
$routing = $line.samaccountname + $msdomain -join ""
$usersmtp = $line.samaccountname + $winddomain -join ""
$sub = "New user [$($line.samaccountname)]"
$body = "$($line.samaccountname) was mail enabled"
#Write-host $line.samaccountname "not enabled.Enabling now..." -foregroundcolor yellow
Enable-RemoteMailbox $line.samaccountname -RemoteRoutingAddress $routing -PrimarySmtpAddress $usersmtp
Set-RemoteMailbox $line.samaccountname -EmailAddressPolicyEnabled $true
Send-Mailmessage -to $to -from $from -Subject $sub -SmtpServer $smtp -body
}
else{
Write-Warning "User $($line.samaccountname) already enabled"
Send-Mailmessage -to $to -from $from -Subject $sub -SmtpServer $smtp -body "$($line.samaccountname) is already mail enabled"
}
}
}
When I run this script manually everything works as expected but when i run this as a Scheduled Task only
Enable-RemoteMailbox $line.samaccountname -RemoteRoutingAddress $routing -PrimarySmtpAddress $usersmtp is applied.
For some reason
Set-RemoteMailbox $line.samaccountname -EmailAddressPolicyEnabled $true
is not happening in Scheduled Task mode.
Thank you for your help