How to automate the installation of a MOC course from A to Z using PowerShell 3.0

We will use as an example the MOC course 10747.

A. Extract VM’s from downloaded MOC archives

 

As explained in my post How to automatically unpack MOC and Base VHDs archives using PowerShell we can use PowerShell to expand the archives.

On the trainer pc we expand the MOC files and then copy the result to the student pc’s

The command Unpack_10747.ps1 will look like this:

#####################################################################################

# Unpack MOC V1b

# Automatically unpack(unrar) MOC downloaded course files and additional Base VHD’s from a list of folders

# You NEED TO EDIT this file to introduce the list of folders containing the downloaded archives ($DownloadedPath and in paragraph – $list… @{source …)

# Destination folders are set to the MOC default location

# You can separately customize location of the MOC files and of the Base VHD’s ( $DestDriveLetter1 + $DestPath2 ; $DestDriveLetter2 + $DestPath3)

# Used variables:

# $Course = Number of the course (no version included – ex:10747 and not 10747A or 10747B, etc)

# $SourceDriveLetter1 = Volume where downloaded archives are located

# $DestDriveLetter1 = Volume for Course VM’s

# $DestDriveLetter2 = Volume for Base VHD’s

# $list = manually edited(updated) list of folders containing archived folders to be unpacked

# $DestPath1 = Default root folder for MOC course and BASE folders

# $DestPath2 = Destination folder where the unpacked MOC files will be located

# $DestPath3 = Destination folder where the unpacked Base VHD’s files will be located

# $DestPath4 = Destination folder where the unpacked Mid-Tier Base VHD’s files will be located

# $UnrarPath = Location of the unrar.exe file – need to be downloaded

# Download unrarw32.exe from http://www.rarlab.com/rar/unrarw32.exe and extract unrar.exe to $UnrarPath

####################

$Course = “10747”

$DestDriveLetter1 = “c”

$DestDriveLetter2 = “c”

$SourceDriveLetter1 = “e”

$DestPath1 = -join ($DestDriveLetter1,”:\Program Files\Microsoft Learning”)

$DestPath2 = -join ($DestDriveLetter1,”:\Program Files\Microsoft Learning\”,$Course,”\Drives”)

$DestPath3 = -join ($DestDriveLetter2,”:\Program Files\Microsoft Learning\Base”)

$DestPath4 = -join ($DestPath3,”\Drives”)

$UnrarPath = -join ($DestDriveLetter1,”:\Program Files\Microsoft Learning”)

# START EDIT List (the list of folders containing the archives)

$list = @(

# Start EDIT (the list of folders containing the archives)

@{source = -join ($SourceDriveLetter1,”:\MOC Downloads\Base\MT12\MT12-CM-NYC-DC1″); dest = $DestPath4},

@{source = -join ($SourceDriveLetter1,”:\MOC Downloads\Base\MT12\MT12-CM-NYC-CFG”); dest = “$DestPath4″},

@{source = -join ($SourceDriveLetter1,”:\MOC Downloads\Base\MT12\MT12-CM-NYC-SVR1″); dest = “$DestPath4″},

@{source = -join ($SourceDriveLetter1,”:\MOC Downloads\Base\MT12\MT12-CM-NYC-SVR2″0; dest = “$DestPath4″},

@{source = -join ($SourceDriveLetter1,”:\MOC Downloads\Base\Base10C-W7-HV”); dest = “$DestPath3″},

@{source = -join ($SourceDriveLetter1,”:\MOC Downloads\Base\Base11A-WS08R2SP1″); dest = “$DestPath3″},

@{source = -join ($SourceDriveLetter1,”:\MOC Downloads\MOC_ConfigMgr2012\10747A- Part 2 – Virtual Machines – Administering System Center 2012 Configuration Manager”); dest = $DestPath2}

# End EDIT List (the list of folders containing the archives)

)

$list | % {

Write-Host “”

$source= $_.source

$dest = $_.dest

if(!(Test-Path $dest)) {

mkdir $dest

}

Write-Host “Source: $($source)”

Write-Host ” Dest: $($dest)”

Set-Location $dest

Get-ChildItem -Path $source -Filter “*.exe” | % {

Write-Host “`t$([DateTime]::Now)”

Write-Host “`t$($_.FullName)”

Start-Process -Wait “$UnrarPath\unrar.exe” -ArgumentList (“x”, “-o+”, “-y”, “””$($_.FullName)”””) -RedirectStandardOutput (Join-Path $DestPath2 ($_.Name + “.log”)) -windowstyle minimized }

Write-Host “`t$([DateTime]::Now)”

}

#####################################################################################

One advantage of the script is that if you have a base VM folder and you forgotten to mark as comment the line comment that is expanding that VM, the script will overwrite it without blocking in waiting for user input.

The only penalty will be some extra time to finish the command, but you can be sure that you have all the dependency needed.

B. Copy files to student pc’s

We need to copy the files to the student pc’s.

I use Explorer interface from Server 2012 to perform the copy operation.

The copy operation done in explorer finished in only 15 min for the 90 GB of the MOC 10747A (including MOC files and related Base vhd’s files and considering the destination folder empty)

All source and destination pc’s are linked on a gigabit network and the OS is Windows Server 2012.

To minimize time we can do copying operations in parallel.

If we have pc1, pc2, ….. pc16 and pc1 is the source we can perform the following steps:

a) Copy pc1 to pc2.

b) After previous step ends start in parallel:

copy pc1 to pc3 and pc2 to pc4.

c) After previous step ends start in parallel:

copy pc1 to pc5, pc2 to pc6, pc3 to pc7 and pc4 to pc4.

d) After previous step ends start in parallel:

copy pc1 to pc9, pc2 to pc10, pc3 to pc11, pc4 to pc12, copy pc5 to pc13, pc6 to pc14, pc7 to pc15, pc8 to b.

With a 15 min by step the copy operation can be done in 1h-1.5h which is a very good time.

You need an alarm clock to tell you that the time to next step has come.

____________________________________________________________________________________

I tried the robocopy command that is now embedded in the Windows 2012 server but the finalizing time was very disappointing.

Just for your info the command used to copy from pc1 to pc2 was:

Robocopy “C:\Program Files\Microsoft Learning” “\\192.168.0.196\c$\Program Files\Microsoft Learning” /mir /MT:64 /Z /W:5 /xjf

/mir -Mirrors a directory tree (equivalent to /e plus /purge)

/xjf – Excludes junction points for files

/Z – Copy files in restartable mode

/MT – Number of multiple threads: 8 is default

/W – Wait time between retries: default is 30 seconds.

Probably I am missing something or the tool is not using the SMB3.0 support.

I quitted the operation after 2 hours as it did not finished copying to 90 GB pool of data.

Same operation was finished in 15 min using Explorer copying.

____________________________________________________________________________________

C. Create virtual switches needed for the current MOC

After we copied the result to the student pc’s we need to prepare the MOC VM’s import.

First we need to delete the VM’s from the previous course installed on that pc.

We use PowerShell.

get-vm |Remove-VM -force

First part select all installed VM’s and the second delete them without any user intervention.

We need to manually delete the previous MOC course folder from c:\Program Files\Microsoft Learning.

Then we prepare the virtual networks needed for the new course.

You can find detail at How to create and manage Virtual Networks for a MOC course on a Windows Server 2012 Hyper-V using PowerShell

First we delete al existing virtual networks:

Get-VMSwitch | Remove-VMSwitch -force

Create new virtual switches (ex: Private Network and Private Network 2)

New-VMSwitch -Name “Private Network” -SwitchType Private

D. Run mklink .bat files commands

 

You can find details about importing MOC VM’s at Automation for Importing MOC VM’S using PowerShell 3.0

We need to run the mklink commands to create the file junction for the Base vhd’s.

The mklink command can be a single one commands in the root of the course files or a set of distributed commands that can be found in each VM folder.

If the .bat file contains pause commands we need to erase them.

They are useful when we run the files manually and want to see the results before the command window is closed.

When we are going to run them in a .ps1 script we do not want it to stop for user intervention after each .bat is executed.

E. Import the VM’s for the new Course

Now we can import the VMs for the current course.

F. Create Initial Snapshots for each VM

At the end we make the initial snapshot for each machine.

A cumulated PowerShell script for course 10747 will look like this:

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

# Remove ALL VMs from previous course

# Need to manually delete previous course folder

# Uncomment needed commands

# Remove ALL VMs – no end user validation

get-vm |Remove-VM -force

# Remove ALL VMs -with end user validation

#get-vm |Remove-VM

#Delete ALL virtual networks(switches)

# Uncomment needed commands

#Delete ALL virtual networks(switches) – no end user validation

Get-VMSwitch | Remove-VMSwitch -force

#Delete ALL virtual networks(switches) – with end user validation

#Get-VMSwitch | Remove-VMSwitch

#Create virtual networks(switches) needed for the course

# Uncomment needed commands

# Create private network(switch) named: Private Network

New-VMSwitch -Name “Private Network” -SwitchType Private

# Create private network(switch) named: Private Network 2

#New-VMSwitch -Name “Private Network 2” -SwitchType Private

# Create internal network(switch) named: Internal Network

#New-VMSwitch -Name “Internal Network” -SwitchType Internal

# Create internal network(switch) named: Internal Network 2

#New-VMSwitch -Name “Internal Network 2” -SwitchType Internal

# Create internal network(switch) named: External Network

#Get-NetAdapter -Physical | New-VMSwitch -Name “External Network” -AllowManagementOS $True

# Import VMs for current course

$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

}

}

# Create snapshots for ALL VMs, each snapshot will be named: Initial point

get-vm | Checkpoint-VM -SnapshotName “Initial point”

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

 

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

 

Florin Nicolescu

MCT

florin@learningsolution.ro

www.learningsolution.ro

www.cursurimicrosoft.com

1 thought on “How to automate the installation of a MOC course from A to Z using PowerShell 3.0

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.