Get-WmiObject with credential fails when within Start-Job scriptblock

你离开我真会死。 提交于 2019-12-05 09:14:57

问题


I am successfully retrieving some information from Windows 2000 machines using the Get-WmiObjet cmdlet. These machines are not part of our domain so I am using the -Credential parameter to pass local administrator credentials.

I am now trying to run several WMI queries in parallel using Start-Job but I can't get even one query to work.

When I run the following:

Start-Job -initializationscript {$cred = get-credential -credential administrator}  -scriptblock {gwmi win32_computersystem -ComputerName 10.1.2.3 -Credential $cred}

a job is created, I am prompted for the credentials, but the job never completes, its state is always "Running".

Of course:

C:\>$cred = Get-Credential -credential administrator
C:\>gwmi win32_computersystem -ComputerName 10.1.2.3 -Credential $cred

works just fine.

How do I get Get-WmiObject to run successfully within Start-Job with alternate credentials?

Thanks for your help.


回答1:


Try this:

$cred = Get-Credential -Credential Administrator
Start-Job -scriptblock {Param ($cred) gwmi win32_computersystem -ComputerName 10.1.2.3 -Credential $cred} -ArgumentList $cred

Looks like the background job is blocked for input and has been running forever for that reason.



来源:https://stackoverflow.com/questions/4856083/get-wmiobject-with-credential-fails-when-within-start-job-scriptblock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!