6

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?

2
  • Why wouldn't you just return requestModel or requestModel.Response? Commented Jun 22, 2015 at 17:38
  • because that contains a few things like a big blob, that I don't want in the object. Commented Jun 27, 2015 at 5:26

2 Answers 2

10

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

So that gives me the same issue that other methods I've tried to. In the WriteResponse line, responseObject reports "Argument type 'System.Management.Automation.PSObject' is not assignable to parameter type 'string'
Sorry, was under the impression that you were inheriting the Cmdlet class (in which case you would return the object with WriteObject() and not WriteResponse())
That did the trick. I super appreciate everyone's comments and suggestions.
2

Without knowing all the details I can't say this is the best plan, but here is what I would do. You could define a class and then return it. So you would create a new class such as below:

public class RequestResponse {
    public int SiteID { get; set;}
    public string Identity { get; set; }
    other fields...
}

Next, in the code you posted, you would create the object and then fill the properties of the class.

var response = new RequestResponse();

if (requestModel != null && requestModel.Response != null)
{
    response.SiteID = requestModel.Response.SiteID;
    response.Identity  = requestModel.Response.Identity ;

    fill in other fields...
    this.WriteResponse(requestModel, response);
}

I hope this gets you started in the right direction.

Wade

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.