Satheesh V
Satheesh V

Reputation: 146

Adding a Custom Object to a Array in Powershell

I would like to add a custom object with Properties as below to a powershell array. I have tried various options but unable to get the proper syntax of such an object addition to an array. Iam using Powershell V2. Please help.

PS C:\Windows\system32> $executemultiplerequest | Get-member


   TypeName: Microsoft.Xrm.Sdk.Messages.ExecuteMultipleRequest

Name          MemberType            Definition
----          ----------            ----------
Equals        Method                bool Equals(System.Object obj)
GetHashCode   Method                int GetHashCode()
GetType       Method                type GetType()
ToString      Method                string ToString()
Item          ParameterizedProperty System.Object Item(string parameterName) {get;set;}
ExtensionData Property              System.Runtime.Serialization.ExtensionDataObject ExtensionData {get;set;}
Parameters    Property              Microsoft.Xrm.Sdk.ParameterCollection Parameters {get;set;}
RequestId     Property              System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, Publ...
RequestName   Property              System.String RequestName {get;set;}
Requests      Property              Microsoft.Xrm.Sdk.OrganizationRequestCollection Requests {get;set;}
Settings      Property              Microsoft.Xrm.Sdk.ExecuteMultipleSettings Settings {get;set;}

Adding the Options i have tried :

Here are the samples that i have tried with :

$item = New-Object System.Object
$item | Add-Member -MemberType Method -Name Equals $executemultiplerequest.Equals -MemberType Property $executemultiplerequest.ExtensionData
$array += item

I received an error even before i went onto store into an array. The error was received on the Add-Member line as :

Add-Member : Cannot add a member with type "Method". Specify a different type for the MemberTypes parameter. 

Upvotes: 1

Views: 1427

Answers (1)

Kiran Reddy
Kiran Reddy

Reputation: 2904

i am not sure about the types you are using but if you want to

$item = New-Object psobject
$item | Add-Member -MemberType NoteProperty -Name RequestName -Value 'some string here'
$item | Add-Member -MemberType ScriptMethod -Name Multiply -Value {param($x,$y);$x * $y }
$item | Add-Member -MemberType ScriptProperty -Name RequestName1 -Value { Get-Service -Name BITS }

read more about Add-member here

Upvotes: 1

Related Questions