Weird script behavior

This topic contains 4 replies, has 2 voices, and was last updated by  Dan Gheorghe 1 year, 11 months ago.

  • Author
    Posts
  • #32012

    Dan Gheorghe
    Participant

    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

  • #32019

    Steven Ayers
    Participant

    Hi Dan, This is difficult to look into without some kind of error,

    can you try catching the error? If your computer you are running this on supports Start-Transcript, please put that at the beginning...

    Start-Transcript -Path $env:temp\logtranscript.log -IncludeInvocationHeader
    

    If not, this is what you could put in place:

    $log = "$env:temp\log.log"
    $logsep = "==============================="
    Enable-RemoteMailbox $line.samaccountname -RemoteRoutingAddress $routing -PrimarySmtpAddress $usersmtp
    Get-RemoteMailbox | select EmailAddressPolicyEnabled >> $log
    $logsep >> $log
    Set-RemoteMailbox $line.samaccountname -EmailAddressPolicyEnabled $true
    $Error[0] >> $log
    $logsep >> $log
    Get-RemoteMailbox | select EmailAddressPolicyEnabled >> $log
    $logsep >> $log
    Send-Mailmessage -to $to -from $from -Subject $sub -SmtpServer $smtp -body
    

    replace the above with your scriptblock below:

    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
    

    please let us see the copy of the log fies!

  • #32033

    Dan Gheorghe
    Participant

    Hello Steven and thank you for your help
    Here is the transcript:
    **********************
    Windows PowerShell Transcript Start
    Start time: 20151117121515

    Machine : server (Microsoft Windows NT 6.1.7601 Service Pack 1)
    **********************

    Get-RemoteMailbox : The operation couldn't be performed because object 'finalte
    st365' couldn't be found on 'server.local'.
    At C:\Scripts\autoenable.ps1:26 char:31
    + $chkmbx = Get-RemoteMailbox <<<< -resultsize Unlimited $line.sam accountname | select IsValid -ErrorAction SilentlyContinue + CategoryInfo : NotSpecified: (:) [Get-RemoteMailbox], Managemen tObjectNotFoundException + FullyQualifiedErrorId : 8DB6813E,Microsoft.Exchange.Management.Recipient Tasks.GetRemoteMailbox Get-RemoteMailbox : The operation couldn't be performed because object 'finalte st365' couldn't be found on 'server.local'. At C:\Scripts\autoenable.ps1:27 char:31 + $policy = Get-RemoteMailbox <<<< -resultsize Unlimited $line.sam accountname | select EmailAddressPolicyEnabled -ErrorAction SilentlyContinue + CategoryInfo : NotSpecified: (:) [Get-RemoteMailbox], Managemen tObjectNotFoundException + FullyQualifiedErrorId : 8DB6813E,Microsoft.Exchange.Management.Recipient Tasks.GetRemoteMailbox Name RecipientTypeDetail RemoteRecipientTyp s e —- ——————- —————— finaltest365 RemoteUserMailbox ProvisionMailbox Set-RemoteMailbox : The operation couldn't be performed because object 'finalte st365' couldn't be found on 'server.local'. At C:\Scripts\autoenable.ps1:45 char:18 + Set-RemoteMailbox <<<< $line.samaccountname -EmailAddressPolicyEnabled $true + CategoryInfo : NotSpecified: (0:Int32) [Set-RemoteMailbox], Man agementObjectNotFoundException + FullyQualifiedErrorId : 7FF5B333,Microsoft.Exchange.Management.Recipient Tasks.SetRemoteMailbox Get-RemoteMailbox : The operation couldn't be performed because object 'finalte st365one' couldn't be found on 'TODCSVDC01.globalivewireless.local'. At C:\Scripts\autoenable.ps1:26 char:31 + $chkmbx = Get-RemoteMailbox <<<< -resultsize Unlimited $line.sam accountname | select IsValid -ErrorAction SilentlyContinue + CategoryInfo : NotSpecified: (:) [Get-RemoteMailbox], Managemen tObjectNotFoundException + FullyQualifiedErrorId : 25E4A8AA,Microsoft.Exchange.Management.Recipient Tasks.GetRemoteMailbox Get-RemoteMailbox : The operation couldn't be performed because object 'finalte st365one' couldn't be found on 'TODCSVDC01.globalivewireless.local'. At C:\Scripts\autoenable.ps1:27 char:31 + $policy = Get-RemoteMailbox <<<< -resultsize Unlimited $line.sam accountname | select EmailAddressPolicyEnabled -ErrorAction SilentlyContinue + CategoryInfo : NotSpecified: (:) [Get-RemoteMailbox], Managemen tObjectNotFoundException + FullyQualifiedErrorId : 25E4A8AA,Microsoft.Exchange.Management.Recipient Tasks.GetRemoteMailbox finaltest365one RemoteUserMailbox ProvisionMailbox Set-RemoteMailbox : The operation couldn't be performed because object 'finalte st365one' couldn't be found on 'server.local'. At C:\Scripts\autoenable.ps1:45 char:18 + Set-RemoteMailbox <<<< $line.samaccountname -EmailAddressPolicyEnabled $true + CategoryInfo : NotSpecified: (0:Int32) [Set-RemoteMailbox], Man agementObjectNotFoundException + FullyQualifiedErrorId : D7A79AA7,Microsoft.Exchange.Management.Recipient Tasks.SetRemoteMailbox ********************** Windows PowerShell Transcript End End time: 20151117121527 ********************** Please disregard the first error Get-RemoteMailbox. I am doing a validity check first in case the mailbox is already enabled. What i found strange is the fact that I am getting an error at: Set-RemoteMailbox : The operation couldn't be performed because object 'finalte st365one' couldn't be found on 'server.local'." The object obviously exist since it was mail enabled 0.1 sec before 🙂 Thank you again

  • #32034

    Steven Ayers
    Participant

    I'd advise that you give it a little delay. I don't know... a few minutes?

    this sounds like a replication issue.

    Please let me know 🙂

    use:
    sleep -seconds 300
    that's 5, shorten if you need to.

  • #32035

    Dan Gheorghe
    Participant

    I already tried -s 30 but i guess it not enough.
    I will try you suggestion.
    Thank you again Steven.

You must be logged in to reply to this topic.