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;
}
4

0 回答 0