![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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' 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 Specs![]() |
| | #3 (permalink) |
| | 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' > 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 Specs![]() |
![]() |
| 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 | |||