Posts tagged “WMI

All about time

timeIn a post in the Powershell Q&A forum, Tim Warner wrote:

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.

Which got me thinking, with Windows 8 / Server 2012 and above we may not be getting uptime by querying WMI, so I looked into different ways to get computer uptime and wrote this script which can be downloaded from the Microsoft Script Center Repository.

Script output looks like:


This script shows different ways to get and display computer uptime.  The script saves output to a log file in the folder where it runs. Log file name can be modified by editing the $LogFile variable.

The script gets uptime from:

  1. The VM’s Hyper-V host if the computer is a virtual machine
  2. Systeminfo
  3. Net statistics
  4. Event Log
  5. Win32_OperatingSystem class
  6. Win32_PerfFormattedData_PerfOS_System class (pointed out by Vern Anderson)

8/1/2014 – V.10

Migrating IP settings from one NIC to another using Powershell

Here’s an example scenario where the following script may be particularly useful:

GridStore array where each node currently uses one 1 Gbps NIC. After adding 10 Gbps NIC to each node, we’d like to migrate the IP settings from the 1 Gbps NIC to the 10 Gbps NIC on each node. GridStore utilizes commodity rack mount servers and hardware and a robust software driver to present scalable, high performance, fully redundant vLUNs. More detailed posts on GridStore will follow.

This diagram shows network connectivity before adding the 10 Gbps NICs:



After adding the 10 Gbps NICs:



  1. You will need administrative credentials to the nodes from GridStore technical support
  2. From Server1, using GridControl snapin, stop all vLUNs:
  3. RDP to each node.
  4. Currently nodes run Windows 7 Embedded and the RDP session will bring up a command prompt.
  5. Run Control to show Control Panel, double-click Network and Sharing Center, click Change Adapter Settings to view/confirm that you have 2 connected NICs:
  6. Start Powershell ISE:
  7. Copy/paste the following script and run it on each node:


# Script to move network configuration from one NIC to another on a GridStore node
# Sam Boutros
# 6/16/2014
# Works with Powershell 2.0
Set-Location “c:\support”
$Loc = Get-Location
$Date = Get-Date -format yyyymmdd_hhmmsstt
$logfile = $Loc.path + “\Move-GSNIC_” + $env:COMPUTERNAME + “_” + $Date + “.txt”
function log($string) {
Write-Host $string; $temp = “: ” + $string
$string = Get-Date -format “ hh:mm:ss tt”; $string += $temp
$string | out-file -FilePath $logfile -append
log “Switching NIC configuration on $env:COMPUTERNAME”
$ConnectedNICs = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter ‘IPEnabled=”true”‘
If ($ConnectedNICs.Count -lt 2) {log “Error: Less than 2 connected NICs:”; log $ConnectedNICs}
else {
if ($ConnectedNICs.Count -gt 2) {log “Error: More than 2 connected NICs:”; log $ConnectedNICs}
else { # 2 connected NICs
Stop-Service GridStoreManagementService
# Storing NICs details in variables for later use
$NIC0Index = $ConnectedNICs[0].index; log “NIC 0 Index: $NIC0Index”
$NIC0Desc = $ConnectedNICs[0].description; log “NIC 0 Description: $NIC0Desc”
$NIC0IPv4 = $ConnectedNICs[0].IPAddress[0]; log “NIC 0 IPv4: $NIC0IPv4”
$NIC0Mask = $ConnectedNICs[0].IPSubnet[0]; log “NIC 0 Subnet Mask: $NIC0Mask”
$Nic0 = Get-WmiObject win32_networkadapter -filter “DeviceId = $NIC0Index”
$NIC0ConnID = $Nic0.NetConnectionID; log “NIC 0 NetConnectionID: $NIC0ConnID”
$NIC1Index = $ConnectedNICs[1].index; log “NIC 1 Index: $NIC1Index”
$NIC1Desc = $ConnectedNICs[1].description; log “NIC 1 Description: $NIC1Desc”
$NIC1IPv4 = $ConnectedNICs[1].IPAddress[0]; log “NIC 1 IPv4: $NIC1IPv4”
$NIC1Mask = $ConnectedNICs[1].IPSubnet[0]; log “NIC 1 Subnet Mask: $NIC1Mask”
$Nic1 = Get-WmiObject win32_networkadapter -filter “DeviceId = $NIC1Index”
$NIC1ConnID = $Nic1.NetConnectionID; log “NIC 1 NetConnectionID: $NIC1ConnID”
# Identify Target NIC and Source NIC
if ($ConnectedNICs[0].IPAddress[0] -match “169.254”) {$TargetNIC = 0} else {$TargetNIC = 1}
$SourceNIC = 1 – $TargetNIC; log “Source NIC: NIC $SourceNIC”
$SourceIP = $ConnectedNICs[$SourceNIC].IPAddress[0]
$SourceMask = $ConnectedNICs[$SourceNIC].IPSubnet[0]
log “Source IP: $SourceIP”
# Setting IP address for Source NIC to DHCP
log “Changing IP address of source NIC to DHCP”
if ($ConnectedNICs[$SourceNIC].EnableDHCP().ReturnValue -eq 0) {
log “==> IP setting change was successful”} else {log “==> IP setting change failed”}
# Need to disable and re-enable the Source NIC for the settings to take effect (!?)
$Nic0.Disable(); Start-Sleep -s 2
$Nic0.Enable(); Start-Sleep -s 2
# Setting IP address for Target NIC to the same values previousely held by the Source NIC
$TargetIP = $ConnectedNICs[$TargetNIC].IPAddress[0]
log “Target IP: $TargetIP”
log “Changing IP address of Target NIC to $SourceIP with subnet mask $SourceMask”
if ($ConnectedNICs[$TargetNIC].EnableStatic($SourceIP,$SourceMask).ReturnValue -eq 0) {
log “==> IP address change was successful”} else {log “==> IP address change failed”}
Remove-Item -Path “HKLM:\SOFTWARE\Wow6432Node\Gridstore\NetworkAdapter”
Start-Sleep -s 2
Start-Service GridStoreManagementService
Invoke-Expression “$env:windir\system32\Notepad.exe $logfile”