Functions/New-Guest.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
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 ) if($null -eq $global:defaultviserver){ return "Not connected to vCenter" } 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" } elseif($ipaddr -like "10.96.1.*"){ $portgroup = "pgSY-DMZ-LON-B2B" } else { $portgroup = Read-Host "Portgroup" } #DMZ-BOU }elseif ($vmname -like "CQCBOU1WVP*"){ $cluster = "SY-ECOM-BOU"; $customspecfile = "CEXPDMZ"; $VMFSVol = "ecom-bou-fc-dsc1" $pdns = "10.96.10.10" $sdns = "10.96.10.11" $portgroup = Read-Host "Portgroup" } else { return "Mauvais nom de VM" } #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 { $portgroup = Read-Host "Portgroup" 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 } } |