如何正确地将变量从C#控制台应用程序传递到PowerShell脚本?

时间:2016-05-19 00:10:24

标签: c# powershell .net-3.5

我正在使用.Net 3.5控制台应用程序并尝试将几个变量从应用程序传递到PowerShell脚本。出于某种原因,我的脚本没有执行。命令我是否试图将变量传递给函数?

C#:

public string oneId;
public int twoId;
public void Execute()
{
    string shellPath = "powershell.exe";
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("\"& '{0}'\" ", @"C:\Active_Scripts\PSupdate.ps1");
    sb.AppendFormat(" -{0} {1} -{2} {3}", "oID", oneId, "tID", twoId);

    string result = ExecuteCommand(shellPath, sb.ToString());
}

private static string ExecuteCommand(string shellPath, string arguments)
{
    arguments = "-noprofile " + arguments;
    var process = new Process();
    var info = process.StartInfo;

    process.StartInfo.UseShellExecute = false;
    process.StartInfo.FileName = shellPath;
    process.StartInfo.Arguments = arguments;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardOutput = true;

    process.Start();

    var output = process.StandardOutput;
    var error = process.StandardError;

    string result = output.ReadToEnd();
    process.WaitForExit();
    return result;
}

的PowerShell:

Function updateStuff
{
 param (
 [string]$entityID
 )
}

try
{
 param (
    [string]$oID, 
    [string]$tID
)
#### do some stuff
}
catch
{
}

0 个答案:

没有答案