Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

WMI Saved Commands GUI

The commands.json

WMI Commands

Not all of these work, but the concept does. Documenting the version I have so far.

{
    "Hardware Information": {
        "Computer Name": "Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Name",
        "Operating System Version": "Get-WmiObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty Version",
        "Serial Number": "Get-WmiObject -Class Win32_BIOS | Select-Object -ExpandProperty SerialNumber",
        "System Manufacturer": "Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Manufacturer",
        "System Model": "Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Model",
        "Installed Memory (Bytes)": "Get-WmiObject -Class Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum | Select-Object -ExpandProperty Sum",
        "Number of Processors": "Get-WmiObject -Class Win32_Processor | Measure-Object | Select-Object -ExpandProperty Count",
        "Processor Name": "Get-WmiObject -Class Win32_Processor | Select-Object -ExpandProperty Name",
        "Processor Speed (MHz)": "Get-WmiObject -Class Win32_Processor | Select-Object -ExpandProperty MaxClockSpeed",
        "Number of Processor Cores": "Get-WmiObject -Class Win32_Processor | Select-Object -ExpandProperty NumberOfCores"
    },
    "Network Information": {
        "List of Network Adapters": "Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -ne $null} | Select-Object -Property Description, IPAddress",
        "Number of Network Connections": "Get-WmiObject -Class Win32_PerfRawData_Tcpip_NetworkInterface | Where-Object {$.Name -eq 'NetworkInterfaceName'} | Select-Object -ExpandProperty CurrentConnections",
        "Number of Active TCP Connections": "Get-WmiObject -Class Win32_PerfRawData_Tcpip_TCPv4 | Select-Object -ExpandProperty ConnectionsEstablished",
        "Number of Active UDP Connections": "Get-WmiObject -Class Win32_PerfRawData_Tcpip_UDPv4 | Select-Object -ExpandProperty DatagramsReceivedPerSecond",
        "Number of Bytes Received per Second": "Get-WmiObject -Class Win32_PerfRawData_Tcpip_NetworkInterface | Where-Object {$.Name -eq 'NetworkInterfaceName'} | Select-Object -ExpandProperty BytesReceivedPersec",
        "Number of Bytes Sent per Second": "Get-WmiObject -Class Win32_PerfRawData_Tcpip_NetworkInterface | Where-Object {$.Name -eq 'NetworkInterfaceName'} | Select-Object -ExpandProperty BytesSentPersec"
    },
    "Process Information": {
        "List of Running Processes": "Get-WmiObject -Class Win32_Process | Select-Object -Property Name, ProcessId, CommandLine",
        "Process CPU Usage": "Get-WmiObject -Class Win32_PerfFormattedData_PerfProc_Process | Where-Object {$.Name -eq 'ProcessName'} | Select-Object -ExpandProperty PercentProcessorTime",
        "Process Memory Usage": "Get-WmiObject -Class Win32_PerfFormattedData_PerfProc_Process | Where-Object {$_.Name -eq 'ProcessName'} | Select-Object -ExpandProperty WorkingSet"
    }
}

The Script.ps1

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Load the WMI commands from the JSON file
$json = Get-Content -Path 'commands.json' | ConvertFrom-Json

# Define the form and controls
$form = New-Object System.Windows.Forms.Form
$form.Text = "WMI Command GUI"
$form.Width = 600
$form.Height = 400

$menu = New-Object System.Windows.Forms.MenuStrip
$menu.Location = New-Object System.Drawing.Point(0, 0)
$menu.Width = $form.Width
$form.Controls.Add($menu)

# Add the menu items for each category
foreach ($category in $json.PSObject.Properties) {
    $submenu = New-Object System.Windows.Forms.ToolStripMenuItem
    $submenu.Text = $category.Name
    foreach ($command in $category.Value.PSObject.Properties) {
        $menuItem = New-Object System.Windows.Forms.ToolStripMenuItem
        $menuItem.Text = $command.Name
        $menuItem.Tag = $command.Value
        $menuItem.add_Click({
            $output.Text = Invoke-Expression $this.Tag
        })
        $submenu.DropDownItems.Add($menuItem)
    }
    $menu.Items.Add($submenu)
}

$output = New-Object System.Windows.Forms.TextBox
$output.Location = New-Object System.Drawing.Point(10, 50)
$output.Width = 560
$output.Height = 300
$output.Multiline = $true
$output.ReadOnly = $true
$form.Controls.Add($output)

# Show the form
$form.ShowDialog() | Out-Null
Last updated on 27 Aug 2023
Published on 27 Aug 2023