Return powershell variable value to c# application

后端 未结 2 1029
庸人自扰
庸人自扰 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:33

    I know I am late to this, but in your script, you need to add global: in front of the variable you want to return in the Powershell script, so for example:

    $global:test = 4
    

    in Powershell script. In C# after you open the runspace, invoke the policy changer, set up the pipline, you do

    var result = runspace.SessionStateProxy.PSVariable.GetValue("test");
    

提交回复
热议问题