Powershell 如何判断特定的ProcessName是否正在运行而不崩溃
假设有一个名为exampleService的应用程序应该在Server1上运行。如果该代码正在运行,则该代码有效。但是,当它不运行时,它会崩溃Powershell 如何判断特定的ProcessName是否正在运行而不崩溃,powershell,windows-7,windows-server-2012,Powershell,Windows 7,Windows Server 2012,假设有一个名为exampleService的应用程序应该在Server1上运行。如果该代码正在运行,则该代码有效。但是,当它不运行时,它会崩溃 $application = Get-Process -ComputerName Server1 -Name "exampleService" 如果应用程序未运行,我会发生此崩溃。有没有更优雅的方法来确定它是否没有运行(没有崩溃) 如果应用程序未运行,是否可以在服务器上启动该应用程序 服务器正在运行Windows Server 2012。PowerSh
$application = Get-Process -ComputerName Server1 -Name "exampleService"
如果应用程序未运行,我会发生此崩溃。有没有更优雅的方法来确定它是否没有运行(没有崩溃)
如果应用程序未运行,是否可以在服务器上启动该应用程序
服务器正在运行Windows Server 2012。PowerShell命令正在Windows 7 64位PC上运行。请查看使用
-ErrorAction SilentlyContinue
来防止显示该错误。如果应用程序没有运行,可以在If语句中使用它来启动应用程序
--已更新,包括启动远程进程
If (-NOT (Get-Process -Computername Server1 -name "cmd" -ErrorAction SilentlyContinue)) {
Write-Host "Launch application"
$application = "c:\windows\system32\cmd.exe"
$start = ([wmiclass]"\\Server1\Root\CIMV2:win32_process").Create($application)
}
您可以将
ErrorAction
设置为SilentlyContinue
(别名为-ea0
):
$application=Get Process-ComputerName Server1-Name“exampleService”-ea 0
现在,您可以检查
$application
并在其为空时启动应用程序。我只希望脚本在一个特定的Get进程错误上继续,即找不到进程。(我更喜欢使用Try/Catch)。但是我没有做太多的powershell,并且很难找到具体的错误
一旦我发现我可以查看FullyQualifiedErrorId,并将以下内容添加到我找到的通用捕获块中
Write-Host ('FullyQualifiedErrorId: ' + $_.FullyQualifiedErrorId);
因此,作为一个适用于我的情况的完整示例:
Try {
$application = Get-Process -Name "exampleService" -ea Stop #note the -ea Stop is so try catch will fire whatever ErrorAction is configured
} Catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
If ($_.FullyQualifiedErrorId) {
If ($_.FullyQualifiedErrorId -eq "NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand"){
Write-Host "Presume not running. That is OK."; # or you could perform start action here
}
}
else {
throw #rethrow other processcommandexceptions
}
} Catch {
# Log details so we can refine catch block above if needed
Write-Host ('Exception Name:' + $_.Exception.GetType().fullname); # what to put in the catch's square brackets
If ($_.FullyQualifiedErrorId) {
Write-Host ('FullyQualifiedErrorId: ' + $_.FullyQualifiedErrorId); #what specific ID to check for
}
throw #rethrow so script stops
}
如何启动应用程序?这是真实的代码。我只是编造了一个不存在的进程名,以确保它将强制显示
“启动应用程序”
文本。您可以用实际命令(以及要查找的实际进程名)替换该文本来启动应用程序。。如果(-NOT(Get Process-name“cmd”-ErrorAction SilentlyContinue)){'Launch application'$application=“c:\windows\system32\cmd.exe”$start={([wmiclass]“win32_Process”)。Create($application)}它对我有效(运行windows 7/PowerShell V4)。您应该删除最后一行中的大括号,因为这只会创建一个sciptblock,而不会实际运行代码$start=([wmiclass]“win32_进程”)。创建($application)
但它会在我的计算机或远程计算机上创建进程吗?我更新了示例以更好地满足您希望执行的操作,该操作应启动远程进程。仅供参考:收到错误!=和撞车一样。在大多数情况下,即使遇到错误,PS脚本仍将继续运行(如果下一个命令依赖于上一个命令的输出,则结果不可预测)。
Try {
$application = Get-Process -Name "exampleService" -ea Stop #note the -ea Stop is so try catch will fire whatever ErrorAction is configured
} Catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
If ($_.FullyQualifiedErrorId) {
If ($_.FullyQualifiedErrorId -eq "NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand"){
Write-Host "Presume not running. That is OK."; # or you could perform start action here
}
}
else {
throw #rethrow other processcommandexceptions
}
} Catch {
# Log details so we can refine catch block above if needed
Write-Host ('Exception Name:' + $_.Exception.GetType().fullname); # what to put in the catch's square brackets
If ($_.FullyQualifiedErrorId) {
Write-Host ('FullyQualifiedErrorId: ' + $_.FullyQualifiedErrorId); #what specific ID to check for
}
throw #rethrow so script stops
}