簡體   English   中英

如果要執行策略繞過,如何在后台運行Powershell?

[英]How can I run powershell in the background if I want execution policy Bypass?

我有運行Powershell腳本的此批處理文件。

我想在后台運行它,但是如果以“隱藏的Windowstyle”運行,仍然可見。

powershell -ExecutionPolicy ByPass-隱藏的Windowstyle-文件“ C:\\ script.ps1”

您可以將例如長時間運行的腳本作為作業運行。

要啟動它,請運行

$job = Start-Job -ScriptBlock {Get-Process}

這將在后台啟動Get-Process cmdlet。 該腳本也可以是一些自定義腳本或更長的腳本,它不必是單行的。

您可以通過運行來檢查其狀態

$job | Get-Job 

並接收您運行的輸出

$job | Receive-Job

請注意,一旦接收到數據,數據就會丟失。 您只能收到一次,然后由您決定將其保存在變量中或進行后續處理。

最后從您運行的隊列中刪除作業

$job | Remove-Job

我使用以下功能:

function bg() {
  Start-Process `
   -WorkingDirectory (Get-Location) `
   -NoNewWindow `
   -FilePath "powershell" `
   -ArgumentList "-NoProfile -command `"$args`" " 
}

它啟動一個新的Powershell實例,該實例在后台執行,並允許使用cmdlet。 您這樣稱呼它:

bg "Start-Sleep 2; get-location; write 'done' "

暫無
暫無

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

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