Skip to content

PS-Tip #2 – Windows Remote System Uptime

In this post I’ll cover a Python and PowerShell script I wrote for a tool which had a function of obtaining the uptime of a remote machine.

I’ll start by posting the script, in full, here:

import os
import subprocess

def ps_remote_system_uptime():
    remote_target = raw_input("Which system do you want the uptime for?: ") #obtain target system name

    psfunction = """
function Get-SystemUptime($RemoteSystem) {
$Target = $RemoteSystem
$operatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $Target
"$((Get-Date) - ([Management.ManagementDateTimeConverter]::ToDateTime($operatingSystem.LastBootUpTime)))"
""" #The PowerShell which does the magic!

    psuptime = open("psuptime.ps1", "w") #open file in writable mode
    psuptime.write(psfunction) #write contents
    psuptime.write("Get-SystemUptime -RemoteSystem " + remote_target) #write more contents
    print "File Created, checking system"
    thing = subprocess.Popen("powershell -ExecutionPolicy Unrestricted -File " +
                             shell=True, stdout=subprocess.PIPE)
    days = thing.split('.')
    hours = days[1].split(':')
    print "\nThe system %s has been up for %s days %s hours and %s minutes\n" % \
          (remote_target, days[0], hours[0], hours[1])

So, there it is. It’s a pretty ugly script in this form and was one of the first I learned to write so I encourage any of you learning to script to try to trim this down! I see at least 3 lines which can be consolidated and a large amount of things which can be removed. Not to mention those variable names, talk about lack of clarity.

Either way, don’t criticize the script – I know it would be easier in pure PowerShell and there are different ways to approach it – Instead let’s focus on what I wanted to introduce in this post. ‘Get-WMI-Object’

It’s awesome.

Before covering it, let’s check out what happens in the script:

  1. Call the os and subprocess modules because I’m going to use things from them
  2. Create the function. This will let us call the ‘ps_remote_system_uptime()’ and specify new targets each time!
  3. Ask for the target
  4. We use the WMI object Win32_OperatingSystem to get ‘LastBootUpTime’ which we convert to an uptime value with maths. We won’t look at the PowerShell conversion for now.
  5. Write what we just did to a file called psuptime.ps1 (We have actually created both a PowerShell function (Get-SystemUptime – it doesn’t exist, we made it!) AND a python function (at the start). It’s not particularly special but if you’re new to coding it’s very cool to see how easy it is to integrate the two languages. That being said I would NEVER recommend writing PowerShell scripts inside of python files. Just save them as files elsewhere and call/modify them with python as needed. Don’t store them in Python.
  6. Run the PowerShell Script
  7. Convert the output to a readable date-time format!

So that’s the script but what I really want to emphasize is how easy it was to make the PowerShell call for WMI information.

$Target = $RemoteSystemYouWant
$ThingYouWant = Get-WmiObject $WMIObjectYouWant -ComputerName $Target

WmiObjects are EVERYWHERE and can give you tons of data. Do you want to know drive space available on a system? How about the serial number of the OS or hardware IDs? You can grab your HardDrive serial number and vendor just as easily as we are grabbing the uptime (easier since we don’t need to write a time conversion!). So that’s the takeaway from this. Use Get-WMIObject. Download WMI Explorer: and explore your /root/ and /cimv2/ trees, see all the good stuff you can find!

Leave a comment about what info you’d like to get from your Windows machines. Remember, we don’t need to do this one at a time, we can actually run this against ALL windows computers in an environment AT ONCE. We could even save it to a report or web page for scanning, health checks, inventory, tracking, or whatever else. If you want to see more ways Python and PowerShell can be used together, take a look at my github IT utility project:

Leave a Reply

Your email address will not be published. Required fields are marked *