2016-09-19 115 views
0

我想運行一個powershell腳本,我嘗試了兩種不同的方法,但從兩個方面都得到了異常。我需要添加哪些代碼或需要更改哪些內容?用C#運行Powershell腳本,例外

腳本內我試圖添加一個參數;

Param([string]$MainDoc="not") 

首先計算策略與管道()和命令():

System.Management.Automation.Host.HostException: Cannot invoke this function because the current host does not implement it. 
    at System.Management.Automation.Internal.Host.InternalHostRawUserInterface.ThrowNotInteractive() 
    at System.Management.Automation.Internal.Host.InternalHostRawUserInterface.get_ForegroundColor() 
    at Microsoft.PowerShell.Commands.ConsoleColorCmdlet.get_ForegroundColor() 
    at Microsoft.PowerShell.Commands.WriteHostCommand.PrintObject(Object o) 
    at Microsoft.PowerShell.Commands.WriteHostCommand.PrintObject(Object o) 
    at Microsoft.PowerShell.Commands.WriteHostCommand.ProcessRecord() 
    at System.Management.Automation.Cmdlet.DoProcessRecord() 
    at System.Management.Automation.CommandProcessor.ProcessRecord() 

與過程第二種方法()::

public static void RunPowerShell(string filePath, string parameterValue, string parameterName) 
     { 

      try 
      { 

       Process p = new Process(); 

       p.StartInfo.UseShellExecute = false; 
       p.StartInfo.RedirectStandardOutput = true; 

       p.StartInfo.FileName = filePath; 
       p.StartInfo.Arguments = " -" + parameterName + " " + parameterValue; 

       p.Start(); 

      } 
      catch (Exception e) 
      { 

       System.Diagnostics.Debug.WriteLine(e.GetBaseException()); 

      } 
     } 

public static void RunPowerShell(string filePath, string parameterValue, string parameterName) 
     { 

      try 
      { 

       Runspace runspace = RunspaceFactory.CreateRunspace(); 
       runspace.Open(); 

       RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace); 
       runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); 

       Pipeline pipeline = runspace.CreatePipeline(); 
       Command command = new Command(filePath); 

       CommandParameter argParam = new CommandParameter(parameterName, parameterValue); 
       command.Parameters.Add(argParam); 

       pipeline.Commands.Add(command); 

       pipeline.Invoke(); 
       runspace.Close(); 

      }catch(Exception e){ 

       System.Diagnostics.Debug.WriteLine(e.GetBaseException()); 

      } 
     } 

從該代碼的異常

此代碼給出例外:

System.ComponentModel.Win32Exception (0x80004005): The specified executable is not a valid application for this OS platform. 
    at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) 
    at System.Diagnostics.Process.Start() 

我只是不明白,因爲它應該適用於我的操作系統平臺。

我錯過了什麼,應該改變什麼?這應該是一個簡單的任務,只用一個參數運行一個Powershell。

日Thnx提前

+0

你試過從AnyCPU切換到x86或64位,看它是否運行? – Marco

+0

也許這可以幫助你:https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/ – Kage

+0

在第二種方法中'filePath'包含了什麼。如果它是腳本名稱,請將UseShellExecute設置爲true,或者使用PowerShell.exe對其進行前綴。 –

回答

0

答案,幫助用Powershell.exe到它前面加上下面的代碼。 filePath包含PowerShell腳本的路徑,parameterValue包含PowerShell腳本參數的值。

public static void RunPowerShell(string filePath, string parameterValue) 
      { 

       try 
       { 

        Process.Start("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\Powershell.exe", "" + filePath + " '" + parameterValue + "'"); 

       } 
       catch (Exception e) 
       { 

        System.Diagnostics.Debug.WriteLine(e.GetBaseException()); 

       } 
      }