Sunteți pe pagina 1din 6

$csvfile ="C:\Template\template.

csv"
$vmsnapin = Get-PSSnapin VMware.VimAutomation.Core -ErrorAction
SilentlyContinue
$Error.Clear()
if ($vmsnapin -eq $null)
{
Add-PSSnapin VMware.VimAutomation.Core
if ($error.Count -eq 0)
{
write-host "PowerCLI VimAutomation.Core Snap-in was successfully
enabled." -ForegroundColor Green
}
else
{
write-host "ERROR: Could not enable PowerCLI VimAutomation.Core
Snap-in, exiting script" -ForegroundColor Red
Exit
}
}
else
{
Write-Host "PowerCLI VimAutomation.Core Snap-in is already enabled"
-ForegroundColor Green
}

if ($env:Processor_Architecture -eq "x86")


{
$vms2deploy = Import-Csv -Path $csvfile

foreach ($vm in $vms2deploy)

write-Host "Deploying VM $($vm.name) to datastore cluster $($vm.datastorecluster)"


Import-vVapp -name $vm.name -template $(get-template -name $vm.template)
-vmhost $vmhost -datastore $(get-datastorecluster -name $vm.datastorecluster) -location $(get-folder -name $vm.folder) | Out-Null
}
else {

#clone the "master" OS Customization Spec, then use it to apply vm


specific IP configuration
$cloned_oscust = Get-OSCustomizationSpec
$vm.oscust | New-OSCustomizationSpec -name "$($vm.oscust)_$($vm.name)"

Set-OSCustomizationNicMapping
-OSCustomizationNicMapping ($cloned_oscust | Get-OscustomizationNicMapping)
-Position 1 -IpMode UseStaticIp -IpAddress $vm.ip -SubnetMask $vm.mask
-DefaultGateway $vm.gw -Dns $vm.dns1,$vm.dns2 | Out-Null
write-Host "Deploying VM $($vm.name) to datastore cluster $
($vm.datastore)"
new-vm -name $vm.name -template $(get-template
-name $vm.template) -vmhost $vmhost -oscustomizationspec $cloned_oscust
-datastore $(get-datastorecluster -name $vm.datastore-cluster) -location $(getfolder -name $vm.folder) | Out-Null
}

#this is where we try to track deployment progress


$loop_control = 0
write-host "Starting VM $($vm.name)"
start-vm -vm $vm.name -confirm:$false | Out-Null

write-host "Waiting for first boot of $($vm.name)"


-ForegroundColor Yellow

do {
$toolsStatus = (Get-VM -name
$vm.name).extensiondata.Guest.ToolsStatus
Start-Sleep 3
$loop_control++
} until ( ($toolsStatus -match toolsOk) -or ($loop_control -gt
$timeout) )

write-host "Waiting for customization spec to apply for


$($vm.name) (a reboot)" -ForegroundColor Green
do {
$toolsStatus = (Get-VM -name
$vm.name).extensiondata.Guest.ToolsStatus
Start-Sleep 3
$loop_control++
} until ( ($toolsStatus -match toolsNotRunning) -or
($loop_control -gt $timeout) )

Write-Host "OS customization in progress for $


($vm.name)" -ForegroundColor red
do {
$toolsStatus = (Get-VM -name
$vm.name).extensiondata.Guest.ToolsStatus
Start-Sleep 3
$loop_control++
} until ( ($toolsStatus -match toolsOk) -or ($loop_control -gt
$timeout) )

#wait another minute "just in case" feel free to remove


this line
Start-Sleep 60

#clean-up the cloned OS Customization spec


Remove-OSCustomizationSpec -CustomizationSpec
$cloned_oscust -Confirm:$false | Out-Null

if ($loop_control -gt $timeout){


Write-Host "Deployment of $($vm.name) took
more than $($timeout/20) minutes, check if everything OK" -ForegroundColor red
}
else {
Write-Host "$($vm.name) successfully deployed,
moving on" -ForegroundColor Green
}
}
else {
Write-Host "$($vm.name) already exists, moving on"
-ForegroundColor Red
}
}
else {
Write-Host "Check input file $csvfile for line with empty vm,
template or OS customization spec value" -ForegroundColor Red
}
}

Write-Host "All vms deployed, exiting" -ForegroundColor Green


#disconnect vCenter
Disconnect-VIServer -Confirm:$false
}
else {
Write-Host "This script should be run from 32-bit version of PowerCLI only, Open
32-bit PowerCLI window and start again" -ForegroundColor Red

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>


Import-VApp -Source $template.template Name $ template.name Location $
template.cluster Datastore $template.datastore

Import-vVapp -name $vm.name -template $(get-template -name $vm.template)


-vmhost $vmhost -datastore $(get-datastorecluster -name $vm.datastorecluster) -location $(get-folder -name $vm.folder)

foreach ($line in $csv_EHC_vApps)

NewVAppName$EHC_vApps.nameDatastore$EHC_vApps.datastoreLocation
$EHC_vApps.clusterVApp$EHC_vApps.template

$CSVfile="c:\admin\scripts\ehc_vapps.csv"

SetPowerCLIConfigurationInvalidCertificateActionIgnoreConfirm:$false|Out
Null

$EHC_vApps=ImportCsvPath$CSVfile
ForEach($EHC_vAppin$EHC_vApps)
{
NewVAppName$EHC_vApp.nameDatastore$EHC_vApp.datastoreLocation
$EHC_vApp.clusterVApp$EHC_vApp.template

#GetlistofvAppVMstosetnetworkcard
$vApp_vms=GetVApp$EHC_vApp.name|GetVM
ForEach($vApp_vmin$vApp_vms)
{
GetVMLocation$EHC_vApp.name$vApp_vm|GetNetworkAdapter|SetNetworkAdapter
Portgroup$EHC_vApp.portgroupConfirm:$false
}

#MovevApptoStudentPodfolder
MoveVAppDestination$EHC_vApp.folderVApp$EHC_vApp.name
}

S-ar putea să vă placă și