You are here: Home // Certificari, Cursuri Microsoft Windows Server 2008 R2, Cursuri Servere, Evaluări produse, Home, Microsoft Exchange, Microsoft Technology Associate, Private Cloud certification, SQL Server 2012, Tips and Tricks, Visual Studio, windows 7, Windows 8, Windows Server 2008, Windows Server 2008 R2, Windows Server 2008 R2 Foundation, Windows Server 2012 // Automation for Importing MOC VM’S using PowerShell 3.0

Automation for Importing MOC VM’S using PowerShell 3.0

Importing MOC VM’s on a large number of pc’s is time consuming job.

I will use course 10747 as an example.

1. First we have to unpack the MOC archives.

After using the PowerShell script Unpack10747VMs.ps1 for unpacking the archives I edited the mklink .bat file from the VMs folders and deleted all the “pause” found in each .bat file.

Note: I prefer to run the scripts in PowerShell ISE and see the results and maybe do some modification if needed.

We need to execute the .bat commands that prepare the file junctions for the vhd’s from the Base folder (using mklink).

2. Then we need to create the virtual Networks for the course

I created the virtual switches using VirtNet10747VMs.ps1

3. Then in Hyper-V we use the import wizard to import VM’s.

And we have to run the wizard for each machine.

4. At the end we have to make a snapshot for each VM.

Let us consider a course with 10-16 VM’s and 16 pc’c class.

You can see the amount of work needed to finish the job.

Let’s see what a Powershell script can do for us.

Here is script for importing course 10747 VMs in a Hyper-V on a Windows Server 2012 host.

Imprt10747VMs.ps1

*************************************************************************************

$Course = “10747”

$DriveLetter = “c”

$path = -join ($DriveLetter,”:\Program Files\Microsoft Learning\”,$Course,”\Drives\*”)

get-childitem -path $path -recurse -filter “*.bat” | foreach-object {start-process $ENV:SystemRoot\system32\cmd.exe -argumentlist (‘/c “‘ + $_.FullName + ‘”‘) -windowstyle minimized }

$VMs = Get-Item -Path $path | where-object {($_.Name -match $Course) -and ($_.Mode -eq “d—-“)} | Select-Object Name,FullName

foreach ($VM in $VMs)

{

if(Get-VM $VM.Name -ErrorAction SilentlyContinue )

{ Write-Host $VM.Name “Already imported” }

else

{

$expfile = (Get-Item ($VM.FullName + “\Virtual Machines\*.exp”)).FullName

Import-VM -Path $expfile

Get-VM REA* | Rename-VM -NewName $VM.Name

}

}

*************************************************************************************

The result of the operation is:

Name State CPUUsage(%) MemoryAssigned(M) Uptime Status

—- —– ———– —————– —— ——

10747A-NYC-CAP-B Off 0 0 00:00:00 Operating normally

10747A-NYC-CFG-A Off 0 0 00:00:00 Operating normally

10747A-NYC-CFG-B Off 0 0 00:00:00 Operating normally

10747A-NYC-CFG-C Off 0 0 00:00:00 Operating normally

10747A-NYC-CL1-A Off 0 0 00:00:00 Operating normally

10747A-NYC-CL1-B Off 0 0 00:00:00 Operating normally

10747A-NYC-DC1-A Off 0 0 00:00:00 Operating normally

10747A-NYC-DC1-B Off 0 0 00:00:00 Operating normally

10747A-NYC-DC1-C Off 0 0 00:00:00 Operating normally

10747A-NYC-SVR1-B Off 0 0 00:00:00 Operating normally

10747A-NYC-SVR2-B Off 0 0 00:00:00 Operating normally

10747A-TOR-CL1-A Off 0 0 00:00:00 Operating normally

PS C:\Program Files\Microsoft Learning\10747>

__________________________________________________________________________________

All my thanks to those who helped me with content and ideas.

The script started from an article on the net (http://vniklas.djungeln.se/2012/04/04/importing-vm%C2%B4s-with-powershell-into-win8-hyper-v-3-0/) and also I had some direct help from my friend MCT/MVP Adrian Stoian (http://www.techready.ro)

 

Live is too short, don’t waist it on classroom setups!

Florin Nicolescu

MCT

florin@learningsolution.ro

www.learningsolution.ro

www.cursurimicrosoft.com

Leave a Reply

Time limit is exhausted. Please reload the CAPTCHA.

Copyright © 2009 Cursuri Oficiale Microsoft. All rights reserved.
Designed by Theme Junkie. Powered by WordPress.