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?