婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Powershell后臺作業、異步操作實例

Powershell后臺作業、異步操作實例

熱門標簽:如何申請400的電話呀 合肥電銷外呼系統供應商 地圖標注審核周期 沈陽ai電銷智能機器人 黑暗之魂3地圖標注 電商外呼系統排名 AI智能電銷機器人壞處 蘭州電銷機器人加盟 電話機器人對家居行業幫助大嗎

Powershell是單線程程序且一次只能做一件事情。后臺作業能額外增加Powershell進程在后臺處理作業。當需要程序同時運行且數據量不是很大時它能很好的解決問題。但從Powershell后臺回傳數據是一個非常麻煩的工作,它將浪費很多時間。將會導致腳本更慢。

這里有3個并發執行任務:

復制代碼 代碼如下:

$start = Get-Date

# get all hotfixes
$task1 = { Get-Hotfix }

# get all scripts in your profile
$task2 = { Get-Service | Where-Object Status -eq Running }

# parse log file
$task3 = { Get-Content -Path $env:windir\windowsupdate.log | Where-Object { $_ -like '*successfully installed*' } }

# run 2 tasks in the background, and 1 in the foreground task
$job1 =  Start-Job -ScriptBlock $task1
$job2 =  Start-Job -ScriptBlock $task2
$result3 = Invoke-Command -ScriptBlock $task3

# wait for the remaining tasks to complete (if not done yet)
$null = Wait-Job -Job $job1, $job2

# now they are done, get the results
$result1 = Receive-Job -Job $job1
$result2 = Receive-Job -Job $job2

# discard the jobs
Remove-Job -Job $job1, $job2

$end = Get-Date
Write-Host -ForegroundColor Red ($end - $start).TotalSeconds

上面執行全部的任務消耗了5.9秒。三個任務的結果將分別存入$result1, $result2, 和 $result3.
讓我們再繼續查看相繼在前臺執行完命令需要多長時間:

復制代碼 代碼如下:

$start = Get-Date

# get all hotfixes
$task1 = { Get-Hotfix }

# get all scripts in your profile
$task2 = { Get-Service | Where-Object Status -eq Running }

# parse log file
$task3 = { Get-Content -Path $env:windir\windowsupdate.log | Where-Object { $_ -like '*successfully installed*' } }

# run them all in the foreground:
$result1 = Invoke-Command -ScriptBlock $task1
$result2 = Invoke-Command -ScriptBlock $task2
$result3 = Invoke-Command -ScriptBlock $task3

$end = Get-Date
Write-Host -ForegroundColor Red ($end - $start).TotalSeconds

結果,這次只花費了5.05秒。與后臺作業幾乎同時完成,所以后臺作業更適合解決長時間執行的任務。從三個任務返回的數據觀察,好處是這種按順數在前臺獲得數據能減少了執行過程的開銷。

您可能感興趣的文章:
  • PowerShell快速創建一個指定大小文件的實例分享
  • PowerShell 獲取系統信息的函數

標簽:通遼 常州 河北 隴南 黔南 河池 黔南 淮南

巨人網絡通訊聲明:本文標題《Powershell后臺作業、異步操作實例》,本文關鍵詞  Powershell,后臺,作業,異步,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Powershell后臺作業、異步操作實例》相關的同類信息!
  • 本頁收集關于Powershell后臺作業、異步操作實例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 南召县| 襄樊市| 衡阳县| 日土县| 阜城县| 宝清县| 澄江县| 永安市| 商都县| 隆德县| 杂多县| 大悟县| 普兰县| 儋州市| 师宗县| 靖边县| 奇台县| 高陵县| 云浮市| 卓资县| 台前县| 台江县| 交城县| 九江县| 铜陵市| 天长市| 临猗县| 神木县| 二连浩特市| 封丘县| 金乡县| 通城县| 迁西县| 太康县| 通渭县| 永善县| 永年县| 曲松县| 自治县| 曲麻莱县| 吉水县|