This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
I Like sometimes a quick overview of I WMI class,
the MSH script below adds the Function :
get-WMIDatagrid (GWD)
to MSH, with this function you can look at the Instances of a WMI Object in a Datagrid.
I will first grab all Instances of the Class and add all the properties to a DataTable.
then it will launce a form with a Datagrid linked to the Generated DataTable.
this gives you a quick overview of the Class and the instances.
for example :
MSH> GWD win32_share
gr /\/\o\/\/
PS as I get All the Data you can get problems with a few classes (for example the Eventlog) that have very much data in them.(but there are only a few of them)
# WmiDatagrid.msh
# Shows Instances of WMIClass in Datagrid
# /\/\o\/\/ 2005
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
function get-WMIDatagrid {
Param ([string] $WmiClass = "")
if ($wmiClass -eq "") {Throw "No WMIClass given"}
$MC = new-object system.management.ManagementClass $wmiCLass
$MOC = $MC.GetInstances()
trap{"Class Not found";break}
$DT = new-object System.Data.DataTable
$DT.TableName = $wmiClass
$i = 0
$MOC | foreach {
$MO = $_
# Make a DataRow
$DR = $DT.NewRow()
$MO.get_properties() | foreach {
$prop = $_
If ($i -eq 0) {
# Only On First Row make The Headers
$Col = new-object System.Data.DataColumn
$Col.ColumnName = $prop.Name.ToString()
$prop.get_Qualifiers() | foreach {
If ($_.Name.ToLower() -eq "key") {
$Col.ColumnName = $Col.ColumnName + "*"
}
}
$DT.Columns.Add($Col)
}
#fill dataRow
if ($prop.value -eq $null) {
$DR.Item($prop.Name) = "[empty]"
}
ElseIf ($prop.IsArray) {
$DR.Item($prop.Name) =[string]::Join($prop.value ,";")
}
Else {
$DR.Item($prop.Name) = $prop.value
#Item is Key try again with *
trap{$DR.Item("$($prop.Name)*") = $prop.Value.tostring();continue}
}
}
# Add the row to the DataTable
$DT.Rows.Add($DR)
$i += 1
}
$form = new-object "System.Windows.Forms.Form"
$form.Size = new-object System.Drawing.Size @(800,400)
$DG = new-object "System.windows.forms.DataGrid"
$DG.CaptionText = "$wmiClass $($DT.Count)"
$DG.AllowSorting = $True
$DG.DataSource = $DT.mshobject.baseobject
$DG.Dock = [System.Windows.Forms.DockStyle]::Fill
#show the Form
$form.text = "$wmiCLass"
$form.Controls.Add($DG)
$form.topmost = $true
$form.showdialog()
}
set-alias gwd get-WMIDatagrid