Posts tagged “WMI”

I’ve added an additional parameter to allow passing a PSCredential object to this function/cmdlet. This is helpful when the user running the script/cmdlet does not have permissions on the target computer. Another enhancement in this update to to trap errors resulting from failure to WMI connect to the target computer like in case of ‘Access Denied’, or ‘RPC Server not available’ errors.

Get-SBWMI PowerShell function is part of the SB-Tools PowerShell module. It provides similar functionality to the native Get-WMIObject cmdlet with added option: Timeout.

The function takes 1 required parameter; ‘Class’. For example

Get-SBWMI Win32_computerSystem

will return output similar to:

It also accepts the following optional parameters:

Property

Property name of the provided ‘Class’ such as ‘NumberofLogicalProcessors’ property of the ‘Win32_computerSystem’ class:

Get-SBWMI Win32_computerSystem -Property NumberofLogicalProcessors

Filter

In the format Property=Value such as ‘DriveLetter=e:’

Get-SBWMI -Class Win32_Volume -Filter 'DriveLetter=e:'

ComputerName

Remote computer name or IP address

Get-SBWMI Win32_computersystem -ComputerName mgmt

NameSpace

The default is ‘root\cimv2’. To see name spaces type:

(Get-WmiObject -Namespace 'root' -Class '__Namespace').Name

TimeOut

In seconds. The default is 20.

This is a particularly helpful option especially if you’re in the situation of trying to query many hundreds or thousands of computers, where the vast majority respond quickly but few can drag out for several minutes or not respond at all causing the script to hang if using the native Get-WMIObject cmdlet.

Who remembers systeminfo? I liked to use systeminfo | more on my Windows XP and Win 7 systems to get the boot datetime, but Microsoft appears to have stripped the property in Windows 8 and Windows Server 2012. At any rate, it’s not what I want.

What would you say is the most efficient way in Windows PowerShell v3 or later to get not the boot time but the elapsed uptime of the box in an eyeball-friendly format?

Ultimately, I’d like to add this function to my profile script so I see the info at-a-glance every time I start the console. I’d like to see output that is similar to the following:

Uptime: 8 days, 12 hours, 36 minutes

I thought sure that should be easy via querying WMI from Powershell. Then Dave Wyatt added:

Windows 8 will throw you all off, though. By default, when you “Shut down” a Windows 8 computer, you’re actually going into a sleep / hibernate state, and waking up from that doesn’t reset the WMI LastBootUpTime value (but a reboot does; go figure.) You can figure out the last “boot” time on Windows 8 by searching the event log, though.