Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Unexpected error $instance.psobject.members.Add($member)

Reply
 
Old 04-10-2008   #1 (permalink)
RickB


 
 

Unexpected error $instance.psobject.members.Add($member)

This code creates a synthetic object.

$instance = new-object Management.Automation.PSObject
Add-Member -InputObject $instance NoteProperty 'Type' $type
write-host "Instance created - $type"
$instance.psobject.members|write-host
foreach ($member in $members)
{ Write-Host "Adding member '$($member.name)'"
$instance.psobject.members.Add($member)
}

As you can see in the output below, all the members
are added except the 'ToString' psScriptMethod.
But I get no error if I add this very same method to
this very same object, using exactly the same syntax,
at the command prompt.
Can anyone give me a hint why?

Instance created - PFW_FileID
System.String Type=PFW_FileID
System.String ToString()
System.Type GetType()
System.Boolean Equals(Object obj)
System.Int32 GetHashCode()Adding member 'LName'
Adding member 'EventNumber'
Adding member 'ToString'
Exception calling "Add" with "1" argument(s): "A PSProperty or
PSMethod object cannot be added to this collection."
At C:\...\Define_Class.ps1:84 char:40
+ $instance.psobject.members.Add <<<< ($member)
Adding member 'Match'


My System SpecsSystem Spec
Old 04-10-2008   #2 (permalink)
RickB


 
 

Re: Unexpected error $instance.psobject.members.Add($member)

On Apr 10, 7:47*am, RickB <rbiel...@xxxxxx> wrote:
Quote:

> This code creates a synthetic object.
>
> * * *$instance = new-object Management.Automation.PSObject
> * * *Add-Member -InputObject $instance NoteProperty 'Type' $type
> * * *write-host "Instance created - $type"
> * * *$instance.psobject.members|write-host
> * * *foreach ($member in $members)
> * * *{ * Write-Host "Adding member '$($member.name)'"
> * * * * *$instance.psobject.members.Add($member)
> * * *}
>
> As you can see in the output below, all the members
> are added except the 'ToString' *psScriptMethod.
> But I get no error if I add this very same method to
> this very same object, using exactly the same syntax,
> at the command prompt.
> Can anyone give me a hint why?
>
> Instance created - PFW_FileID
> System.String Type=PFW_FileID
> System.String ToString()
> System.Type GetType()
> System.Boolean Equals(Object obj)
> System.Int32 GetHashCode()Adding member 'LName'
> Adding member 'EventNumber'
> Adding member 'ToString'
> Exception calling "Add" with "1" argument(s): "A PSProperty or
> PSMethod object cannot be added to this collection."
> At C:\...\Define_Class.ps1:84 char:40
> + * * * * *$instance.psobject.members.Add <<<< ($member)
> Adding member 'Match'
Sorry,
Never mind.

I was caching the ScriptMethods like this.
Obviously there would be a native Method called ToString
so my synthetic member was never being created. The native
method really was a duplicate and thus the error.

if ($class.$name -eq $null) {
Add-Member -InputObject $class ScriptMethod $name $script}
$class.$name
My System SpecsSystem Spec
Old 04-10-2008   #3 (permalink)
Marco Shaw [MVP]


 
 

Re: Unexpected error $instance.psobject.members.Add($member)

Quote:

> At C:\...\Define_Class.ps1:84 char:40
> + $instance.psobject.members.Add <<<< ($member)
> Adding member 'Match'
>
Are you able to provide us with all of the code? We don't see how $type
and $members are defined, for example.

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Remove members of the group. Members are from different domains PowerShell
Unexpected Error: Fax Can't be received because of an unexpected e Vista print fax & scan
Creating Instance Failed Error (8007045a) .NET General
Bug? Strange wrapping of string into psobject causes Select error PowerShell
Get-Member repeats members for identical types - bug? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46