Create PowerShell PSObject in C# cmdlet
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);}
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