Deadly-Bagel
Deadly-Bagel

Reputation: 1620

$array[index] doesn't work for some collections?

Just looking for someone to point me in the right direction please, repeatedly in PowerShell I come across collections that I can't get elements of using [x].

The one I've just hit is in IIS:

Import-Module WebAdministration
[System.Reflection.Assembly]::LoadFrom("C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll") | Out-Null
$serverManager = New-Object Microsoft.Web.Administration.ServerManager

$site = $servermanager.sites[0]

This returns nothing. However, $servermanager.sites | foreach-object {$site = $_} correctly loops through each object.

Using gm only returns the members of the object, not the collection, and I've been unable to find anything online to explain this behaviour.

Upvotes: 0

Views: 42

Answers (1)

Deadly-Bagel
Deadly-Bagel

Reputation: 1620

Actually managed to solve it just after finishing the question using gettype() then looking it up.

In this particular case you need to use $servermanager.sites.item(0) to get the item at index 0.

Upvotes: 1

Related Questions