Adding tab-completion to your PowerShell Functions

January 13, 2017 FoxDeploy

Probably my single favorite feature of PowerShell isn't exciting to most people...but I love Auto-Completion.  I have my reasons: As I have the typing skills of a preying mantis (why did I mention them...they're easily the creepiest and worst insect...ewww) and constantly typo everything, I LOVE auto-completion. Continue Reading...

Tool-highlight: Show Windows Toast Messages with PowerShell

January 04, 2017 FoxDeploy

This will be a quick post here, but I just wanted to shine a spotlight on an AWESOME tool that I absolutely love: Joshua King's 'BurntToast' PowerShell module, which makes the arduous task of rendering a Windows Toast notification VERY Easy. Continue Reading...

Registering for WMI Events in PowerShell

December 16, 2016 FoxDeploy

registering-for-wmi-events

An alternate title might be ‘Running PowerShell Code ONLY when the power state changes’, because that was the very interesting task I received from my customer this week.

It was honestly too cool of a StackOverflow answer NOT to share, so here it goes, you can vote for it here if you thought it was worth-while.

If you want your code to trigger only when the System Power State changes, as described here, use this code.


Register-WMIEvent -query "Select \* From Win32\_PowerManagementEvent" \` -sourceIdentifier "Power" \` -action { #YourCodeHere } 

Now, this will trigger whenever the power state changes, whether you plug the device in, OR unplug it. So you might further want to stop and pause to ask the question:

Am I on power or not?

Fortunately we can use the WMI Class Win32_BatteryStatus to detect if we’re charging or not, so here’s the full construct that I use to ONLY run an operation when a power event changes, and then only if I’m no longer on Power.

Locking the workstation when the system is unplugged

Register-WMIEvent -query "Select * From Win32_PowerManagementEvent" `
  -sourceIdentifier "Power" `
  -action {
      if ([BOOL](Get-WmiObject -Class BatteryStatus -Namespace root\wmi).PowerOnLine ){
         #Device is plugged in now, do this action
         write-host "Power on!"
     }
    else{
        #Device is NOT plugged in now, do this action
        write-host "Now on battery, locking..."
        [NativeMethods]::LockWorkStation()
     }

If you’re curious how this looks in real time

Registering for device events

It can also be useful to have your code wait for something to happen with devices, such as running an action when a device is added or removed. To do this, use this code.

#Register for power state change
#Where TargetInstance ISA 'Win32_Process'"
Register-WMIEvent -query "Select * From Win32_DeviceChangeEvent where EventType = '2'" `
-sourceIdentifier "Power" `
-action {#Do Something when a device is added
Write-host "Device added at $(Get-date)"
} 

You might also want to do an action if a device is removed instead, so use this table to choose which event is right for you. Read more about it here.

EventType Id
ConfigurationChanged 1
Device Arrived 2
Device Removed 3
Device Docked 4

What else can I wait for?

Not only these, but you can trigger your code to execute on a variety of useful WMI Events, all of which can be seen in this image below!

ClassNameTriggers when
Win32_DeviceChangeEvent A device is installed, removed, or deleted, or the system is docked
Win32_VolumeChangeEventSomething happens to your disk drives
Win32_PowerManagementEventYour device is plugged, unplugged or docked
Win32_ComputerSystemEventSomething major happens to the system
Win32_ComputerShutdownEventThe system is shutting down!
RegistryEventAnythign happens to the registry
RegistryKeyChangeEventA reg key you specify is changed
RegistryValueChangeEventA reg value you specify is changed
Continue Reading...

Locking your Workstation with PowerShell

December 15, 2016 FoxDeploy

Locking a workstation using PowerShell?  It sounds like an easy task, right?  That's what I thought too...and told the customer...but NO!  Friends, it wasn't easy...before now. As it turns out, some tasks in Windows just aren't accessible via WMI.  For instance, the useful Win32 OperatingSystem class has some nifty methods for working with the system's power state, like Reboot and Continue Reading...

Hands-off deployments

December 12, 2016 FoxDeploy

Let's face it, guys.  There are times that you JUST don't have access to SCCM, MDT or Kace, and need to deploy a completely automated and silent Windows install without our normal build tools.  If this is you, and you deploy systems frequently, you've probably spent way too much time looking at screens like this one Continue Reading...

Class is in Session: PowerShell Classes

November 22, 2016 FoxDeploy

PowerShell has been mostly complete for you and me, the ops guys, for a while now. But for Developers, important features were missing. One of those features were Classes, a important development concept which probably feels a bit foreign to a lot of my readers. For me, I'd been struggling with classes for a long time. Ever since Middle School. #DadJokeLol. In this post, I'll cover my own journey from WhatIsGoingOnDog to 'Huh, I might have something that resembles a clue now'. Continue Reading...

Microsoft MVP

Five time Microsoft MVP, and now I work for the mothership


Need Help?

Get help much faster on our new dedicated Subreddit!

depicts a crowd of people in a night club with colored lights and says 'join the foxdeploy subrreddit today'


Blog Series
series_sml_IntroToDsc
series_sml_PowerShellGUI series_sml_IntroToRaspberryPi Programming series_sml_IntroToWindows Remote Management Series The Logo for System Center Configuration Manager is displayed here Depicts a road sign saying 'Learning PowerShell Autocomplete'




Blog Stats