Learning PowerShell Failures & Ramblings: Part1 Windows Updates, the Registry, and Compare Object.

Warning his post is just rambling on sharing the fun of learning PowerShell. The tide is finally starting to turn. On one side, my procrastination and limited skills vs. a growing desire and ever-growing knowledge. Time to open a PowerShell script-alike-function I created some time ago. Basically, a function is a script that that can be used like a cmdlet. Instead of running a script by file name, you load a script with a function or group of functions. So, what’s that make a script-alike-function? It is what you create when you don’t know what you are doing. I now know plenty to get myself in trouble. My script-alike-function is named get-hitech. I have dreams of using PowerShell for helping with HIPAA/HITECH in small networks. HITECH has some administrative requirements about tracking user accounts, firewalls/anti-virus/updates must be enabled. My script-alike-function has multiple half-finished information gathering functions and scripts. I plan to cover each info gathering function with a post. My ramblings in this post cover the few lines of code for gathering information on settings for Windows Update, kind-of.

Here’s the little code that was dealing with Windows updates. I think the comment section is the most useful part. The code uses the PowerShell drive HKLM: to access the HKEY_LOCAL_MACHINE in the Windows registry like it was a storage drive. The Get-ChildItem is retrieving (getting) the settings for Windows Updates and stores this info a variable name $UpdateAU. For those who don’t know PowerShell can open the registry just type ” cd hklm: ” into PowerShell.

Here’s the output of this coding gem. At least thanks to my commit section I know what 0 equals.

My first thought is to apply what I learned from Mr. Jones and Mr. Hicks book, “Learn PowerShell ToolMaking…”. That is I’ll use a Hash Table to store data, use the table to create a PSObject, and then output the object. I wasn’t sure what data I had to store in a hash table? Ok, so by piping to format-list. I could see what little my $UpdateAU variable had to work with. For those super newbies’, this is a pipe ||||. And PowerShell has alias for commands, so format-list is fl. 

You got start somewhere, right? So, I created the hash table name $props. Then using something called a “Switch Statement”. Which is just a single statement to replace multiple “IF Statements”.

Here’s the output. Well the switch is working. I’m shocked, doesn’t look I got the “UpdatesEnable” output right. I’m not even sure the 0 is what I think, better test that later by changing my update settings in Windows and running the code again.

Now for my real ramblings… Are there not more settings in Windows 10? Will this code work in Win7 and win10? Does anyone even care about 8 and 8.1?  And how to do I get the updates enable to show true or false? The screen setting from my Windows 10 Developers Edition for sure has more options then I have in PowerShell. Where are these options stored in the registry? Well I could search the web, but that’s not as fun as powershell.

So how about that whole Compare-Object thing PowerShell supports? Could I save the registry as a file, make the changes in the GUI, then save the registry again, and finally compare the two files? Right off the bat, I made a few mistakes. First, I did not read the Help Compare-Object -details! Second, I failed to use the -Recurse parameter with get-child item. Third possible mistake I used JSON Files. Only because I have been playing with the JSON file Chrome stores its bookmarks. Would CliXML files be better? Notice I also forgot to provide a required parameter, so PowerShell prompted me.

I have never used Compare-Object. Why would I need files to compare? Maybe I just need two variables to compare, as shown by the Help system in PowerShell. Seems like a variable is more object like, right? 

Off to a great start, huge error message. Why is access not allowed? Doesn’t look like a PowerShell problem. Looks like a permission, access control list, file in-use??? Mr. Don Jones is right PowerShell is a great way to learn or re-enforce your knowledge on all types of stuff. You just have fun trying and learning. 

I now turn to the Internet and search for “user access to HKLM”. I find something about assigning permissions with the Windows Registry Editor (RegEdit). 

Wait a minute, why don’t I just try to export the registry as a file from RegEdit. Because I do have some rules about manually modifying the registry; only on a test VM and/or only on someone else’s machine.

Here we go, let’s get some content from a file and save it as a variable. … and PowerShell is now using 6GB of RAM! Better save this post just encase Windows goes crazy. Thinking maybe export the whole registry may have been a little much. Ctrl-C Ctrl-C, Ctrl-C please PowerShell stop. Well I’m just going to close PowerShell now.


OK, this time I’ll just export polices and not the whole registry. Umm, 28K vs. 477MB. Get-Content did seem to work, or there’s no errors.

Either I did something wrong (not me) or there is nothing different about my two objects. Not sure I’m going to find what I’m looking for with just a small amount of the registry. But the HKLM\SOFTWARE IS 358MB


Now what? I not sure compare-object would work for finding new registry links or not. You know the best think about your own rules, you can break them. I exported the registry setting for Paint.Net. I manually added a test entry and exported another file. Bam, there’s the difference. I’m going to have to play with the registry and compare-object on my much faster workstation. Maybe I should just compare the two save exported registry files, go back to using JSON? Again that’s the fun of PowerShell. 


Took about 10secs to manually find these settings in Regedit. There’s most of the other Windows Update settings. Better add that to my get-hitech script-alike-function so I have them.

That will do for my first IT post ever.

My goals for these failures and rambling posts are to help people realize you don’t have to be a genius to learn PowerShell. Most importantly it’s not just about PowerShell, Learning and failing with PowerShell will reinforce your knowledge on other subjects. I guess there will have to be a Part1A just to finish my function for gathering information on Windows Updates. As a side note, Mr. Don Jones is right about PowerShell. It is just one of many tools out there. There are likely much better tools for dealing with HITECH in small offices: Microsoft Operations Management, Group-Policy Reports, In-tune, Pulseway, Kaseya, Server-Essential Features, just to think of a few. Anyways I better stop rambling.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s