Get-WMIObject fails when run AsJob
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.
Rea开发者_运维百科son: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?
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.
The "Access Denied" you are seeing is actually from DCOM. -AsJob and WMI use a better, more efficent form of WMI remoting known as asynchronous remoting. Because of this, you have to do some additional firewall configuration changes. In particular, I believe you need to "Allow Remote Administration" in the Windows Firewall UI.
This document on MSDN describes the setting in greater detail:
Connecting to WMI Thru Windows Firewall
Hope this helps
精彩评论