$VMLibrary = Join-Path $scriptPath VMCreateLibrary.ps1 . $VMLibrary $errorActionPreference = "Stop" Write-Host "Starting Machine Provisioning..." try { $VMMServerName = $Options.VMMServerName $VMMServer = Get-VMMServer -ComputerName $VMMServerName -SetAsDefault if (!($VMMServer)) { throw "Could not connect to VMM Server $($VMMServer)" } $private:AnswerFile = Get-SCScript -VMMServer $VMMServer | Where-Object {$_.SharePath -eq $Options.AnswerFile} foreach ($private:Machine in $Options.Computers.SelectNodes(".//Computer")) { Write-Host "Starting Machine Provisioning for: $($Machine.Name)" $private:VMTemplateName = $Machine.Template # If the machine already exists, we will skip to the post creation steps. $private:VM = Get-SCVirtualMachine -Name $Machine.Name if ($VM -and ($Options.DeleteExisting -eq $true) -and (!($Machine.Persist -eq $true))) { foreach ($M in $VM) { Power-Off $M Delete-Machine $M } $VM = $null } if (!($VM)) { $private:Cloud = Get-SCCloud -Name $Options.Cloud $private:LogicalNetwork = $Cloud.LogicalNetworks | Select -First 1 $private:BaseTemplate = Get-SCVMTemplate -VMMServer $VMMServerName -All | where {$_.Name -eq $VMTemplateName} $private:OS = $BaseTemplate.OperatingSystem $private:JobGroup = [System.Guid]::NewGuid() $private:hwProfile = Create-HardwareProfile -CPUCount $Machine.Hardware.CPU.Count ` -MinMemoryMB $Machine.Hardware.Memory.Min ` -MaxMemoryMB $Machine.Hardware.Memory.Max ` -Network $LogicalNetwork ` -IncludeDVD ` -JobGroup $JobGroup $private:BuildConfiguration = $Machine.BuildDefinition $private:BuildXML = $Machine.BuildXML $runOnce = @("reg add HKLM\Software\AIS /v `"Build Configuration`" /d `"$BuildConfiguration`"") if ($buildXML) { $runOnce += "reg add HKLM\Software\AIS /v `"Build XML`" /d `"$buildXML`"" } $runOnce += "ROBOCOPY \\MyServer\Share C:\GUIRunOnce /MIR" $runOnce += "cmd.exe /C Powershell Set-ExecutionPolicy unrestricted" $runOnce += "cmd.exe /C Powershell Enable-PSRemoting -Force" $runOnce += "winrm set winrm/config/service @{AllowUnencrypted=`"true`"}" $runOnce += "winrm set winrm/config/service/auth @{Basic=`"true`"}" $runOnce += "winrm set winrm/config/client @{AllowUnencrypted=`"true`"}" $runOnce += "winrm set winrm/config/client @{TrustedHosts=`"localhost`"}" $runOnce += "cmd.exe /C PowerShell -ExecutionPolicy Bypass -file C:\GUIRunOnce\Master.ps1" $private:template = Create-MachineTemplate -TemplateName "Temporary Template$($JobGroup)" ` -BaseTemplate $BaseTemplate ` -HardwareProfile $hwProfile -JobGroup $JobGroup ` -AnswerFile $AnswerFile ` -GUIRunOnceCommands $runOnce ` -MachineConfiguration $Machine if ($template) { $job = Start-Job -ScriptBlock ${function:Create-MachineAsJob} -ArgumentList @($template.Name, ` $Cloud.Name, ` $Machine.OuterXml, ` $JobGroup, ` $VMMServerName, ` $VMLibrary) $jobs += ,$job if ($Options.WaitBetween) { Write-Host "Waiting $($Options.WaitBetween) seconds before continuing to next machine ..." Sleep -Seconds $Options.WaitBetween } } } else { Write-Host "Machine already exists..." } } if ($jobs) { Write-Host "Waiting for Machine Provisioning to Complete..." Wait-Job -Job $jobs } Write-Host "Done" } catch { Write-Host $_ Write-Error $_ } finally { $jobs = $null }