2014-02-18 21 views
2

運行PowerShell的我要開始從C#PowerShell腳本和get()上pipeline.Invoke的PSSSecurityException從C#

AuthorizationManager檢查失敗。

我的代碼:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); 
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration)) 
{ 
    runspace.Open(); 
    Pipeline pipeline = runspace.CreatePipeline(); 
    Command scriptCommand = new Command(scriptfile); 
    pipeline.Commands.Add(scriptCommand); 
    pipeline.Invoke(); 
} 

問題

  1. ,我懷疑,我需要設置PSCredential的。但我不能保證,所以我必須在代碼中處理這個問題。這可以以安全的方式完成嗎? (這種情況並非如此

回答

1

的問題是,該腳本放在一個份額。要在此共享上運行腳本,我需要將該共享添加到受信任的站點。

enter image description here

enter image description here

2

看看這個超級用戶後:https://superuser.com/questions/106360/how-to-enable-execution-of-powershell-scripts

你可能只需要允許未簽名的腳本運行。在PS控制檯上,鍵入以下內容:

set-executionpolicy remotesigned 

另一個資源相呼應,這樣的:http://tgnp.me/2011/09/powershell-authorizationmanager-check-failed-resolution/

給一個嘗試,讓我知道會發生什麼。

捕獲來自腳本的輸出,試試這個:

Collection output = pipeline.Invoke(); 
foreach (PSObject psObject in output) 
{ 
    <here you can ToString the psObject for the output and write it line by line to your log> 
} 
+0

謝謝您的輸入,我可以使用部分有關打印輸出,對部分「AuthorizationManager檢查失敗。」與我的環境有關的另一個問題。我現在已經弄明白了,並且會爲遇到同樣問題的其他人發佈解決方案。 – Jorn