【问题标题】:Get status of a process started by Invoke-WmiMethod获取由 Invoke-WmiMethod 启动的进程的状态
【发布时间】:2011-07-19 22:48:13
【问题描述】:

刚接触 PowerShell,但我很喜欢到目前为止我可以这么快做这么多事情的事实 :)

无论如何,我正在 PowerShell 脚本中启动一个远程进程:

$compname = "MY-PC"
$myinstallcmd = "c:\install\myprog.exe /s"
$proc = Invoke-WmiMethod -class Win32_Process -name Create -ArgumentList ($myinstallcmd) -ComputerName $compname

在我尝试过的大多数 PC 上,Invoke-WmiMethod cmdlet 都能正常工作,但在一台 PC 上,它就挂了。我现在要做的是获取正在运行的进程的状态,如果它已挂起,则将其杀死并记录杀死,然后继续。

我确实在帖子中找到了一种可能的方法来做到这一点 Starting a process remotely in Powershell, getting %ERRORLEVEL% in Windows - 但是,当我尝试对进程 $proc.ProcessId 执行 Register-WmiEvent 时,我遇到了可怕的 0x80070005 (E_ACCESSDENIED) 错误...我正在以域管理员身份运行 PowerShell 主机。

任何人都可以提出一种方法来获取我已启动的流程的状态,并能够根据状态采取行动吗?

谢谢!

【问题讨论】:

    标签: powershell powershell-2.0 win32-process


    【解决方案1】:

    更新:我猜你缺少远程系统凭据:

    尝试使用-Credential 参数将凭据传递给远程系统。这需要一个 PSCredential 对象,因此您可以执行以下操作:

    $cred = Get-Credential
    Register-WMIEvent -Credential $cred <and other parameters here>
    

    查看以下任何一项是否解决了拒绝访问错误:

    0x80070005 (DCOM ACCESS_DENIED) 当远程服务器无法识别或以某种方式限制连接的用户(例如,用户可能被锁定)时,会发生此错误。当帐户位于不同域中时,最常发生这种情况。最近对 WMI 安全性的更改也可能导致发生此错误:

    • 以前允许的空白密码在 Windows XP 和 Windows Server 2003 中不允许使用。

    • WMI 不允许异步回调到 Windows 98 客户端。从 Windows 98 计算机到 Windows XP 计算机的 SWbemServices.ExecNotificationQueryAsync 之类的调用将导致向 Windows 98 计算机返回拒绝访问错误。

    • DCOM 配置访问设置可能已更改。

    • 如果目标计算机运行的是 Windows XP,则注册表项 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa 下的 Forceguest 值可能会设置为强制关闭来宾帐户(值为零)。

    来源:http://technet.microsoft.com/en-us/library/ee692772.aspx

    【讨论】:

    • 添加信息:运行 Win7 的系统上的 Powershell 主机,运行 Win Vista 的目标计算机。让powershell_ise 进程作为域管理员帐户运行。 Register-WmiEvent 没有-Authentication-Impersonation 的参数,就像Get-WmiObject 一样,我以前在遇到这种错误时使用过...
    • 可以使用-Credential参数提供凭据
    • 所以这是我现在刚刚尝试的:$proc2 = Invoke-WmiMethod -AsJob -class Win32_Process -name Create -ArgumentList ($installcmd) -ComputerName $compname -Authentication 6 -Credential $cred Register-WmiEvent -ComputerName $compname -Query "Select * from Win32_ProcessStopTrace Where ProcessID=$($proc2.ProcessId)" -Action { Write-Host "Process ExitCode: $($event.SourceEventArgs.NewEvent.ExitStatus)" } -Credential $cred -- 我仍然收到错误Register-WmiEvent : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
    • 另外我应该提到我确实通过Get-Credential 设置了$cred 变量并使用了域管理员帐户
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 2013-08-02
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多