2014-09-21 73 views
3

我想用C#在運行空間中執行帶有參數的Powershell文件。不幸的是我得到以下輸出:C#Runspace Powershell(交互式)

A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows. 

我該怎麼辦?

當前的c#代碼。這需要執行將在PS文件中並需要返回json字符串的命令。

public string ExecuteCommandDirect(int psId, string psMaster, string psFile) 
{ 
    String FullPsFilePath = @"C:\CloudPS\" + psFile + ".ps1"; 

    String PsParameters = FullPsFilePath + " -psId " + psId + " -psMaster " + psMaster + " -NonInteractive"; 

    // Create Powershell Runspace 
    Runspace runspace = RunspaceFactory.CreateRunspace(); 

    runspace.Open(); 

    // Create pipeline and add commands 
    Pipeline pipeline = runspace.CreatePipeline(); 
    pipeline.Commands.AddScript(PsParameters); 

    // Execute Script 
    Collection<PSObject> results = new Collection<PSObject>(); 
    try 
    { 
     results = pipeline.Invoke(); 
    } 
    catch (Exception ex) 
    { 
     results.Add(new PSObject((object)ex.Message)); 
    } 

    // Close runspace 
    runspace.Close(); 

    //Script results to string 
    StringBuilder stringBuilder = new StringBuilder(); 
    foreach (PSObject obj in results) 
    { 
     Debug.WriteLine(obj); 
     stringBuilder.AppendLine(obj.ToString()); 
    } 

    return stringBuilder.ToString(); 

} 

PS代碼:

param([int]$psId = 0, [string]$psMaster = 'localhost'); 

$date = Get-Date -Format 'h:m:s' | ConvertTo-Json; 

Write-Host $date; 

exit; 
+0

你可以發佈你正在使用的代碼? – DanM7 2014-09-21 20:36:04

+1

嗨,我已經添加了代碼。 – user2702653 2014-09-21 20:46:45

+0

試着移除'Write-Host',只留下'$ date'。 ''出口'也不需要。 – 2014-09-21 20:56:21

回答

5

使用Write-Output而不是Write-Host

+0

我GOOGLE了一下,我點擊了,我滾動,看到這個答案。我從-Host切換到-Output,並停止獲取OP發佈的錯誤消息。這個答案不僅正確,而且簡潔和有用。 – Benrobot 2016-08-16 23:14:03

+0

工作完美。 – Anand 2016-11-02 05:08:55

1

我已經通過重寫這兩個cmdlet來自己解決了讀主機和寫主機的問題。在我的解決方案中,我悄悄放棄了任何發送給Write-Host的東西,但我需要實現Read-Host。這啓動了一個新的powershell進程,並將結果返回。

 string readHostOverride = @" 
      function Read-Host { 
       param(
        [string]$Prompt 
       ) 



       $StartInfo = New-Object System.Diagnostics.ProcessStartInfo 
       #$StartInfo.CreateNoWindow = $true 
       $StartInfo.UseShellExecute = $false 
       $StartInfo.RedirectStandardOutput = $true 
       $StartInfo.RedirectStandardError = $true 
       $StartInfo.FileName = 'powershell.exe' 
       $StartInfo.Arguments = @(""-Command"", ""Read-Host"", ""-Prompt"", ""'$Prompt'"") 
       $Process = New-Object System.Diagnostics.Process 
       $Process.StartInfo = $StartInfo 
       [void]$Process.Start() 
       $Output = $Process.StandardOutput.ReadToEnd() 
       $Process.WaitForExit() 
       return $Output.TrimEnd() 
      } 
     "; 

     string writeHostOverride = @" 
      function Write-Host { 
      } 
     "; 

     Run(readHostOverride); 
     Run(writeHostOverride); 

Run()是,毫不奇怪,我執行PowerShell的方法。

你可以很容易地擴展它來實現寫主機,讓我知道你是否有困難。

對於建議切換到寫入輸出而不是寫入主機的評論者,這不是許多用例的解決方案。例如,您可能不希望用戶通知進入輸出流。

如果您需要使用原來的cmdlet,使用此:

Microsoft.PowerShell.Utility\Read-Host -Prompt "Enter your input"