Functions/New-Guest.psm1
Function New-Guest { #exemple File: #ipaddress,subnet,gateway,vmname,domain,sourcevm #10.96.11.242,255.255.255.0,10.96.11.1,CQCBOU1WVDDEV12,STAPLESAMS,CQCBOU1WVDDEV11 Param( [string]$File = "C:\scripts\GuestDeployer.csv", [string]$Datastore, [switch]$Template = $false ) write-host "Using file: $File" $vmlist = Import-CSV $File foreach ($item in $vmlist) { $customspecfile, $VMFSVol, $pdns, $sdns, $portgroup = "" $item # Map variables [IpAddress]$ipaddr = $item.ipaddress [IpAddress]$subnet = $item.subnet [IpAddress]$gateway = $item.gateway $vmname = $item.vmname #$template = $item.template $diskformat = "Thin" $sourcevm = $item.sourcevm $domain = $item.domain #STAPLESAMS if($domain -eq "STAPLESAMS"){ #DEV if ($vmname -like "*WVD*" -or $vmname -like "*WVX*" -or $vmname -like "*wvd*" -or $vmname -like "*wvx*"){ $cluster = "DEV-STG"; $customspecfile = "STAPLESAMS"; #$VMFSVol = "dev-stg-vvol" $VMFSVol = "dev-stg-fc-dsc1" $portgroup = "pgBL-LAN-Dev-11" } #PROD if ($vmname -like "*WVP*" -or $vmname -like "*wvp*"){ $cluster = "SY-PROD"; $customspecfile = "STAPLESAMS"; $VMFSVol = "prod-pp-fc-bou1" $portgroup = "pgSY-LAN-Prod-8" } $pdns = "10.96.7.80" $sdns = "10.96.8.80" #CEXPDMZ } elseif($domain -eq "CEXPDMZ"){ #DMZ-LON if ($vmname -like "CQCLON1WVP*" -or $vmname -like "CQCANJ1WVP*"){ $cluster = "SY-ECOM-LON"; $customspecfile = "CEXPDMZ"; $VMFSVol = "ecom-lon-fc-dsc1" $pdns = "10.96.0.10" $sdns = "10.96.0.11" if($ipaddr -like "10.96.0.*"){ $portgroup = "pgSY-DMZ-LON-WEB" } } #DMZ-BOU if ($vmname -like "CQCBOU1WVP*"){ $cluster = "SY-ECOM-BOU"; $customspecfile = "CEXPDMZ"; $VMFSVol = "ecom-bou-fc-dsc1" $pdns = "10.96.10.10" $sdns = "10.96.10.11" } #VMSTAGING } elseif($domain -eq "VMSTAGING"){ $cluster = "DEV-STG"; $customspecfile = "VMSTAGING"; $VMFSVol = "dev-stg-fc-dsc1" $pdns = "10.96.12.86" $sdns = "10.96.12.84" if($gateway -eq "10.96.12.33"){ $portgroup = "pgBL-STG-Web-2061" } elseif($gateway -eq "10.96.12.17"){ $portgroup = "pgBL-STG-FTP-2062" } elseif($gateway -eq "10.96.12.81"){ $portgroup = "pgBL-STG-DC-2064" } elseif($gateway -eq "10.96.12.97"){ $portgroup = "pgBL-STG-SQLECOM-2066" } else { write-warning "Wrong gateway" exit } } else { write-warning "Wrong domain" exit } if(![string]::IsNullOrEmpty($Datastore)){ $VMFSVol = $Datastore } write-host "customspecfile: $customspecfile" write-host "VMFSVol: $VMFSVol" write-host "Cluster: $cluster" write-host "DNS: $pdns, $sdns" write-host "Portgroup: $portgroup" if($customspecfile -eq "" -or $VMFSVol -eq ""){ return "Invalid customspecfile or VMFSVol" } # Find matching datastore with most free space $Datastore = Get-Datastore | where {($_.Name -match $VMFSVol) -and ($_.Name -notlike "snap-*")} | Sort FreeSpaceGB | Select -Last 1 # Find host with most available memory $BestHost = Get-Cluster $cluster | Get-VMHost | Sort MemoryUsageGB | Select -Last 1 write-host "Datastore: $Datastore" write-host "Host: $BestHost`n" if([string]::IsNullOrEmpty($Datastore)){ return "Invalid datastore" } if($confirm -ne "y"){ $confirm = read-host "Continue? (y/n)" if($confirm -ne "y"){ return "task cancelled" } } # Deal with customization spec file Get-OSCustomizationSpec $customspecfile | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns,$sdns #Deploy the VM based on the template with the adjusted Customization Specification try{ if($Template){ New-VM -Name $vmname -Template $sourcevm -Datastore $Datastore -DiskStorageFormat $diskformat -VMHost $BestHost -ErrorAction Stop -OSCustomizationSpec $customspecfile } else { New-VM -Name $vmname -VM $sourcevm -Datastore $Datastore -DiskStorageFormat $diskformat -VMHost $BestHost -ErrorAction Stop -OSCustomizationSpec $customspecfile } }catch{ return "ERREUR $($Error[0])" } Sleep -seconds 30 #Set the Port Group Network Name (Match PortGroup names with the VLAN name) try{ Get-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -Portgroup $portgroup -Confirm:$false -ErrorAction Stop Get-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -startconnected:$true -Confirm:$false -ErrorAction Stop }catch{ return "ERREUR $($Error[0])" } Start-VM $vmname Sleep -seconds 20 } } |