Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create PowerShell PSObject in C# cmdlet

New to C# but experienced in PowerShell. Taking over someone else's code. Writing a compiled PowerShell module, and trying to figure out how to create an object based on returned data. Right now, code returns a string:

ServerResponse<UCCSiteModel> requestModel = this.MakeRequest("/site/api/", "site", "GET", this.Credentials, queryString);
StringBuilder builder = new StringBuilder();

if (requestModel != null && requestModel.Response != null)
{
    builder.AppendLine("SiteID: " + requestModel.Response.SiteID);
    builder.AppendLine("Identity: " + requestModel.Response.SiteName);
    builder.AppendLine("Site Code: " + requestModel.Response.SiteCode);
    builder.AppendLine("Contact Name: " + requestModel.Response.ContactName);
    builder.AppendLine("Contact Number: " + requestModel.Response.ContactNumber);
    builder.AppendLine("Contact Email Address: " + requestModel.Response.ContactEmailAddress);
    builder.AppendLine("Address: " + requestModel.Response.Address);
    builder.AppendLine("City: " + requestModel.Response.City);
    builder.AppendLine("State: " + requestModel.Response.State);
    builder.AppendLine("Post Code: " + requestModel.Response.PostCode);
    builder.AppendLine("Time Zone: " + requestModel.Response.Timezone);
    builder.AppendLine("Longitude: " + requestModel.Response.longitude);
    builder.AppendLine("Latitude: " + requestModel.Response.latitude);                    
    this.WriteResponse(requestModel, builder.ToString());
}

How do I create an object from requestModel.Response to send back to PowerShell instead of the string? When writing PowerShell, I would normally use New-Object PsObject, and then Add-Member. Not sure how to do that in C#, or what it's called (so I can search). Anyone?

like image 770
Pat Richard Avatar asked Jun 22 '15 16:06

Pat Richard


People also ask

What is PSObject in PowerShell?

A PSObject is very much like a hashtable where data is stored in key-value pairs. You can create as many key-value pairs as you like for each PSObject .

What is the difference between PSObject and PSCustomObject?

[PSCustomObject] is a type accelerator. It constructs a PSObject, but does so in a way that results in hash table keys becoming properties. PSCustomObject isn't an object type per se – it's a process shortcut. ... PSCustomObject is a placeholder that's used when PSObject is called with no constructor parameters.

How do you create an array of objects in PowerShell?

By default, an array in PowerShell is created as a [PSObject[]] type. This allows it to contain any type of object or value. This works because everything is inherited from the PSObject type.


1 Answers

You can mirror the behavior of Add-Member simply by calling Add() on the Members property of your PSObject (I would change the property names to CamelCase for ease of accessibility in PowerShell):

if (requestModel != null && requestModel.Response != null)
{
    PSObject responseObject = new PSObject();

    responseObject.Members.Add(new PSNoteProperty("SiteID", requestModel.Response.SiteID));
    responseObject.Members.Add(new PSNoteProperty("Identity", requestModel.Response.SiteName));
    // and so on etc... 
    responseObject.Members.Add(new PSNoteProperty("Latitude", requestModel.Response.latitude));

    this.WriteObject(responseObject);
}
like image 173
Mathias R. Jessen Avatar answered Sep 29 '22 09:09

Mathias R. Jessen