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 ) write-host "Using file: $File" $vmlist = Import-CSV $File foreach ($item in $vmlist) { $customspecfile, $VMFSVol, $pdns, $sdns, $portgroup = "" $item # Map variables $ipaddr = $item.ipaddress $subnet = $item.subnet $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" } } 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} | 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{ 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 } } |