在C#cmdlet中创建PowerShell PSObject

在C#cmdlet中创建PowerShell PSObject,c#,powershell,object,C#,Powershell,Object,C#新手,但在PowerShell方面经验丰富。接管别人的代码。编写已编译的PowerShell模块,并尝试了解如何基于返回的数据创建对象。现在,代码返回一个字符串: ServerResponse<UCCSiteModel> requestModel = this.MakeRequest("/site/api/", "site", "GET", this.Credentials, queryString); StringBuilder builder = new StringBuil

C#新手,但在PowerShell方面经验丰富。接管别人的代码。编写已编译的PowerShell模块,并尝试了解如何基于返回的数据创建对象。现在,代码返回一个字符串:

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());
}
ServerResponse-requestModel=this.MakeRequest(“/site/api/”,“site”,“GET”,this.Credentials,queryString);
StringBuilder=新的StringBuilder();
if(requestModel!=null&&requestModel.Response!=null)
{
builder.AppendLine(“SiteID:+requestModel.Response.SiteID”);
builder.AppendLine(“标识:”+requestModel.Response.SiteName);
builder.AppendLine(“站点代码:”+requestModel.Response.SiteCode);
builder.AppendLine(“联系人姓名:”+requestModel.Response.ContactName);
builder.AppendLine(“联系人号码:”+requestModel.Response.ContactNumber);
builder.AppendLine(“联系人电子邮件地址:”+requestModel.Response.ContactEmailAddress);
builder.AppendLine(“地址:”+requestModel.Response.Address);
builder.AppendLine(“城市:+requestModel.Response.City”);
AppendLine(“状态:”+requestModel.Response.State);
builder.AppendLine(“邮政编码:”+requestModel.Response.PostCode);
AppendLine(“时区:+requestModel.Response.Timezone”);
AppendLine(“经度:”+requestModel.Response.Longitude);
AppendLine(“纬度:+requestModel.Response.Latitude”);
this.WriteResponse(requestModel,builder.ToString());
}

如何从
requestModel.Response
创建对象以发送回PowerShell而不是字符串?编写PowerShell时,我通常会使用
新对象PsObject
,然后使用
添加成员
。不知道如何在C#中实现这一点,或者它叫什么(这样我就可以搜索)。有人吗?

在不知道所有细节的情况下,我不能说这是最好的计划,但我会这么做。您可以定义一个类,然后返回它。因此,您将创建一个新类,如下所示:

public class RequestResponse {
    public int SiteID { get; set;}
    public string Identity { get; set; }
    other fields...
}
接下来,在您发布的代码中,您将创建对象,然后填充类的属性

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);
}
我希望这能让你从正确的方向开始


Wade

只需在
PSObject
Members
属性上调用
Add()
,即可镜像
Add Member
的行为(为了便于在PowerShell中访问,我会将属性名称更改为CamelCase):


你为什么不直接返回
requestModel
requestModel.Response
?因为它包含一些我不想在对象中出现的东西,比如一个大斑点。因此,这给了我与我尝试过的其他方法相同的问题。在WriteResponse行中,responseObject报告“参数类型”System.Management.Automation.PSObject“不可分配给参数类型”string“对不起,您的印象是您继承了
Cmdlet
类(在这种情况下,您将使用
WriteObject()
返回对象,而不是
WriteResponse()
)我非常感谢大家的评论和建议。
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);
}