Powershell - 检查远程进程,如果完成继续 [英] Powershell - Check on Remote Process, if done continue

查看:47
本文介绍了Powershell - 检查远程进程,如果完成继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为备份操作的一部分,我正在运行 7zip 命令以将文件夹压缩为单个 .7z 文件.没有问题,因为我使用的是 InVoke-WMIMethod.

As part of a backup operation, I am running the 7zip command to compress a folder into a single .7z file. No problems there as I am using the InVoke-WMIMethod.

示例:

$zip = "cmd /c $irFolder\7za.exe a $somedirectory.7z $somedirectory"
"InVoke-WmiMethod -class Win32_process -name Create -ArgumentList $zip -ComputerName $remotehost"

当我的脚本继续运行时,我的问题出现了,7za.exe 进程还没有完成.然后我试图从远程系统复制该项目,但它要么不完整,要么失败.

My problem comes in as my script continues, the 7za.exe process hasn't completed. I am then attempting to copy the item off of the remote system and it is either incomplete or fails.

有人能指出我如何确定 7za.exe 进程是否仍在运行,等到它死了,然后继续我的脚本的其余部分吗?

Can someone point me in the direction to figure out how to identify if the 7za.exe process is still running, wait until it is dead, then proceed with the rest of my script?

我可以通过...掌握从远程系统拉取进程

I can grasp pulling the process from the remote system via...

get-wmiobject -class Win32_Process -ComputerName $remotehost | Where-Object $_.ProcessName -eq "7za.exe"}

不确定如何将其转化为我的问题的可用信息.

Not sure how to turn that into usable info for my issue.

回答更新:(感谢@dugas 推动)

Answer UPDATE: (thx to nudge by @dugas)

这将为需要它的人提供一些反馈......

This will do it with some feedback for those that need it...

do {(Write-Host "Waiting..."),(Start-Sleep -Seconds 5)}
until ((Get-WMIobject -Class Win32_process -Filter "Name='7za.exe'" -ComputerName $target | where {$_.Name -eq "7za.exe"}).ProcessID -eq $null)

推荐答案

您可以使用 Invoke-Command cmdlet 在远程计算机上调用 Wait-Process cmdlet.示例:

You can invoke the Wait-Process cmdlet on the remote computer with the Invoke-Command cmdlet. Example:

$process = Invoke-WmiMethod -Class Win32_Process -Name create -ArgumentList notepad -ComputerName RemoteComputer

Invoke-Command -ComputerName RemoteComputer -ScriptBlock { param($processId) Wait-Process -ProcessId $processId } -ArgumentList $process.ProcessId

既然你提到使用 Invoke-Command 不是一个选项,另一个选项是轮询.示例:

Since you mentioned using Invoke-Command is not an option, another option is polling. Example:

$process = Invoke-WmiMethod -Class Win32_Process -Name create -ArgumentList notepad -ComputerName hgodasvccr01
$processId = $process.ProcessId

$runningCheck = { Get-WmiObject -Class Win32_Process -Filter "ProcessId='$processId'" -ComputerName hgodasvccr01 -ErrorAction SilentlyContinue | ? { ($_.ProcessName -eq 'notepad.exe') } }

while ($null -ne (& $runningCheck))
{
 Start-Sleep -m 250
}

Write-Host "Process: $processId is not longer running"

这篇关于Powershell - 检查远程进程,如果完成继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆