More Wmi

Let's say you want to display a WMI object from a list of computernames inside a textfile
You want to create a foreach-object to scroll through the textfile and grab those names
Okay, how does that look into a commandline script?:

Get-Content computers.txt | foreach-object { Get-WMIObject Win32_operatingsystem -computer $_ }

This will get the content from computers.txt and for each object in computers.txt it will run the scriptblock get the WMI object named Win32_operatingsystem from computer whatever is in the pipeline

Would it not be better to write a script that will ping the host and run a bunch of WMI objects and displays only the things you want. Here is a neat little script that will do just that and you can modify and use it in different ways:

function Get-InventoryInfo {
        Write-Debug "`$_ contains $_"
        # Create new object and attach computer name as a property
        $obj = New-Object psobject
        $obj | Add-Member noteproperty Computername ($_)
        # attempt to ping whatever is in $computer
        $result = Get-WmiObject -Query "SELECT * FROM Win32_PingStatus WHERE Address = '$_'"

        # statuscode = 0 is good news; anything else is bad
        if ($result.StatusCode -eq 0) {
            $obj | Add-Member Noteproperty Responding $true
        } else {
            $obj | Add-Member NoteProperty Responding $false

        # Get the computer system info, attach two properties to our object
        $compsystem = Get-WmiObject Win32_computersystem -ComputerName $_
        $obj | Add-Member noteproperty Processors ($compsystem.numberoflogicalprocessors)
        $obj | Add-Member noteproperty Architecture ($compsystem.systemtype)

        # Get the OS info, attach two properties to our object
        $os = Get-WmiObject Win32_operatingsystem -ComputerName $_
        $obj | Add-Member noteproperty SPVer ($os.servicepackmajorversion)
        $obj | Add-Member noteproperty Build ($os.buildnumber)

        # Write our object to the pipeline
        Write-Output $obj

Get-Content c:\scripts\computernames.txt | Get-InventoryInfo | Format-Table -AutoSize

Let say you want to see all the installed software on a computer you would type:
Get-WmiObject -Computername computername Win32_Product | Sort-Object Name | Format-Table -auto Name, Version, Vendor

Let's say you want to find the events in the eventlog that describes the events of a diskfailure. Very important!
Get-EventLog -LogName System -computername servername -InstanceId 3221487627 -ea 0 | ForEach-Object { $_.ReplacementStrings[0] } | Group-Object -NoElement | Sort-Object Count -Descending

On Me

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License