Get-WMIObject fails when run AsJob

Posted by codepoke on Stack Overflow See other posts from Stack Overflow or by codepoke
Published on 2010-04-20T19:51:08Z Indexed on 2010/04/20 19:53 UTC
Read the original article Hit count: 557

Filed under:

It's a simple question, but it's stumped me.

$cred = Get-Credential
$jobs = @()
$jobs += Get-WmiObject `
-Authentication 6 `
-ComputerName 'serverName' `
-Query 'Select * From IISWebServerSetting' `
-Namespace 'root/microsoftiisv2' `
-EnableAllPrivileges `
-Credential $cred `
-Impersonation 4 `
-AsJob

$joblist = Wait-Job -Job $jobs -Timeout 60

foreach ($job in $jobs)
{
    if ($job.State -eq "Completed")
    {
        $app = Receive-Job -Job $job
        $app
    } else {
        ("Job not completed: " + $job.Name + "@" + $job.State + ". Reason:" + $job.ChildJobs[0].JobStateInfo.Reason)
        Remove-Job -Job $job -Force
    }
}

The query succeeds when run directly and fails when run -AsJob.

Reason:System.UnauthorizedAccessException: Access is denied. 

I've jiggered with -Impersonation, -Credentials, -Authority, and -EnableAllPrivileges to no useful effect. It appears I'm overlooking something fundamental. Why is my Powershell prompt allowed to connect to the remote server, but my child process denied?

© Stack Overflow or respective owner

Related posts about powershell