使用c#中的参数运行powershell并将这些参数写入文本文件

使用c#中的参数运行powershell并将这些参数写入文本文件,c#,powershell,C#,Powershell,我正在尝试用c运行powershell脚本# 这是第一个测试脚本 param( [string] $computer, [string] $password ) out-file -filepath C:\File\parameter.txt -inputobject $computer -encoding ASCII -width 50 -Append out-file -filepath C:\File\params.txt -inputobject $password -encodin

我正在尝试用c运行powershell脚本# 这是第一个测试脚本

param(
[string] $computer,
[string] $password
)


out-file -filepath C:\File\parameter.txt -inputobject $computer -encoding ASCII -width 50 -Append
out-file -filepath C:\File\params.txt -inputobject $password -encoding ASCII -width 50 -Append
out-file -filepath C:\File\Sys32.txt -inputobject $password -encoding ASCII -width 50 -Append
这是我的C#应该运行powershellscript

     private void testRunningPowershell()
    {
        string mypass = gettingPass();

        using (Runspace runspace = RunspaceFactory.CreateRunspace())
        {
            runspace.Open();
            runspace.SessionStateProxy.Path.SetLocation("C:\\Users\\robert\\Desktop\\Titanium\\");

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

            // create a pipeline and feed it the script text (AddScript method) or use the filePath (Add method)
            using (Pipeline pipeline = runspace.CreatePipeline())
            {

                Command command = new Command(@"C:\Users\robert\Desktop\Titanium\Titanium2.ps1");
                dictionaryParams.Remove("FriendlyName");

                foreach (var item in dictionaryParams)
                {
                    if (item.Key == (string)comboBoxScreen.SelectedItem)
                    {
                        CommandParameter testParam = new CommandParameter(null, item.Value);
                        CommandParameter testParam2 = new CommandParameter(null, mypass);
                        command.Parameters.Add(null, testParam);
                        command.Parameters.Add(null, testParam2);
                    }
                }

                pipeline.Commands.Add(command);
                var results = pipeline.Invoke();

                ReturnInfo ri = new ReturnInfo();
                foreach (PSObject p in results)
                {
                    Hashtable ht = p.ImmediateBaseObject as Hashtable;

                    ri.ReturnText = (string)ht["ComputerName"];
                    ri.ReturnText = (string)ht["password"];
                }





                runspace.Close();
            }
        }
    }
我的目的是测试cmdlet是否使用正确的参数运行 我有一个windows窗体应用程序,允许用户选择参数 我想将参数值写入文本文件(.txt) 相反,我得到了这个

System.Management.Automation.Runspaces.CommandParameter

而不是参数的值choosen

Write Host
不会返回任何内容。因此,如果我像这样更改脚本,我是否可以看到参数param,如var result=pipeline.invoke();或者我怎么知道我的超级地狱被执行了?[string]$computer,[string]$password][hashtable]$Return=@{}$Return.ReturnCode=[int]1$Return.computerName=[string]$computer.ToString()$Return.password=[string]$password.ToString()Return$Return我可以在您的代码中看到以下问题:
Set ExecutionPolicy Unrestricted
-该命令在
LocalMachine
范围内设置执行策略,这需要管理员权限(因为它在失败时抛出异常,您必须注意,如果这是问题)。您从未打印或返回
resutl
变量(那么您怎么知道没有结果呢?)<代码>运行空间。关闭缺少的括号和
()缺少要调用的方法。修复所有问题后,您的代码对我来说运行良好。
Write Host
不会返回任何内容。因此,如果我像这样更改脚本,我是否可以看到参数param,如var result=pipeline.invoke();或者我怎么知道我的超级地狱被执行了?[string]$computer,[string]$password][hashtable]$Return=@{}$Return.ReturnCode=[int]1$Return.computerName=[string]$computer.ToString()$Return.password=[string]$password.ToString()Return$Return我可以在您的代码中看到以下问题:
Set ExecutionPolicy Unrestricted
-该命令在
LocalMachine
范围内设置执行策略,这需要管理员权限(因为它在失败时抛出异常,您必须注意,如果这是问题)。您从未打印或返回
resutl
变量(那么您怎么知道没有结果呢?)<代码>运行空间。关闭缺少的括号和
()缺少要调用的方法。在解决了所有问题之后,您的代码对我来说运行良好。