如何使用C#运行Powershell脚本

时间:2018-10-10 09:32:09

标签: powershell

我正在尝试运行Powershell脚本,但没有成功。我在C:\ drive中有powershell脚本。该脚本接受UserCredentials和一个Export路径作为用户输入。 当我尝试在命令行上运行此脚本时,它会提示输入exportpath和凭据。

下面是我正在尝试的。

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

        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript("C:\\ex.ps1");
        pipeline.Commands.AddParameter(ExportPath", "C://");

       SecureString password = new SecureString();
       string str_password = "";
       string username = "";
       foreach (char x in str_password) { password.AppendChar(x); }
       PSCredential credential = new PSCredential(username, password);
       shell.Commands.AddParameter("Credential", credential);

       pipeline.Commands.Add("Out-String");
       Collection<PSObject> results = pipeline.Invoke();
       runspace.Close();

0 个答案:

没有答案