0

I have a script file that when ran from within ISE or from the powershell window it works fine. But when ran trough the task scheduler with "runas" the same account it only outputs half the data.

After some troubleshooting i have concluded that the part of the code that is not working is:

Get-ADOrganizationalUnit -SearchBase $OU -Filter {objectclass -eq "organizationalunit"} -SearchScope onelevel -Properties description | 
    % {
        $mailbox = @( get-user -OrganizationalUnit $_.distinguishedName -resultsize unlimited |
            ? { $_.title -ne "xxx" -and $_.RecipientType -eq "usermailbox" -and $_.RecipientTypeDetails -ne "RoomMailbox" } )
        if ($mailbox.count -gt 0) {
            $name = $_.name
            $mail_customer = $body
            $mail_customer += "<h3>"  + $name + "</h3>"
            $mail_customer += $mailbox | get-MailboxStatistics |
                select displayname, lastlogontime, @{ label = "Mailbox Size(MB)"; expression = {
                    if ($_.totalitemsize.value.toMB() -gt 25600) { "!!!" + $_.TotalItemSize.Value.ToMB() }
                    else { $_.TotalItemSize.Value.ToMB() }
                } } | Sort-Object displayname | ConvertTo-Html -Fragment | Out-String
            $count += $mailbox.count
            $stattable += @{$name = $mailbox.count}
            $mail_customer = $mail_customer | ForEach-Object { $_ -replace '!!!', '<font color="#FF0000">' }
            if ( $_.description -ne $null ) {
                $mailaddresses = $_.description.split(",")
                Send-MailMessage `
                    -SmtpServer $SMTPserver `
                    -Encoding $encoding `
                    -From "$company <noreply@$companylower.se>" `
                    -To $mailaddresses `
                    -Subject "$company report - $name - $date" `
                    -BAH `
                    -Body $mail_customer
            }
        $mail += $mail_customer
        $mailaddresses = "";
        }
    }

I get the email but it does not include the information that these lines should output, any ideas?

Script is called like this: powershell -file c:\temp\scriptname.ps1

4
  • Get-MailboxStatistics requires membership of the Organization Management role-group in Exchange. Is the user account running the task member of the group? Commented Nov 16, 2015 at 8:27
  • the account is a member of Organization Management yes. Commented Nov 16, 2015 at 8:43
  • Can you please try to output $mail_customer just before the mail is sent, to check if it holds what you need ? Commented Nov 16, 2015 at 9:33
  • when ran from ISE/console it contains the information i need, when ran ask task, it outputs nothing. I'm running as the same user as the task... Is there a different property when running as a task? i think not as i have never had this issue ever before. Commented Nov 16, 2015 at 9:56

1 Answer 1

1

This is a classic case of not running something elevated. Nothing in the code was wrong.

In Task Scheduler, make sure "Run with highest privileges" is ticked in for the task or it will not work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.