Return powershell variable value to c# application

后端 未结 2 1027
庸人自扰
庸人自扰 2021-01-22 03:08

I am running powershell script from c#.

string scriptPath = \"/script/myscript.ps1\";
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pi         


        
2条回答
  •  被撕碎了的回忆
    2021-01-22 03:27

    string variable_to_return_from_ps_script = "test"; 
    
    // create Powershell runspace
    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    
    //
    // here you write the code to invoke the PS script, pipeline, pass parameters etc...
    // just like the code you already have
    //
    
    // and here's how you retrieve a variable test from PS
    var out_var = runspace.SessionStateProxy.PSVariable.GetValue(variable_to_return_from_ps_script);
    Console.WriteLine("Variable ${0} value is: ", variable_to_return_from_ps_script);
    Console.WriteLine(out_var.ToString());
    

提交回复
热议问题