Manipulating the registry remotely with alternate Credentials

As an IT pro I frequently need to read and write to registry keys on remote computers, either ad-hoc or via script. Sure I could use Regedit, or RDP to the server in question, but that involves a lot of clicking, and to be honest, moving my right hand to my mouse seems like such hard work ūüôā

I¬† though I’d show you a number of ways of doing this, as well as their limitations, as well as my personal favourite.

Option 1 – Get-ItemProperty

The Powershell cmdlet Get-ItemProperty can be used in conjunction with Invoke-Command to execute a command on a remote computer.

The nice thing about this command is you can also specify alternate credentials. However, it does require that WsMan is correctly configured for powershell remoting to work. Which, 9 times out of 10 in most environments it is not.

Option 2 – The Microsoft.Win32.RegistryKey Class

The Microsoft.Win32.RegistryKey class is another way of accessing registry settings remotely. An example using this method is as follows:

This is more likely to work as it is not reliant on WsMan being configured. However, it does require the RemoteRegistry service to be running on the target computer. Which, by default is not. Also, there is no way to specify alternate credentials, which can present a few problems depending on the computer you are talking to. e.g. non domain-joined machines mounted on the back of a 42″ LED TV mounted 5 meters off the floor… Quite a specific example there.

Option 3 (preferred) – WMI

My final and preferred option is using WMI. They beautiful thing about this method is WMI is typically available (I’m my experience) in most environments, also it accepts alternative credentials and is not reliant on the RemoteRegistry service.

You will be prompted for alternate credentials when running this script, if you wish these can be hardcoded, although I strongly discourage saving passwords as plain text.

You can view all the required methods of the StdRegProv WMI class on MSDN here: https://msdn.microsoft.com/en-us/library/aa393664(v=vs.85).aspx

The only downside to this option was it took me a while to stumble across it!

I hope this is of use , thanks for taking the time to read my blog!

Copying Powershell Output to your Clipboard

Just a real quick tip I’d like to share. When dealing with data I find it really handy to take my PowerShell output and then manipulate this in Excel. Traditionally I’d been using the Export-Csv Cmdlet. While this would work, it would require me to save a CSV file, then open this in Excel. Occasionally this can be a pain when I’m remotely connected to another computer and transferring the CSV file becomes an issue.
The better way!, just use clip.exe. This handy executable will take whatever output you give it and save it to your clipboard. Using it in conjunction with the ConvertTo-CSV Cmdlet, and all you need to do is paste your data straight into Excel. E.g:

 

Kicking the Hedgehog – A case for Automation

Hi there, this is my first of hopefully many blogs. I thought I’d start things off talking about hedgehogs and automation, too things that go hand in hand right?? Don’t worry it will all make sense.

When trying to sell the value of automation in a business one of the key problems I say it can overcome is dealing with complacency.¬†When you have a long,¬†repetitive, mind numbing task to do, after performing it time and time again there is the temptation to cut corners to complete it¬†faster. Because, let’s face it, there are other priorities you would rather focus on. Over time, this may mean skipping out essential steps just to get it over with sooner, you become complacent. This can be when bad things happen!

Time for my anecdote :).

Every night at our house I will the rubbish out to our outside bins. It’s just a short five meter walk outside, through a gate, no big deal. It’s normally dark so my process is:

  1. Find shoes
  2. Put on shoes
  3. Get rubbish
  4. go outside
  5. open gate
  6. put rubbish in bin
Exhibit A: The evil hedgehog
Exhibit A: The evil hedgehog

A simple¬† six step procedure right?, well, after performing this over and over, I started getting complacent. I stopped putting on shoes, as this task would take up most of my time (I have two little kids who like to hide daddy’s shoes!). Awesome!, it’s now a four step process, that’s a great return on investment. So I carried on doing this for a few weeks, until one night…

One rather mild night, I was taking the rubbish through the gate as I do every night, without shoes on, ¬†when all of a sudden I kick something… Initially all I hear is the sound of something skimming down my driveway at pace because I hit it pretty hard. After a few seconds, the pain sets in. Sure enough, I’ve just punted a hedgehog down my driveway! The poor little thing was kind enough to leave half a dozen quills sticking out of my feet.

So, as you can see, complacency can cause bad things to happen. Now, if this procedure was automated, (which would probably require some sort of trash robot) you know it would get it right every time, because computers do exactly what you program them to do, there is no risk of complacency.

In a business environment, the risk of not following business processes correctly can be a lot worse than punting a small mammal. For important repetitive tasks within a business, automation (such as PowerShell Workflows / Azure Automation etc..) can make a big difference to ensure consistent accurate results every time.