2017-08-07 40 views
0

我需要從另一個PowerShell腳本爲不同的用戶和隱藏的窗口中運行PowerShell腳本。運行PowerShell腳本爲不同的用戶和隱藏的窗口從另一個PowerShell腳本

什麼我到目前爲止有:

Start-Process powershell '& C:\test.ps1' -WindowStyle Hidden -Credential $cred 

其中$credPSCredential對象。該test.ps1腳本是用於測試目的與Remove-Item一行。由於我仍然可以看到文件沒有被刪除,我可以告訴腳本沒有運行。

編輯

我的初衷是爲了運行regedit劇本,但當時我正在執行Start-Job答案(下)我得到這個錯誤:

Start-Job : The value of the FilePath parameter must be a Windows PowerShell script file. Enter th 
e path to a file with a .ps1 file name extension and try the command again. 
+0

因爲這個問題是如此普遍,我想指出的是,一個'PSCredential'對象使用此技術的用戶是「管理員」的成員不會運行提升。你必須爲此使用'Runas'動詞。 (我不知道這是否是你的意圖或沒有。) –

回答

1

您可以使用jobs這一點。作爲Powershell documentation

A Windows PowerShell background job runs a command without interacting with the current session.

PS> $myJob = Start-Job -FilePath C:\test.ps1 -Credential $cred 

爲了得到這份工作的結果/輸出,使用Receive-Job

PS> $myJob | Receive-Job -Keep 
+0

想你的解決方案,並與PowerShell腳本,但不與註冊表編輯器腳本完美。認爲這將是相同的,該死的PowerShell中... – pandemic

+0

文件'C:\ test.ps1'should包含'REGEDIT.EXE/SC:\ file.reg',或者你可以通過一個腳本塊,以'啓動工作'就像這樣:'Start-Job -ScriptBlock {regedit /sc:\file.reg} -Credential $ cred' –

0

您可以運行通過PowerShell腳本任務調度程序。我不確定這是否是您的目標,但它可讓您使用憑據在用戶會話上執行PowerShell腳本。

我會讓你谷歌如何,以滿足您的需求。

相關問題