When running PowerShell script as a scheduled task some Exchange 2010 database properties are null

Posted by barophobia on Server Fault See other posts from Server Fault or by barophobia
Published on 2011-01-13T21:01:58Z Indexed on 2011/01/13 21:55 UTC
Read the original article Hit count: 454

Filed under:
|

Hello,

I've written a script that intends to retrieve the DatabaseSize of a database from Exchange 2010. I created a new AD user for this script to run under as a scheduled task. I gave this user admin rights to the Exchange Organization (as a last resort during my testing) and local admin rights on the Exchange machine.

When I run this script manually by starting powershell (with runas /noprofile /user:domain\user powershell) everything works fine. All the database properties are available. When I run the script as a scheduled task a lot of the properties are null including the one I want: DatabaseSize.

I've also tried running the script as the domain admin account with the same results.

There must be something different in the two contexts but I can't figure out what it is.

My script:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

Write-EventLog 'Windows PowerShell' -source PowerShell -eventid 100 -message "Starting script"

$databases = get-mailboxdatabase -status

if($databases -ne $null)
{
    Write-EventLog 'Windows PowerShell' -source PowerShell -eventid 100 -message "Object created"

    $databasesize_text = $databases.databasesize.tomb().tostring()

    if($databasesize_text -ne $null)
    {
        $output = "echo "+$databasesize_text+":ok"

        Write-EventLog 'Windows PowerShell' -source PowerShell -eventid 100 -message "Path check"

        if(test-path "\\mon-01\prtgsensors\EXE\")
        {
            Write-EventLog 'Windows PowerShell' -source PowerShell -eventid 100 -message "Path valid"
            Set-Content \\mon-01\prtgsensors\EXE\ex-05_db_size.bat -value $output
        }

        Write-EventLog 'Windows PowerShell' -source PowerShell -eventid 100 -message "Exiting program"
    }
    else
    {
        Write-EventLog 'Windows PowerShell' -source PowerShell -eventid 100 -message "databasesize_text is empty. nothing to do"
    }
}
else
{
    Write-EventLog 'Windows PowerShell' -source PowerShell -eventid 100 -message "object not created. nothing to do"
}

exit 0

© Server Fault or respective owner

Related posts about powershell

Related posts about exchange-2010