C#,为什么我仍然得到一个PowerShell输出结果,即使它从未分配过?

时间:2013-11-13 16:17:41

标签: c# asp.net powershell-v3.0

我注释掉了分配给str的powershell输出结果。我仍然在ResultBox中得到powershell输出,它不是假设的。可以被窃听吗?我试过调试器,它无法识别函数......

这是C#代码:

protected void ExecuteCode_Click(object sender, EventArgs e)
{
    // Clean the Result TextBox
    ResultBox.Text = string.Empty;
    ResultBox.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");

    string str = "";

    // Powershell
    Runspace runSpace = RunspaceFactory.CreateRunspace();
    runSpace.Open();
    Pipeline pipeline = runSpace.CreatePipeline();

    Command invokeScript = new Command("Invoke-Command");
    RunspaceInvoke invoke = new RunspaceInvoke();

    // invoke-command -computername compName -scriptblock { get-process }

    ScriptBlock sb = invoke.Invoke("{"+ PowerShellCodeBox.Text +"}")[0].BaseObject as ScriptBlock;
    invokeScript.Parameters.Add("scriptBlock", sb);
    invokeScript.Parameters.Add("computername", TextBoxServer.Text);

    pipeline.Commands.Add(invokeScript);
    Collection<PSObject> output = pipeline.Invoke();

    foreach (PSObject psObject in output)
    {

        //str = str + psObject;
        //str = psObject + "\r\n";
        //str += "\n" + psObject;
        //str = str + Environment.NewLine + psObject;
    }

    if (str == ""){
        str = "Error";

        ResultBox.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
    }

    ResultBox.Text = str;
}

0 个答案:

没有答案