Need to work out the IP address and MAC addresses of your Hyper-V VMs in SCVMM?

If you have been trying to get information out of VMM using the ‘traditional’ Get-VM commandlet you may have noticed it throws an error similar to this:

get-vm : The cmdlet cannot find a specified class. Verify that the relevant feature is enabled on the operating system.

Now this threw me off as being an incorrect command. What I found is that you need to specify the VMM server name.

If you just want a list of all the VMs, you can run the following:

I also noticed that if you run get-vm by itself in the PowerShell (don’t forget to run Import-Module -Name “virtualmachinemanager”) it does not need the -VMMServer to be specified.

Anyhoo – once you have the command to spit out all the VMs in your environment, its a simple pipe to select the information you need:

Your output should look something like this:

VMMVMsIPSimple as that!

Read More

Change Default Windows Audio Device via Command Line

I just had to share this!

I use 3 different audio devices (Speakers, Stereo Headset and a 7.1 Headset) for various different applications. While Windows is now relatively smart about how it sets the default device when detected, it still requires removing / adding the device.

I thought to myself, wouldn’t it be great if I could just create a script that I could execute before running certain applications that would allow me to set the audio device automatically. Most search results provided links to third party macro / hotkey applications which was not what I was looking for. Looking deeper I came across a gem of an application, AudioEndPointController by Dan Stevens.

This application lets you list the audio output devices attached to the computer and can also set the default output. Using shortcuts to call the executable, I can parse the audio device number of my choosing, and the default device flips, simple as that. Chrome plays well and any audio playing (Youtube etc) will immediately switch over. Firefox (for me) requires a restart.

I Googled for images of my headphones / speakers and converted them to .ico files so I could have graphical shortcuts on the start bar. I used http://www.convertico.com/ to convert them online. Works great!

Compulsory – yet tiny image:

Audio

Read More

Hyper-V Getting MAC Address using VM Name

As part of my automated provisioning (yes – System Center and Sysprep will probably be a better option, but hey whatever) script, I need to be able to automatically assign an IP address to a VM. At this point, the only way I can see this being a possibility is to mount the VHD before booting then copying a script that will set the correct IP address to an autostart location.

The ‘script’ is broken down into two parts. The first needs to run on the node, and create the second script inside the VHD. The second script then needs to identify what network adapter/s are present and assign the correct IP address to each.

The first challenge is finding out how to have the second script identify the correct adapter. I initially though about testing connectivity after the IP address was set, and upon a failure, revert the changes and try the next adapter present. Rather than doing this, I found you can get the MAC address for a VM from the Hyper-V node, and filter based on which Virtual Switch the interface is attached to:

The following example will get the MAC address for a VM based on the VM name, and the network it is attached to:

This returns a MAC address.

I believe I can then use this MAC address in the second part of the script to pull the network adapter name.

Something along the lines of this:

Once I have the correct adapter name, I can use this in an IP address setting script to set the correct IP / gateway etc essentially automating the networking side of provisioning.

At this point there are a few things I have yet to confirm that might make the above attempt futile. First, I do not know if I can run a powershell script on startup – I know there are issues with signed code etc, and this might mean a freshly provisioned system will not allow unsigned code to run – especially if it needs administrator permissions. Second – I don’t know how this will work with a sysprepped image. It is possible (probable?) that with a sysprep image, the network configuration information could be copied to the sysprep config eliminating the need for an autorun script.

You can see an example of a powershell script to assign IP addresses here: http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/040b8993-d737-4436-8fb1-29187583e7d1/

Read More

Copy to mounted VHD with PowerShell for automated Hyper-V Provisioning – Part II

While I initially tried to get the Powershell Mount-VHD cmdlet working – I gave up as it didn’t seem to exist (despite obvious documentation: http://technet.microsoft.com/en-us/library/hh848551.aspx)

I was using a Server 2012 DS for testing purposes, but as I was just working on the drive letter side of things – I had yet to install the Hyper-V role. Turns out that Mount-VHD is exactly what I need to get the driveletter of a VHD and requires Hyper-V to be installed. The last example on the Microsoft Site gives you all you need to know:

With a little modification we can ensure that the System Reserved partition does not get in the way:

This is what you will get after mounting using the above command:

MountedVHD

You can then select the driveletter column to get what you need:

Much more simple than my previous attempt!

Enjoy.

Read More

Copy to mounted VHD with PowerShell for automated Hyper-V Provisioning

I have been working on an automated provisioning script for Hyper-V and have been annoyed that there is no way to get the associated drive letter for a mounted VHD based on the VHD path. I have read a few posts from around the net for older versions of PowerShell but on Server 2012 they do not seem to work.

At this point, I have given up trying to find a way to get a unique drive letter, and instead just get the drive letters for any attached VHD. The script I am working on will only continue if there is only one VHD attached. This is a requirement as if there were multiple VHDs attached, I do not know how I can identify which drive letter is assigned to which VHD.

The goal is to have the VHD mounted, and then have a BAT script generated containing IP address configuration information, which, once unmounted and used in my main provisioning script will allow the user to ‘automatically’ set the VM IP address. The BAT (Or PowerShell – haven’t worked out if the security settings will allow for a PowerShell script to run on start-up) script will then run on the first boot of the VM and set the correct IP address, gateway and so on.

 

http://www.nexco.com.my/images/wh2012.png

 

In the example below (which I will be updating from time to time) I have created a script that will first ensure that there are no more than one VHDs mounted. It will then create/check a lockfile before continuing. If the lockfile is unlocked, it will echo “Copying to VHD”. At this point it is just an echo of text and continue. I have yet to work out the BAT format for setting the IP correctly.

I will be aiming to have the BAT file configured before it hits the copy stage. This will probably just be another function that creates the file. Then in the copyToVhd function, it will mount the associated VHD, copy the BAT file to a start-up folder (or something of the sort) and then unmount the VHD. All with a fairly decent level of protection against having the copyToVhd function being run by different people at the exact same time (which would make it impossible to copy the BAT file to the correct VHD)

When I have some more time to work on it, Ill probably be more flexible with the first check and have it as a while loop to allow for cases where the VHD may not have been detached from another script in progress.

Anyway, this is what I have so far: *Forgive my total and utter messiness – I have no formal PowerShell training, and have only been playing with it for a week!

 

If anyone has any idea on how to get a drive letter based on the mounted VHD I would be super happy! This would mean the lock file wouldn’t be necessary, as I could identify which drive letter to copy to, and unmount based on that also.

Read More