Pages

tirsdag 19. august 2014

OpsMgr: Properties with brackets in PowerShell

Have you tried to select or filter on properties using brackets in PowerShell? Did you figure out how to do it? If not, read on...

For example, I execute this command in Operations Manager Shell:
Get-SCOMClass -DisplayName 'Windows Computer'|Get-SCOMClassInstance|select -First 1 *

And the result:
[Microsoft.Windows.Server.Computer].IsVirtualNode            : (null)
[Microsoft.Windows.Computer].PrincipalName                   : dc1.contoso.com
[Microsoft.Windows.Computer].DNSName                         : dc1.contoso.com
[Microsoft.Windows.Computer].NetbiosComputerName             : dc1
[Microsoft.Windows.Computer].NetbiosDomainName               : CONTOSO
[Microsoft.Windows.Computer].IPAddress                       : 10.0.0.10
[Microsoft.Windows.Computer].NetworkName                     : dc1.contoso.com
[Microsoft.Windows.Computer].ActiveDirectoryObjectSid        : S-0-0-00-000000000-0000000000-000000000-0000
[Microsoft.Windows.Computer].IsVirtualMachine                : True
[Microsoft.Windows.Computer].DomainDnsName                   : contoso.com
[Microsoft.Windows.Computer].OrganizationalUnit              : OU=Computers,DC=contoso,DC=com
[Microsoft.Windows.Computer].ForestDnsName                   : contoso.com
[Microsoft.Windows.Computer].ActiveDirectorySite             : contoso
[Microsoft.Windows.Computer].LogicalProcessors               : 1
[Microsoft.Windows.Computer].PhysicalProcessors              : 1
[Microsoft.Windows.Computer].HostServerName                  : (null)
[Microsoft.Windows.Computer].VirtualMachineName              : (null)
[Microsoft.Windows.Computer].OffsetInMinuteFromGreenwichTime : (null)
[Microsoft.Windows.Computer].LastInventoryDate               : (null)
[Microsoft.SystemCenter.ManagedComputer].InstallDirectory    : C:\Program Files\Microsoft Monitoring Agent\Agent\
[Microsoft.Windows.Server.2008.Computer].InstallType         : Full
[System.ConfigItem].ObjectStatus                             : System.ConfigItem.ObjectStatusEnum.Active
[System.ConfigItem].AssetStatus                              : (null)
[System.ConfigItem].Notes                                    : (null)
[System.Entity].DisplayName                                  : dc1.contoso.com
IsManaged                                                    : True
HealthState                                                  : Success
StateLastModified                                            : 8/19/2014 10:00:00 AM
IsAvailable                                                  : True
AvailabilityLastModified                                     : 8/19/2014 10:00:00 AM
InMaintenanceMode                                            : False
MaintenanceModeLastModified                                  :
MonitoringClassIds                                           : {e817d034-02e8-294c-3509-01ca25481689, ea99500d-8d52-fc52-b5a5-10dcd1ba-33f801db7d37...}
LeastDerivedNonAbstractMonitoringClassId                     : ea99500d-8d52-fc52-b5a5-10dcd1e9d2bd
ManagementGroup                                              : SCOMMG
Name                                                         : dc1.contoso.com
Path                                                         :
DisplayName                                                  : dc1.contoso.com
FullName                                                     : Microsoft.Windows.Computer:dc1.contoso.com
ManagementPackClassIds                                       : {e817d034-02e8-294c-3509-01ca25481689, ea99500d-8d52-fc52-b5a5-10dcd1ba-33f801db7d37...}
LeastDerivedNonAbstractManagementPackClassId                 : ea99500d-8d52-fc52-b5a5-10dcd1e9d2bd
TimeAdded                                                    : 8/19/2014 10:00:00 AM
LastModifiedBy                                               :
Values                                                       : {(null), dc1.contoso.com, dc1.contoso.com, dc1...}
LastModified                                                 : 8/19/2014 10:00:00 AM
IsNew                                                        : False
HasChanges                                                   : False
Id                                                           : 83f8d115-ccc8-5d88-2345-00d0148ecbd3
ManagementGroupId                                            : c6f7583b-f37e-101a-30fa-e53639ee0d80

Now you may wonder, how do I use Select-Object to view only Name and IPAddress? Well, square brackets require double-escaping in PowerShell (see this Microsoft Connect Feedback), so this is how:
Get-SCOMClass -DisplayName 'Windows Computer'|Get-SCOMClassInstance|select -First 1 Name,``[Microsoft.Windows.Computer`].IPAddress

Great, but what if I would like to use Where-Object to filter on IPAddress? Well, in this case we do not need to backtick-escape these brackets, instead we need to put single quotes around the entire property name, like this:
Get-SCOMClass -DisplayName 'Windows Computer'|Get-SCOMClassInstance|where{$_.'[Microsoft.Windows.Computer].IPAddress' -match '10.0.0.10'}|select Name,``[Microsoft.Windows.Computer`].IPAddress