none
Getting an instance of the PSObject model

    Question

  • I'm calling PowerShell from C# and I was hoping to be able to get an instance of the PSObject model(s) returned from the pipeline Invoke() method call.

    Is this possible?  If so, how do I include the model definitions in my code and create an instance from the PSObject?

    Thank you.


    David Downing

    • Moved by Bill_Stewart Monday, May 1, 2017 6:15 PM Move to more appropriate forum
    Monday, May 1, 2017 5:51 PM

Answers

  • It looks like referencing the Azure.RM assemblies does the trick.

    As an experiment:

    1. I added a reference to: "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.Profile\Microsoft.Azure.Commands.Profile.dll"
    2. Included the namespace "using Microsoft.Azure.Commands.Profile.Models;"
    3. Made a simple call to "Add-AzureRMAccount"
    4. And cast the results to the appropriate object type (PSContext in this case)
    var result = ((PSAzureProfile)results[0].BaseObject).Context;
    


    David Downing

    • Marked as answer by Dave Downing Monday, May 1, 2017 8:30 PM
    Monday, May 1, 2017 8:30 PM

All replies

  • You cannot return objects from an external process.  If you host PowerShell you can return objects.  See PowerShell SDK for examples.

    \_(ツ)_/

    Monday, May 1, 2017 6:34 PM

  • You cannot return objects from an external process.  If you host PowerShell you can return objects.  See PowerShell SDK for examples.

    \_(ツ)_/

    Monday, May 1, 2017 6:35 PM
  • As an example from VS quick watch, I can see the following:

    Microsoft.Azure.Commands.Profile.Models.PSAzureContext

    and

    ((Microsoft.Azure.Commands.Profile.Models.PSAzureProfile)result.ImmediateBaseObject).Context.Account

    So there is clearly a .Net object involved... In this case I'm interested in the PSAzureContext model.

    Anyone have in any insight?


    David Downing

    Monday, May 1, 2017 7:37 PM
  • It looks like referencing the Azure.RM assemblies does the trick.

    As an experiment:

    1. I added a reference to: "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.Profile\Microsoft.Azure.Commands.Profile.dll"
    2. Included the namespace "using Microsoft.Azure.Commands.Profile.Models;"
    3. Made a simple call to "Add-AzureRMAccount"
    4. And cast the results to the appropriate object type (PSContext in this case)
    var result = ((PSAzureProfile)results[0].BaseObject).Context;
    


    David Downing

    • Marked as answer by Dave Downing Monday, May 1, 2017 8:30 PM
    Monday, May 1, 2017 8:30 PM