VM Lab – Switch it up

Back again for another part in my VM Lab series.

So far I’ve created the folder structure which we will use to store our ISO files, VHD files [including template VHDs and Unattend.xml files] and VM’s.
In order to manipulate the ReadOnly status of various files [such as the template VHD’s and Unattend.xml files], I’ve provided simple functions which will assist us.

Now we need to make sure that the final piece of the infrastructure is ready: the Hyper-V VM Switches.

Breakdown

As a change of scenery, this time I won’t just dump the code, I’ll try and explain my reasoning behind the code used.
Since the 3 functions [again going for Get- , New- and Remove-] all use the same base structure, I thought it would be nice to explain some method to my madness.
If this isn’t what you’ve come here for, please scroll down to the end of the page and just scoop the final code 🙂

Advanced functions – ValidateSet

There are only select options to choose when creating a VM Switch: External, Internal, Private (and in some versions NAT, but this is not included in my function for now).
In order to limit the user’s choices, I’ve used something called ValidateSet, which gives the end user a predefined list of options to choose from.
Should you manually enter a value which is not defined in the ValidateSet list, access will be denied.

You can find various examples in common Cmdlets, such as

This will give you a predefined set of options to choose from, such as AllSigned, ByPass, Default, RemoteSigned, Restricted, Undefined and Unrestricted.
In my code I limit the user to the default VM Switch types, but I also provide an option to select all 3 types at the same time, All.

This does however require your function to be an advanced function, as defined by

In case you didn’t know yet, adding something as simple as that will provide your function with additional features and functionality, such as being able to use CommonParameters, such as –Verbose or -WhatIf.

For a more in depth view of Standard functions vs Advanced functions, I’d like to refer to these great articles by the following great minds:

Switch statement

Now these are functions about VM Switches, but in my code I also use something called a switch statement.
This allows me to set/change values, based on what value was chosen in my ValidateSet.
There is a great article on Microsoft’s TechNet by The Scripting Guys, which will explain the entire concept better than I can do, so please have a look at that if anything is unclear about the options/usage of this statement.

In my case, this is the logic I used behind it:

At first I have defined all types of VM Switches as a variable

Now through the switch statement, I check which $SwitchType has been selected and I change the accompanying variable to $true

If statement

After the variables are set to either $true or $false using the above mentioned switch statement, I use an if statement which will execute certain code if the value is set to $true.
If the variable is set to $false,  you can have it do something else, of elseif, but for this code it wasn’t important.

By doing this, I can check each VM SwitchType, and if it was set as $true, I would either get, create or remove the VM Switch.
In the above example I also use a try/catch block, but I will try and explain this in a future blog post.

Functions

As per usual, I’ve provided the final code below.

BEWARE

These functions are meant for a test environment.
I’ve borrowed Jeffery Hicks‘ disclaimer, because it simply says it all:

DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED THOROUGHLY IN A LAB ENVIRONMENT.
USE AT YOUR OWN RISK.
IF YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.

I’ve set the scripts to a max height as to not flood the page, but rest assured, everything’s there.
Please do note that functions might refer to other functions, this is intended as a complete toolset, not just 1 piece.

If you have questions on the techniques used or suggestions on how to improve something, please let me know in the comment section!

Get-LabSwitch

New-LabSwitch

Remove-LabSwitch

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail

VM Lab – Read Only status

In this next installment of my  VM Lab series of posts, I have created a tiny set of tools which basically do exactly as the names imply.
While I admit, not quite hard to do, perhaps you’d even prefer to type things manually, but as mentioned in my previous post, for me it’s all about re-usability.

Requirements

In one of the upcoming installments of my VM Lab series, I will create a template VHD file, which will need to be set to Read Only in order to prevent changes to be made accidentally.
In another, I will need to copy a current Unattend.xml file, check if it was set to Read Only and if it was, remove that value so we can customize the contents.

So I made a small set of tools that did just that.

Functions

As the scripts aren’t too complex, but do server a solid purpose for the rest of the series, I’ve provided the code below.

BEWARE

These functions are meant for a test environment.
I’ve borrowed Jeffery Hicks‘ disclaimer, because it simply says it all:

DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED THOROUGHLY IN A LAB ENVIRONMENT.
USE AT YOUR OWN RISK.
IF YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.

I’ve set the scripts to a max height as to not flood the page, but rest assured, everything’s there.
Please do note that functions might refer to other functions, this is intended as a complete toolset, not just 1 piece.

If you have questions on the techniques used or suggestions on how to improve something, please let me know in the comment section!

Get-ReadOnly

Set-ReadOnly

Remove-ReadOnly

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail

VM Lab – Lab Folders

Lets get started…

First of all, I always prefer my lab to have the same layout and tend to use the same solutions, since they’re either best practice or the most logical approach for me.
Now in a Lab environment, this starts at the basics, creating a folder structure to put everything in, from VHDs, VMs to ISO files and templates.

Requirements

For my Lab I always want it to have the same folder structure.

This structure is as follows:

  • $Root\ISO
  • $Root\VHD
  • $Root\VHD\Template
  • $Root\VM

Structure

Now I wanted to have the following “solutions” for this requirement:

  • Get-LabFolder
    To see if the current folder [including all sub folders] actually already exists.
    The most annoying bit was to check if all the sub folders actually existed, which I bet can be done more elegantly, but I’ll need to check that at a later point.
  • New-LabFolder
    Create a new set of Lab folders in the provided Root.
  • Remove-LabFolder
    Take a wild guess 😉
    Mostly for my testing purposes, since I’ve not yet done Pester tests for all my scripts.

While it seems very basic, making tiny building blocks out of every solution, makes it so I can re-use these solutions at a later point.
This isn’t a “hard” solution, but for me the most important bit was getting the structure done right, making sure everything has a proper help function and is somewhat well documented/scripted.

In the process I also used a handy trick for a confirmation pop-up on the deletion, to let someone confirm they want to remove the folder structure, because gone = gone.

Functions!!

Here’s what most people come for.

BEWARE

These functions are meant for a test environment.
I’ve borrowed Jeffery Hicks‘ disclaimer, because it simply says it all:

DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED THOROUGHLY IN A LAB ENVIRONMENT.
USE AT YOUR OWN RISK.
IF YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.

I’ve set the scripts to a max height as to not flood the page, but rest assured, everything’s there.
Please do note that functions might refer to other functions, this is intended as a complete toolset, not just 1 piece.

If you have questions on the techniques used or suggestions on how to improve something, please let me know in the comment section!

Get-LabFolder

New-LabFolder

Remove-LabFolder

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail