HOWTO: Color Code Sections for Powershell scripts in PowerGUI

Note: The following was tested with PowerGUI 3.8.0.129.

As I have gotten more confident with Powershell, my scripts have grown longer and longer. I use PowerGUI as my IDE of choice and I have been quite happy with it. One of the problems I’ve increasingly run into however is that as the code grows in length, it becomes harder to visually read and follow the code to pinpoint what you want to work on. I know that Powershell supports the Region/EndRegion commands but that still made it difficult to distinguish one section from another visually. I looked into alternatives but after fairly extensive Google searches was surprised to find on one else commenting on this. I did however find a PowerGUI addon called “Script Colors”. Unfortunately that didn’t offer any configuration for the region command but did discover that this script was just making modifications to an XML configuration file. I then decided I’d see if I couldn’t tackle this myself. Before we delve into how to do this though, let me show you what the end goal is.

The first screenshot below is of the default color scheme in PowerGUI. (Note the code is random gibberish and is there only to fill space for the purposes of this demonstration)

 

Continue reading

Run scripts requiring a password without storing the password in plain text

 

What do you do when you need to provide a password in a batch file or to a legacy command line application but don’t want to store the password in plain text inside of the file? As usual, it’s Powershell to the rescue.

 

The basic steps are:

  1. Create a text file that contains the encrypted version of your password
  2. Read that password into a variable
  3. Convert that password into a Credential Powershell object
  4. Call a method of the credentials object to pull out the plain text password

Continue reading

Whatever it takes

During a recent swim, I happened to overhear a conversation between two other people in the pool that was distinctly Korean. I introduced myself and we ended up having a great chat that was probably 70% Korean. After that encounter, I came to the realization that I’ve learned enough of the language that I’m right on the precipice of basic fluency in Korean. To that end, I decided, to resume my Korean studies. One of the skillsets I wanted to work on was my ability to touch-type in Korean. I’m already reasonably OK at the task and can type in Hangul without looking at the keyboard. But I still make a ton of mistakes which of course frustrates the learning process. I decided the first thing I need to do was to obtain that last 15% and master Korean typing. I then went out searching for a Mavis Beacon style typing tutor for Korean. It turns out there is one that rises above all others called 한메타자교사.

Unfortunately, the application was written in 1999 and as such had two issues, one highly annoying and one show stopping. The first is that the application is hard coded to run at 640×480. The second is that it has a really clever game built in that for some reason doesn’t allow accept Korean input. I suspected this had to do with the fact that I was running the application on an English language Windows base OS with Korean language added as an additional IME. After much troubleshooting and trial and error trying to solve the symptoms, I decided to go for broke and fix the problem at the source.

I checked the build date on the executable and it was written in February of 1999. According to this Wikipedia (http://en.wikipedia.org/wiki/Timeline_of_Microsoft_Windows), the most recent version of Windows available at the time this application was written was Windows 98.

I then scoured the Internet to try to find an ISO for the Korean version of Windows 98. This proved to take a lot longer than I would have otherwise expected. But I did eventually find one and installed KO_Win98 into a virtual machine. (As an aside, holy hell I forgot how long Windows 98 takes to install. Even with the modern CPU, memory and SSD backend, it still took almost half an hour to install and *4* reboots! We truly live in the golden age).

Continue reading

Powershell as a problem solver

Last year I watched an absolutely fantastic two day training session on Powershell now available here:

http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start

Among the many things I learned from that training, one quote from one of the trainers really stuck out with me. Jeffrey Snover, the inventor of Powershell said “Powershell is designed to solve business problems.”

The take away for me from that quote is that Powershell can be an incredible vehicle for building complex automation tools that can be shared with your entire company and beyond. But also, sometimes you have business problems that are much simpler and “one-off” in scope. It turns out Powershell shines brightly here too. I’d like to give you a quick example of how I used Powershell to quickly solve a personal “business” problem in what otherwise would have been very frustrating.

I am starting to revisit the idea of finally learning Korean – enough to be actually properly conversational. With that goal in mind, I found a Korean aptitude test on the Internet. It was 70 questions and increased in difficulty as you worked your way through the list. I thought this was a great place to sink my teeth into the language again. However, I decided that I would much prefer to have these questions inside a “Visual CertExam” or VCE file. This works with an application called the VCE Designer and VCE Manager to present electronic multiple choice exams. “Wouldn’t it be cool if I could somehow import this exam into a VCE file so I could quiz myself? The question is, how could I do that? Let’s start with a sample of what the source material looks like:

Continue reading

HOWTO: Update NetApp DataFabric Manager Hosts without needing to rebaseline

This HOWTO describes how to reconfigure an existing NetApp DataFabric Manager installation with preexisting Snap Vault relationships when the primary hostnames and IPs change and to accomplish all of this without needing to re-baseline.

 

Consider the situations where you have a NetApp Filer that you want to replace with a new Windows file server but you want the Windows server to continue to backup the data via an already existing OSSV relationship.  Or more specifically:

 

Device Type Current Device Name Current IP Address Former Device Name Former IP Address

Existing SnapVault

Windows 2012 VM FSRVDPHSAN1 10.32.0.32 FSRVDPHSAN1A 10.32.112.x (DHCP)

Yes

NetApp FAS2040 FSRVDPHSAN1B 10.32.0.37 FSRVDPHSAN1 10.32.0.32

Yes

 

Before you can do anything inside of DFM, you need to update the Snapvault relationships themselves.  NetApp does a good job of documenting these steps here:

https://kb.netapp.com/support/index?page=content&id=1010282&locale=en_US

 

Unfortunately, I couldn’t find any documentation for the DFM update hence the need for the documentation below.

  • Log into DFM and go to Control Center / Member Details / Physical Systems and select the Physical NetApp Filer
  • On the left hand pane, choose Edit Settings

Continue reading

HOWTO: Rename a VMware virtual machine base files

This HOWTO describes how to rename a VMware virtual machine’s underline files inside the VMFS file system.  It turns out that the name that is displayed in the vSphere Web Client and VI Client is simply a display name.  It is actually attached to an underline UUID that describes that machine.  That UUID is associated with a physical folder and files in the file system.  When you rename a virtual machine from inside the host and even when you rename it from vCenter, these files do not change.  This can lead to future confusion when you find what you think is an old VM and try to delete it.  Following the steps below will ensure that the machine name is consistent throughout the VMware ecosystem.

Important Note: Unless you’re comfortable with the steps below, do not proceed.  This is one of those cases where literally one wrong character can permanently corrupt a VM – or worse.

Continue reading

HOWTO: Add ISCSI Support to Macrium Reflect Recovery Disk

This HOWTO describes how to create a bootable USB recovery CD for Macrium Reflect. This in itself isn’t terribly useful in itself as the Reflect Software offers an excellent GUI based wizard to accomplish this. However, the default Windows PE environment it creates does not include ISCSI support which means if your backups are stored on an ISCSI NAS as mine are, it will not be possible to actually connect to your storage to restore your backups after a disaster. Following this guide will enable you to restore Reflect based backups off ISCSI storage.

  • An important note: When you first run Reflect and a new Windows PE version is available, it’ll prompt you and ask to make a 401MB download from Microsoft. I thought this was the Windows Assessment and Deployment Kit (WADK) for Windows 8.1 but it only includes a subset that doesn’t include the additional packages we need to make ISCSI work. So before you begin this process, download the WADK for Windows 8.1 (http://www.microsoft.com/en-us/download/details.aspx?id=30652)
  • You only need Deployment Tools and Windows Preinstallation Environment (Windows PE)
  • Also, if you already have the Windows 8.0 ADK installed, you will need to manually uninstall it before proceeding to install the Windows 8.1 ADK. In other words, ensure your WADK version matches your Windows PE version

     

     

  • The base Reflect WIM image they already created for us that includes the Reflect recovery software can be found at C:\boot\Macrium\WA5KFiles\media\sources\boot.wim
  • So to make sure we don’t break that, let’s make a copy

    copy “C:\boot\Macrium\WA5KFiles\media\sources\boot.wim” to c:\temp\reflect.wim

 

  • Create folder called c:\mnt that we will use the mount the WIM image and then mount it with the command below

    md c:\mnt

    dism /mount-image /imagefile:c:\temp\reflect.wim /index:1 /mountdir:c:\mnt

Continue reading

vRanger Automated Savepoint Cleanup

In our environment, we have configured vRanger to backup with the equivalent of 2 full backups and 6 incrementals before the earliest backup is overwritten.  This provides some protection against a single full backup becoming corrupted for whatever reason and rendering the entire backup of that virtual machine useless.  However, this configuration does add a significant complication.  Because vRanger requires that a backup be written successfully before purging the oldest one, this means that we requires 3x the space on our backup repository as is used in the production environment.  As most of our VMs are in the 50 to 100GB range, this is acceptable.  However, we have 4 VMs that are at or over 1TB in in size.  This means that we require 4TB * 3 copies = 12TB of disk space on our repository just for these VMs!  This doesn’t even include the incrementals either.  We simply do not have enough space on our backup repository at this time to support this along with all of our other VMs so a compromise was required.

vRanger jobs unfortunately do not allow for any kind of granularity in VM backup configuration so we are unable to adjust for these VMs there.  We could create separate jobs for these VMs but that introduces complications of its own.  The ultimately solution that I worked out as to write a Powershell script to run as a scheduled task each day that automatically deletes the oldest full backup for these VMs provided at least 2 exist.  It seems to work fairly well so I figured I’d include it here should it be useful to anyone else.

 

Continue reading

HOWTO: Compare your weight to famous celebrities

This is going to be one of the more unusual HOWTOs I’m likely ever to produce for this site. It all started earlier today when I asked myself a seemingly simple question:

What is the average weight of famous celebrities that are roughly my height and age?

I was curious because I was wondering how far off the mark I currently was in terms of weight compared to what pop culture considers attractive.
In this day and age, finding the stats on celebrities is frighteningly simple. Aggregating those stats into something meaningful to my question however proved to be more complex.
First and foremost, I’d like to give full credit to the website with which the following information is based on:

http://www.celebheightandweight.com/actor.html

With that out of the way, let’s have a look at their site:

It looks nice enough. The vital statistics of over 400 celebrities are listed here. However, filtering it to get the information I wanted was impossible.
The only option then would be to extract and compile the data myself. Let’s begin shall we?

Continue reading

HOWTO: Ultimate Powershell Home Drive Verification Script

The script below was designed to solve the following problem in my environment.  We have 32 discrete home servers scattered across Canada and thousands of employees that use them.  The problem was appeared to have many times more folders than we did employees.  This turned out to be the result of having an incomplete take down process for terminated employees that ran for an extended period of time.  I therefore needed to write a script to identify all of the folders that were no longer used and could be archived or deleted.

This proved to be far more complex than I first suspected for at least the following reasons:

– Manually providing a list of home servers had the potential to miss entire file servers in a larger environment
– The folder names of the home drives did not always (often) match the username either due to spelling mistakes or varying conventions (firstname.lastname, firstnamelastinitial, lastnameonly, etc)
– The ACLs on the folders may not have been configured correctly and so the user couldn’t access it anyway
– The home drive was not configured in active directory for the user so they couldn’t use the folder even if it existed
– The folder ACL only contained SIDs as the employee account was deleted
– The ACL on the folder only contained the username from an obsolete but still around domain
– The user was terminated but their account was never disabled
– Someone else was given access to a folder and so even though the employee is terminated, we don’t want to remove it
– The user account had been deleted from Active Directory entirely
– The user account was created but the employee never started and so the account was never logged into
– The folder is only accessible to administrators
– The user moved to a new site and their OU was updated but their home drive was not so they are now accessing data over the WAN
– The account is in fact disabled or the account expiry is set
– and still more

Continue reading