powershell等待工作不会等待

时间:2018-04-05 06:47:11

标签: powershell process wait jobs suspend

我试图获得一套设置指令,等待一个进程在它们启动之前完成,但我继续让变量尝试直接运行。

我会尝试wait(),但我不知道扫描需要多长时间。

#Variables
$logPath = "C:\Users\Administrator\Desktop\log.txt"
$scanPath = "C:\Users\Administrator\Desktop\scan.txt"
$repairLog = "C:\Users\Administrator\Desktop\repair.txt"
$failLog = "C:\Users\Administrator\Desktop\fail.txt"
$computer = "SERVER2012"
$CBSFileLocation = "C:\Windows\Logs\CBS\CBS.log"
$date = Get-Date
$sevenDays = New-Object System.TimeSpan 7,0,0,0,0
$repair = "Repair"
$fail = "fail"
$then = $date.Subtract($sevenDays)

#Start of Code:
$EventLog = Get-EventLog -LogName System -ComputerName $computer -After $then -Before $date -EntryType Error | Out-File $logPath
Start-Job -Name SFC {Start-Process sfc /scannow}

#I want the wait/suspend here.
Wait-Job -Name SFC {
    $ScanX = Get-Content $CBSFileLocation
    $ScanX | Out-File $scanPath
    Select-String -Path $scanPath -Pattern $repair | Out-File $repairLog
    Select-String -Path $scanPath -Pattern $fail | Out-File $failLog
    echo "Done!!"
}

1 个答案:

答案 0 :(得分:0)

我猜你想要等待,当SFC准备就绪时,在脚本的最后$ pip3 show networkx Name: networkx Version: 2.1 Summary: Python package for creating and manipulating graphs and networks Home-page: http://networkx.github.io/ Author: NetworkX Developers Author-email: networkx-discuss@googlegroups.com License: BSD Location: /usr/local/lib/python3.5/dist-packages Requires: decorator 内执行脚本。

据我在阅读{ ... } Wait-Job时所知,没有任何脚本块参数允许这样做。

请尝试重新排列代码,例如

#Variables
$logPath = "C:\Users\Administrator\Desktop\log.txt"
$scanPath = "C:\Users\Administrator\Desktop\scan.txt"
$repairLog = "C:\Users\Administrator\Desktop\repair.txt"
$failLog = "C:\Users\Administrator\Desktop\fail.txt"
$computer = "SERVER2012"
$CBSFileLocation = "C:\Windows\Logs\CBS\CBS.log"
$date = Get-Date
$sevenDays = New-Object System.TimeSpan 7,0,0,0,0
$repair = "Repair"
$fail = "fail"
$then = $date.Subtract($sevenDays)

#Start of Code:
$EventLog = Get-EventLog -LogName System -ComputerName $computer -After $then -Before $date -EntryType Error | Out-File $logPath

#Start the job and return the prompt once the job has State Completed
Start-Job {Start-Process sfc /scannow} | Wait-Job

#These lines are executed once Wait-Job above returns the prompt
$ScanX = Get-Content $CBSFileLocation
$ScanX | Out-File $scanPath
Select-String -Path $scanPath -Pattern $repair | Out-File $repairLog
Select-String -Path $scanPath -Pattern $fail | Out-File $failLog
echo "Done!!"
相关问题