簡體   English   中英

PowerShell - 在等待作業(開始作業)完成時向用戶提供控制台 output

[英]PowerShell - provide console output to user while waiting for jobs (start-job) to finish

我使用這個腳本來運行一些作業:

#========================================================================
#Get User stats with ADInfo < --- need to see if this can be converted to native PowerShell
$XMLConfig = Get-ChildItem c:\ADInfo\XML_Config_Files

$Jobs = @()

#loop through each config file and run ADAudit - there is one file per domain
foreach ($config in $XMLConfig) {
        write-host "Starting a background job for $($config.name)"
        $Jobs += start-job -ScriptBlock {c:\ADInfoCmd.exe /config $args[0] } -ArgumentList $config.fullname.tostring()
}

write-host "`nJobs are running"


#=======================================================================
#End of script 

有些作業比其他作業花費更長的時間,我希望能夠在任何一個已啟動作業仍在運行以顯示腳本沒有停止時向控制台發送用戶友好的更新。

我嘗試過這樣的事情

do{
write-host "working..."
}
while (wait-job $jobs)

但它只寫一次,然后等待作業完成

我試過這個

$joblist = get-job $jobs | where state -eq running
while ($joblist){
write-host "working..."
}

但我收到所有作業get-job: The command cannot find the job because the job name System.Management.Automation.PSRemotingJob was not found並且$joblist從未分配過值。

有沒有辦法做到這一點?

我已經通過了整個 PS Object 才能找到工作。 當我只通過工作 ID 時它起作用了

這就是我最終使用的內容,並向用戶提供了足夠的反饋來證明腳本仍在工作。

write-host "`nJobs are running" -ForegroundColor Yellow -NoNewline
$RunningJobs = Get-Job $jobs.id | where state -eq running
while($runningjobs){
    write-host "." -NoNewline
    $RunningJobs = Get-Job $jobs.id | where state -eq running
    Start-Sleep -Seconds 3
}
Write-host "Background Jobs Complete"
Write-Host "Script Ends" -ForegroundColor Yellow

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM