我有两个脚本要合并,但是第二个脚本要等到程序(Photoshop)关闭后才能开始。通过使用Invoke-Item启动Photoshop脚本来结束脚本。 Photoshop脚本完成后,PhotoShop将关闭。第二个代码使用简单的Move-Item归档原始文件。使用PowerShell,如何知道何时关闭PhotoShop并开始我的Move-Item?
我花了一些时间对此进行研究,以查看其中有什么文档,但是要么我问的问题很少,要么晦涩难懂,以至于找不到任何线索。
# Script One
ii "E:\resizerScript.jsx"
#Something to determine when PhotoShop is closed and begin the next bit of code.
# Script Two
Move-Item -path "E:\Staged\*" -Destination "E:\Archived"
我对编码非常陌生,其他文章将我所学的内容融合在一起。如果有什么不清楚的地方,我将很乐意阐述。在此先感谢您的帮助或指导。
答案 0 :(得分:1)
您可以使用Wait-Process
,
Invoke-Item "E:\resizerScript.jsx"
Wait-Process photoshop
Move-Item -Path "E:\Staged\*" -Destination "E:\Archived"
但是我建议使用Start-Process -Wait
启动Photoshop。
$photoshopPath = "C:\...\Photoshop.exe"
Start-Process $photoshopPath "E:\resizerScript.jsx" -Wait
Move-Item -Path "E:\Staged\*" -Destination "E:\Archived"
如果要设置超时时间:
Start-Process $photoshopPath "E:\resizerScript.jsx" -PassThru |
Wait-Process -Timeout (15 * 60) -ErrorAction Stop
答案 1 :(得分:0)
Get-Process | Select-Object -Property ProcessName
param(
[string]$procName = "Outlook",
[int]$timeout = 90, ## seconds
[int]$retryInterval = 1 ## seconds
)
$isProcActive = $true
$timer = [Diagnostics.Stopwatch]::StartNew()
# to check the process' name:
# Get-Process | Select-Object -Property ProcessName
while (($timer.Elapsed.TotalSeconds -lt $timeout) -and ($isProcActive)) {
$procId = (Get-Process | Where-Object -Property ProcessName -EQ $procName).Id
if ([string]::IsNullOrEmpty($procId))
{
Write-Host "$procName is finished"
$isProcActive = $false
}
}
$timer.Stop()
if ($isProcActive)
{
Write-Host "$procName did not finish on time, aborting operation..."
# maybe you want to kill it?
# Stop-Process -Name $procName
exit
}
# do whatever
[UPDATE]如果需要将其放在另一个脚本中,则需要省略param
,因为该必须是脚本中的第一件事。看起来像这样:
# start of script
$procName = "Outlook"
$timeout = 90 ## seconds
$retryInterval = 1 ## seconds
$isProcActive = $true
# etc etc
希望这会有所帮助,
吉姆