Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums

Go Back   Vista Forums > Vista technology newsgroups > PowerShell

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

Reply
 
Thread Tools Display Modes
Old 04-10-2008   #1
RickB
Guest
 
Posts: n/a

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'

  Reply With Quote

Old 04-10-2008   #2
RickB
Guest
 
Posts: n/a

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
  Reply With Quote
Old 04-10-2008   #3
Marco Shaw [MVP]
Guest
 
Posts: n/a

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
  Reply With Quote
 
Reply

Thread Tools
Display Modes









Vistax64.com 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 2005-2008

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 47 48