# CreateAndPresent-Iso.ps1 (cap-iso.ps1) # Author: Ben Pearce # Date: 6th July 2007 # This script is provided "AS IS" with no warranties, and confers no rights # This script gathers all files in a directory to an ISO, and presents it to a VM. # This script assigns the ISO to every DVD drive. If you have more than 1 you need to modify the script to take account of this. # You must specify the VMName parameter. All others are optional and the default values are below # Usage: The only required parameter is VMName. If no other parameters are specified the script will create an iso with random name comprising files from the default source directory. param ($Source, $Dest, $ISOName, $VMName) function setsecurity($Object) # This function sets the appropriate security on any Virtual Server object { $result = [Microsoft.VirtualServer.Interop.PowerShell]::SetSecurity($Object) } # Check the input parameters, and assign default values if empty if ($Source -eq $null) {$Source = "C:\store\tools\makeiso\source"} if ($Dest-eq $null) {$Dest = "C:\store\tools\makeiso\dest"} if ($ISOName -eq $null) {$Random = new-object System.Random; $ISOName = "Iso" + $Random.Next(1000) + ".iso"} if ($VMName -eq $null) {"You must specify a Virtual Machine Name"; exit} write-host "`n**** Creating ISO ****" #Create the ISO C:\store\tools\makeiso\OSCDIMG.EXE -m "-bC:\store\tools\makeiso\Etfsboot.com" $Source -n -l$ISOName "$Dest\$ISOName" write-host "`n**** ISO Complete ****" $error.clear() $profdir = #Load VS Assembly $result = [System.Reflection.Assembly]::LoadFrom(“$profdir\VSWrapperForPSH.dll”) #Create VS Object $vs = new-object -com VirtualServer.Application -Strict if ($error.count -gt 0){"'nCould not connect to Virtual Server"; $error[0].exception.message; exit} #Set appropriate Security on Virtual Server $result = SetSecurity $VS #connect to VM $VM = $VS.findvirtualmachine($VMName) if ($VM -eq $null){"`nERROR`nCould not connect to Virtual Machine $VMName`n";exit} #Set the VM security $result = SetSecurity $VM "`nConnected to VM:" + $VM.Name #Get DVD Drives $DVDDrives = $VM.DVDROMDrives setsecurity $DVDDrives if ($DVDDrives.count -eq 0){"`nERROR`nThere is no DVD attached to this VM`nCould not attach ISO`n";exit} if ($DVDDrives.Count -gt 0) { for ($i=1;$i -le $DVDDrives.Count;$i++) { $DVD = $DVDDrives.item($i) $Result = setsecurity $DVD switch ($DVD.Attachment) { 0 {} 1 {$DVD.ReleaseImage()} 2 {$DVD.ReleaseHostDrive()} default {} } $DVD.AttachImage("$Dest\$IsoName") "Attached image $Dest\$IsoName to " +$VM.Name +"`n" } }