Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell windows服务和Stop-Process cmdlet的有趣问题_Powershell_Process_Windows Services_Cmdlet - Fatal编程技术网

Powershell windows服务和Stop-Process cmdlet的有趣问题

Powershell windows服务和Stop-Process cmdlet的有趣问题,powershell,process,windows-services,cmdlet,Powershell,Process,Windows Services,Cmdlet,我们这里有一些自制的windows服务。其中一个是有问题的,因为当被问到时,它不会总是停止。它有时会陷入“停止”状态 我们正在使用powershell检索其PID,并使用Stop-Process cmdlet终止相关进程,但这也不起作用 相反,我们会收到一条关于名为System.ServiceProcess.ServiceController.Name的服务的消息,该服务显然不是我们的服务,而是它引用的PID 以下是我们为停止服务所做的工作。首先,我们使用Get-Service cmdlet:

我们这里有一些自制的windows服务。其中一个是有问题的,因为当被问到时,它不会总是停止。它有时会陷入“停止”状态

我们正在使用powershell检索其PID,并使用Stop-Process cmdlet终止相关进程,但这也不起作用

相反,我们会收到一条关于名为
System.ServiceProcess.ServiceController.Name的服务的消息,该服务显然不是我们的服务,而是它引用的PID

以下是我们为停止服务所做的工作。首先,我们使用Get-Service cmdlet:

$ServiceNamePID = Get-Service -ComputerName $Computer | where { ($_.Status -eq 'StopPending' -or $_.Status -eq 'Stopping') -and $_.Name -eq $ServiceName}
然后,使用ServiceNamePID,我们获得PID并在Stop-Process cmdlet中使用它

$ServicePID = (get-wmiobject win32_Service -ComputerName $Computer | Where { $_.Name -eq $ServiceNamePID.Name }).ProcessID
Stop-Process $ServicePID -force

此时,Stop-Process cmdlet squawks about
无法找到进程标识符为XYZ的进程,而实际上,根据任务管理器,PID XYZ是服务的正确进程ID。以前有人见过这样的问题吗?

要停止远程计算机上的进程,请使用远程处理,例如

 Invoke-Command -cn $compName {param($pid) Stop-Process -Id $pid -force } -Arg $ServicePID
这需要在远程PC上启用远程处理,并且本地帐户在远程PC上具有管理员价格

当然,使用远程处理后,您可以使用远程处理来编写脚本,例如:

Invoke-Command -cn $compName {
    $ServiceName = '...'
    $ServiceNamePID = Get-Service | Where {($_.Status -eq 'StopPending' -or $_.Status -eq 'Stopping') -and $_.Name -eq $ServiceName}
    $ServicePID = (Get-WmiObject Win32_Service | Where {$_.Name -eq $ServiceNamePID.Name}).ProcessID
    Stop-Process $ServicePID -Force
}

在Get服务和Get WmiObject上使用-ComputerName。服务是在另一台机器上运行的吗?很好,Keith,我自己也注意到了。似乎停止进程也不能在远程机器上运行?如果PShell脚本在另一台机器上运行(另一个框有管理员权限?),我可以做些什么呢?为了补充我的最后一点,我将尝试在Invoke命令-ComputerNameYup中包装Stop-Process cmdlet,这将是我的下一步。确保已在远程PC上启用远程处理。谢谢Keith。万一不是所有的远程机器上都安装了powershell,如果我调用get-wmiobject win32_process-cn,然后对返回的对象调用Terminate(),会怎么样?你能想到什么警告信号吗?假设GWMI可以连接到远程机器,这应该会起作用。这将有希望在我们的一些TopShelf服务中起到很好的作用,这些服务有时会表现得很愚蠢。