tags:

views:

108

answers:

1

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?

+1  A: 

Is the remote computer and the computer you are running this on configured for PowerShell V2 remoting? If you look at the help for get-wmiobject it states the following:

Note: To use this parameter with remote computers, the local and remote computers must be configured for remoting. Additionally, you must start Windows PowerShell by using the "Run as administrator" option in Windows V ista and later versions of Windows,. For more information, see about_Remote_Requirements.

James Pogran
Ah. I see now. When I was able to connect directly, I assumed the issue was authentication somehow. Actually, PowerShell is trying to run the command remotely. I cannot Enter-PSSession on any of the 200+ boxes I'm working against, and I cannot configure Remoting on them. I think I'll batch my requests and run them as simultaneous, multiple local sessions. Same benefits and works within my assigned environment. Thanks, James.
codepoke