How would I return an object or multiple values from PowerShell to executing C# code

后端 未结 3 1427
忘掉有多难
忘掉有多难 2021-01-30 17:22

Some C# code executes a powershell script with arguments. I want to get a returncode and a string back from Powershell to know, if everything was ok inside the Powershell script

3条回答
  •  [愿得一人]
    2021-01-30 17:58

    Wow, good question! I'll take a shot off the top of my head...

    You could design a class in C# that represents the structure you want to use to pass data between the two. In the PS script, you could use an XmlWriter to craft an XML response and use Write-output to spit out the XML string.

    On the C# side, capture the standard out response, deserialize the XML into your new response class, and then process the result. Note that you can't write anything out to stdout other than your XML response, or else you won't be able to deserialze into the class.

提交回复
热议问题