halr9000
Commander
Commander

NameValueCollection

Looking back at the "hard way" examples that Carter provided earlier, I see a line like this:

$nvc = new-object System.Collections.Specialized.NameValueCollection

Now, I don't really know what these objects do that is so special, never used them before, but...Once I saw in the code how it is used, I'm left wondering why can't I use a native powershell hashtable? e.g.

$filter = @{ Name = "MyVirtualMachine" }

to be followed by "find-entityview -viewtype VirtualMachine -filter $filter" for example.

This seems like a really logical way to go, but again I admit I don't know crap about NVCs so maybe there's a reason something like this can't be done.

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
2 Replies
halr9000
Commander
Commander

Doesn't work. Smiley Sad

83# $filter = @{ 'guest.guestState' = 'running'; 'guest.toolsStatus' = 'toolsOk' }
84# $filter

Name                           Value
----                           -----
guest.toolsStatus              toolsOk
guest.guestState               running


85# $vms = Find-EntityViews -ViewType VirtualMachine -Filter $filter
Find-EntityViews : Cannot bind parameter 'Filter'. Cannot convert "System.Collections.Hashtable" to "System.Collections.Specia
lized.NameValueCollection".
At line:1 char:57
+ $vms = Find-EntityViews -ViewType VirtualMachine -Filter <<<<  $filter

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
halr9000
Commander
Commander

Good news. VMware decided to create a new constructor for the underlying method so that it'll take hashtables.

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos