用c#运行Powershell脚本,例外

本文关键字:例外 脚本 Powershell 运行 | 更新日期: 2023-09-27 18:07:29

我正在尝试运行一个powershell脚本,我尝试了两种不同的方法,但都得到例外。我需要添加什么代码或者我需要更改什么代码?

在脚本中,我试图添加一个参数;

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

使用Pipeline()和Command()的第一种方法:

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.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()

Process()的第二种方法:

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());
            }
        }

这个代码给出了一个异常:

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。

用c#运行Powershell脚本,例外

有帮助的答案是用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());
                }
            }