【问题标题】:powershell wait-job doesn't waitpowershell wait-job 不等待
【发布时间】:2018-04-05 06:47:10
【问题描述】:

我正在尝试获取一组指令以等待进程完成后再开始,但我一直让变量尝试在之后直接运行。

我会尝试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!!"
}

【问题讨论】:

    标签: powershell process wait jobs suspend


    【解决方案1】:

    我猜你想等待,当 SFC 准备好后,执行脚本中最后一个 { ... } 内的脚本。

    据我阅读documentationWait-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!!"
    

    【讨论】:

    • 我刚刚删除了一个类似的答案,因为你的更快更好。
    猜你喜欢
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 2015-01-04
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多