[PCLI] VMWARE Autodeploy script
use csv file to get infos
# Input parameters :
# -------
param ($csvfile)
$deployinfos = Import-Csv -Path $csvfile
$deployinfos = $deployinfos | where-object {($_.name -ne "") -and ($_.template -ne "") -and ($_.oscust -ne "") -and ($_.cluster -ne "")} | sort-object name -unique
function getvariable
{
param($name)
$deployinfos | sort-object $name -unique | select-object $name
}
$vcenter = getvariable -name vcenter
$vmname = getvariable -name vmname
$datastore = getvariable -name datastore
$template = getvariable -name template
$respool = getvariable -name respool
$networkname = getvariable -name networkname
$folder = getvariable -name folder
$custprf = getvariable -name custprf
$cluster = getvariable -name cluster
$cpu = getvariable -name cpu
$mem = getvariable -name mem
$dsk = getvariable -name dsk
$datacenter = getvariable -name datacenter
$dnsserver = getvariable -name dnsserver
$ip = getvariable -name ip
$mask = getvariable -name mask
$gw = getvariable -name gw
$ostype = getvariable -name ostype
$FullName = getvariable -name FullName
$OrgName = getvariable -name OrgName
$ErrorWarningPreference = "SilentlyContinue"
Function StopStartVM {
param($VMname)
Write-Host " > Stopping VM : " $VMname -BackgroundColor DarkRed
Stop-VM -VM $VMname -Confirm:$false
Write-Host "Stopping VM : OK" -BackgroundColor DarkGreen
Write-Host " > Starting VM : " $VMname -BackgroundColor DarkRed
Start-VM -VM $VMname
Write-Host "Starting VM : OK" -BackgroundColor DarkGreen
}
## --------------------- load PowerCLI snap-in
$vmsnapin = Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
# --------------------- Log file definition
$result_install = "install_"+$vmname+".log"
$redirect_warning = "warning_"+$vmname+".log"
## Get-Command -PSSnapIn VMware.VumAutomation
$Error.Clear()
If (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue))
{
Add-PSSnapin VMware.VimAutomation.Core > $null
}
Set-ExecutionPolicy unrestricted
Set-PowerCLIConfiguration -InvalidCertificateAction "Ignore" -Confirm:$false > $redirect_warning
## -------------------- START CHECK ------------------------- ##
start-transcript -path $result_install
Write-Host "Checking parameters ..." -BackgroundColor DarkGreen
if (-not(connect-VIServer -Server $vcenter -ErrorAction SilentlyContinue))
{
Write-Warning "Cannot connect to $($server) vCenter server !"
exit
}
if (-not (Get-cluster -Name $cluster -ErrorAction SilentlyContinue))
{
Write-Warning "Cluster_ESXi $($cluster) not found !"
exit
}
if (-not (Get-datacenter -Name $datacenter -ErrorAction SilentlyContinue))
{
Write-Warning "Cluster_ESXi $($datacenter) not found !"
exit
}
if ((get-cluster -name $cluster | Get-vm -Name $vmname -ErrorAction SilentlyContinue))
{
Write-Warning "VM $($vmname) already exists !"
exit
}
if (-not (get-datacenter -name $datacenter | Get-datastore -Name $datastore -ErrorAction SilentlyContinue))
{
Write-Warning "Datastore $($datastore) not found !"
exit
}
if (-not (get-datacenter -name $datacenter | Get-Template -Name $template -ErrorAction SilentlyContinue))
{
Write-Warning "Template $($template) not found !"
exit
}
if (-not ((get-datacenter -name $datacenter | get-template -Name $template).Extensiondata.Network))
{
Write-Warning "Template $($template) : network card not found !"
exit
}
if (-not (get-cluster -name $cluster | Get-resourcepool -Name $respool -ErrorAction SilentlyContinue))
{
Write-Warning "ResourcePool $($respool) not found !"
exit
}
else {
$respool=get-cluster -name $cluster | Get-ResourcePool -Name $respool
}
if (-not (Get-VirtualPortGroup -distributed -Name $networkname -ErrorAction SilentlyContinue -WarningAction SilentlyContinue))
{
Write-Warning "Virtual Distributed PortGroup $($networkname) not found !"
exit
}
if (-not (get-datacenter -name $datacenter | Get-folder -Name $folder -type VM -ErrorAction SilentlyContinue))
{
Write-Warning "Location $($folder) not found !"
exit
}
else {
$folder=get-datacenter -name $datacenter | Get-folder -Name $folder -type VM
}
if (-not (Get-OSCustomizationSpec $custprf -ErrorAction SilentlyContinue))
{
Write-Warning "Guest Customization profile $($custprf) not found !"
exit
}
if (($ostype -ne "Linux") -and ($ostype -ne "Windows")){
Write-Warning "ostype is a MANDATORY value - its default value is NULL"
Write-Warning "ostype must be Linux or Windows with case sensitive"
exit
}
## -------------------- END CHECK ------------------------- ##
## --------------------- Connecting to the vcenter server
Write-Host "Connecting to vCenter server ..." -BackgroundColor DarkRed
Write-Host "------------------------------------------"
connect-VIServer -Server $vcenter
Write-Host " > vCenter Connection : OK" -BackgroundColor DarkGreen
Write-Host "------------------------------------------"
## --------------------- We are anlysing the template content
Write-Host "Analysing template content ..." -BackgroundColor DarkRed
Write-Host "------------------------------------------"
$inventory = Get-Template -Name $template
$mem2 = ($inventory.ExtensionData.Config.Hardware.MemoryMB)/1024
$nbhdd = ($inventory | Get-HardDisk | Measure-Object).Count
$sizehdd = (($inventory | Get-HardDisk).CapacityGB)
write-host "vCPU = " $inventory.ExtensionData.Config.Hardware.NumCPU -BackgroundColor DarkGreen
write-host "Memory (Gb) = " $mem2 -BackgroundColor DarkGreen
write-host "Number of Hard Disks = " $nbhdd -BackgroundColor DarkGreen
write-host "Size of Hard Disks (Gb) = " $sizehdd -BackgroundColor DarkGreen
Write-Host "------------------------------------------"
## --------------------- We are anlysing the template content
Write-Host "Selecting randomly a host in the cluster ..." -BackgroundColor DarkRed
Write-Host "------------------------------------------"
$cluster = Get-Cluster -name $cluster | Get-VMHost -state connected | Get-Random
write-host "HOST = " $cluster -BackgroundColor DarkGreen
## --------------------- We are deploying the new VM from template
Write-Host "Clone is running with value below..." -BackgroundColor DarkRed
Write-Host "------------------------------------------"
write-host "CPU : "$cpu -BackgroundColor DarkGreen
write-host "MEM : "$mem -BackgroundColor DarkGreen
write-host "DSK : "$dsk -BackgroundColor DarkGreen
if (($($cpu) -ne "default") -and ($($mem) -ne "default")) {
New-VM -Name $vmname -Template $template -Datastore $datastore -ResourcePool $respool -Location $folder -vmhost $cluster | set-vm -NumCpu $cpu -MemoryMB $mem -Confirm:$false >> $redirect_warning
}
elseif (($($cpu) -eq "default") -and ($($mem) -ne "default")) {
New-VM -Name $vmname -Template $template -Datastore $datastore -ResourcePool $respool -Location $folder -vmhost $cluster | set-vm -MemoryMB $mem -Confirm:$false >> $redirect_warning
}
elseif (($($cpu) -ne "default") -and ($($mem) -eq "default")) {
New-VM -Name $vmname -Template $template -Datastore $datastore -ResourcePool $respool -Location $folder -vmhost $cluster | set-vm -NumCpu $cpu -Confirm:$false >> $redirect_warning
}
elseif (($($cpu) -eq "default") -and ($($mem) -eq "default")) {
New-VM -Name $vmname -Template $template -Datastore $datastore -ResourcePool $respool -Location $folder -vmhost $cluster -Confirm:$false >> $redirect_warning
}
if ($?) {
Write-Host " > Clone : OK ..." -BackgroundColor DarkGreen
}else {
Write-Host " > Clone : NOK ..." -BackgroundColor DarkRed
}
Write-Host "------------------------------------------"
Write-Host "Trying to start VM :" $vmname -BackgroundColor DarkRed
Write-Host "------------------------------------------"
Start-VM -VM $vmname >> $redirect_warning
Write-Host " > Start VM : OK " -BackgroundColor DarkGreen
Write-Host "------------------------------------------"
## --------------------- Change disk size
Write-Host "Changing disk size : " $vmname -BackgroundColor DarkRed
if ($($dsk) -ne "default"){
get-harddisk -VM $vmname | ?{$_.name -eq "Disque Dur 1"} | Set-HardDisk -CapacityGB $dsk -Confirm:$false
}
## --------------------- VM Network is configured
Write-Host "Configuring vNetwork on VM : " $vmname -BackgroundColor DarkRed
Get-VM -Name $vmname | get-NetworkAdapter -Name "Adaptateur réseau 1" | set-networkAdapter -NetworkName $networkname -startconnected 1 -connected 1 -Confirm:$false
if (get-networkadapter -vm $vmname) {
Write-Host "vNetwork on VM : OK" -BackgroundColor DarkGreen
}
else
{
Write-Host "vNetwork on VM : NOK" -BackgroundColor DarkGreen
}
StopStartVM ($vmname)
## VM customization
$specname = "tempspec" + (Get-Random -Minimum 1 -Maximum 100000000)
if ($ostype -eq "Linux") {
$vmcust = New-OSCustomizationSpec –Name $specname –Domain eng.local –DnsServer $dnsserver –NamingScheme VM –OSType Linux -Type NonPersistent
$nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec $vmcust
$nicMapping | Set-OSCustomizationNicMapping –IpMode UseStaticIP –IpAddress $ip –SubnetMask $mask –DefaultGateway $gw
}
elseif ($ostype -eq "Windows") {
$vmcust = New-OSCustomizationSpec –Name $specname -FullName $FullName -OrgName $OrgName –workgroup Workgroup –OSType Windows -Type NonPersistent -ChangeSid
$nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec $vmcust
$nicMapping | Set-OSCustomizationNicMapping –IpMode UseStaticIP –IpAddress $ip –SubnetMask $mask –DefaultGateway $gw -Dns $dnsserver
}
Stop-VM $vmname -Confirm:$false
Set-VM –VM $vmname –OSCustomizationSpec $vmcust –Confirm:$false
Start-VM $vmname -Confirm:$false
Write-Host "On attend 3 minutes le temps que le réseau monte ..." -BackgroundColor DarkGreen
Start-Sleep 180
Write-Host "On ping la machine ..." -BackgroundColor DarkRed
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.Send($ip)
if ($Reply.status –eq "Success")
{
Write-Host "RESULAT : Ping OK pour la VM $($vmname)" -BackgroundColor DarkGreen
}
else {
Write-Host "RESULAT : Ping NOK pour la VM $($vmname)" -BackgroundColor DarkRed
}
stop-transcript
exit
/image%2F0881799%2F20140610%2Fob_a8c7fe_logo2.jpg)
Commenter cet article