Get-Module –ListAvailable VM* | Import-Module
$HostName = "PO-INFR08.panoramaortho.com"
$REFVM = "CAPTURE01"
$RAM = 4
$DISK = 200
$NETWORK = "Bridged"
$DATASTORE = ""
$ISO = "[vmware-isos] MS WorkStation OS\Win 7\LiteTouchPE_x64.iso"
$SMTPServer = ""
$VMSearch = $REFVM
$VMHost = ""
$FROM = "wf_admin@testdomain.com"
$TO = "myemail"
Connect-VIServer $HostName
If ((Get-VM -Name $REFVM -ErrorAction SilentlyContinue).PowerState -eq "PoweredOn") {
Send-MailMessage -SmtpServer $SMTPServer -From $FROM -To $TO `
-Subject "Reference Image creation previous attempt failed" `
-Body "Something went wrong with the last reference image capture process.`
Please connect to $REFVM on $HostName and solve any issues."
Exit
}
Else {
Remove-VM -VM $REFVM -Server $HostName -DeleteFromDisk -Confirm:$false
}
New-VM -Name $REFVM -Datastore $DATASTORE -DiskGB $DISK -DiskStorageFormat Thin -MemoryGB $RAM `
-GuestId windows7_64Guest -NumCpu 2 -VMHost $VMHost
New-CDDrive -VM $REFVM -IsoPath $ISO -StartConnected:$true -Confirm:$false
Get-NetworkAdapter -VM $REFVM | Set-NetworkAdapter -StartConnected:$true -NetworkName "VM Network" -Confirm:$false
Start-VM -VM $REFVM -Confirm:$false
Send-MailMessage -SmtpServer $SMTPServer -From $FROM -To $TO `
-Subject "Reference Image creation has begun" `
-Body "Please monitor for an email in about 2-3 hours to signal that the image has been created."