简体   繁体   English

使用C#运行Powershell脚本,异常

[英]Run Powershell script with C#, Exceptions

I am trying to run a powershell script and I have tried two different approaches but get exceptions from both. 我正在尝试运行Powershell脚本,并且尝试了两种不同的方法,但都从这两种方法中获取了例外。 What code do I need to add or what do I need to change? 我需要添加什么代码或需要更改什么?

Inside the script am I trying to add a parameter; 我正在脚本中尝试添加参数;

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

First approch with Pipeline() and Command(): 首先使用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());

            }
        }

Exception from this code: 此代码异常:

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

Second approach with Process(): 使用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());

            }
        }

This code gives exception: 这段代码给出了异常:

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

Which I just can not understand since it should be valid for my OS platform. 我只是不明白,因为它应该对我的OS平台有效。

What have I missed and what should I change? 我错过了什么,我应该改变什么? This should be a simple task, just running one Powershell with one argument. 这应该是一个简单的任务,只需使用一个参数运行一个Powershell。

Thnx in advance 提前Thnx

The answer that helped were to preface it with Powershell.exe as the code below. 帮助的答案是使用Powershell.exe作为下面的代码开头。 filePath contains the path to the powershell script and the parameterValue contains the value for the powershell scripts parameter. 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());

                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM