PowerCLI虛擬機器自定規範批次建立虛擬機器測試

1、背景需求

使用Powershell的VMware PowerCLI模組建立虛擬機器,基本思路,透過Powershell的Import-Excel模組讀取自定義Excel表格的內容,到vCenter Server中執行指令碼的操作命令。

測試環境如下:

vCenter Server下的一個群集Cluster,Cluster啟用了DRS,並配置了自動化級別為全自動,以便使群集在虛擬機器啟動時自動選擇虛擬機器的存放主機。群集內主機掛載有共享儲存DATA。建立的虛擬機器都放置在共享儲存上。

群集Cluster上的主機規劃有

預設管理網vSwitch0埠組VM Network,VLAN ID:0。

業務網vSwitch1埠組INSIDE-NETWORK01,VLAN ID:1000。

心跳網路vSwith2埠組heartbeat,VLAN ID:999。

vCenter Server已有虛擬機器模板:rehl7。9和Windows2016。模板要求:

1、安裝VM Tools

2、建議非Minimal安裝,rhel7。9使用‘Server with GUI’。

要實現以上目標,規劃了Excel的虛擬機器配置資訊表中的各欄位如下:

PowerCLI虛擬機器自定規範批次建立虛擬機器測試

虛擬機器配置資源表

使用power shell的import-excel來讀取這個資源表,再連線到vCenter Server執行虛擬機器建立操作。

2、工具:

1)、Window 10的Powershell

PS C:\Users\stcoc> $PSVersionTableName Value—— ——-PSVersion 5。1。19041。610PSEdition DesktopPSCompatibleVersions {1。0, 2。0, 3。0, 4。0。。。}BuildVersion 10。0。19041。610CLRVersion 4。0。30319。42000WSManStackVersion 3。0PSRemotingProtocolVersion 2。3SerializationVersion 1。1。0。1

2)、Powershell的模組:

下邊VMware的是安裝或者匯入的。模組的放置目錄

PS C:\Users\stcoc> Get-Module | Select-Object -Property ModuleType,Version,Name | ft -a ModuleType Version Name —————— ————- —— Manifest 3。1。0。0 Microsoft。PowerShell。Management Manifest 3。0。0。0 Microsoft。PowerShell。Security Manifest 3。1。0。0 Microsoft。PowerShell。Utility Manifest 3。0。0。0 Microsoft。WSMan。Management Script 2。0。0 PSReadline Script 7。0。0。15939650 VMware。Vim Script 12。0。0。15939657 VMware。VimAutomation。Cis。Core Script 12。0。0。15939652 VMware。VimAutomation。Common Script 12。0。0。15939655 VMware。VimAutomation。Core Script 12。1。0。16997004 VMware。VimAutomation。Sdk

3)、模組放置目錄:

PS C:\Program Files\WindowsPowerShell\Modules> Get-ChildItem -Directory 目錄: C:\Program Files\WindowsPowerShell\ModulesMode LastWriteTime Length Name—— ——————- ———— ——d——- 2020/12/31 1:00 ImportExceld——- 2019/12/7 17:31 Microsoft。PowerShell。Operation。Validationd——- 2019/12/7 17:31 PackageManagementd——- 2019/12/7 17:31 Pesterd——- 2019/12/7 17:31 PowerShellGetd——- 2020/12/31 1:00 PSReadlined——- 2020/12/31 1:00 VMware。CloudServicesd——- 2020/12/31 1:00 VMware。DeployAutomationd——- 2020/12/31 1:00 VMware。ImageBuilderd——- 2020/12/31 1:00 VMware。PowerCLId——- 2020/12/31 1:00 VMware。Vimd——- 2020/12/31 1:00 VMware。VimAutomation。Cis。Cored——- 2020/12/31 1:00 VMware。VimAutomation。Cloudd——- 2020/12/31 1:00 VMware。VimAutomation。Commond——- 2020/12/31 1:00 VMware。VimAutomation。Cored——- 2020/12/31 1:00 VMware。VimAutomation。Hcxd——- 2020/12/31 1:00 VMware。VimAutomation。HorizonViewd——- 2020/12/31 1:00 VMware。VimAutomation。Licensed——- 2020/12/31 1:00 VMware。VimAutomation。Nsxtd——- 2020/12/31 9:48 VMware。VimAutomation。Sdkd——- 2020/12/31 1:00 VMware。VimAutomation。Securityd——- 2020/12/31 1:00 VMware。VimAutomation。Srmd——- 2020/12/31 1:00 VMware。VimAutomation。Storaged——- 2020/12/31 1:00 VMware。VimAutomation。StorageUtilityd——- 2020/12/31 1:00 VMware。VimAutomation。Vdsd——- 2020/12/31 1:00 VMware。VimAutomation。Vmcd——- 2020/12/31 1:00 VMware。VimAutomation。vROpsd——- 2020/12/31 1:00 VMware。VimAutomation。WorkloadManagementd——- 2020/12/31 1:00 VMware。VumAutomation

4)、安裝有Windows office 套件。

3、指令碼內容:

# Import-Module VMware。PowerCLI 並連線到vCenter$username=“administrator@vsphere。local”$password=ConvertTo-SecureString “VMware1!” -AsPlainText -Force$credential=New-Object System。Management。Automation。PSCredential($username,$password)$server = Connect-VIServer -Server 10。10。10。230 -Credential $credential -Force -Verbose# 匯入Excel檔案內容$excel=Import-Excel -Path D:\Scripts\Template。xlsx# 建立臨時的虛擬機器的自定義規範配置$windows=New-OSCustomizationSpec -Name Windows `-Type NonPersistent `-OSType Windows `-NamingScheme vm `-FullName Zeus `-OrgName domain。com `-AdminPassword “P@ssw0rd” `-AutoLogonCount 3 `-ChangeSid:$true `-ProductKey “WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY” `-LicenseMode PerServer `-LicenseMaxConnections 5 `-Workgroup workgroup `-DnsSuffix domain。com `-TimeZone 210 `-Verbose$linux=New-OSCustomizationSpec -Name Linux `-Type NonPersistent `-OSType Linux `-NamingScheme vm `-DnsSuffix domain。com `-Domain domain。com `-Verboseforeach($vm in $excel){$vmcompute=Get-Cluster -Name $vm。cluster -Server $server$vmdatastore=Get-Datastore -Name $vm。datastore -Server $server# 透過使用者名稱建立一個使用者目錄,在使用者目錄內建立虛擬機器Get-Folder -Name $vm。username -Type VMif($? -ne $true){ “Folder Not Exist,Wait For Craete ” | Write-Verbose New-Folder -Name $vm。username -Location VM }$vmfolder=Get-Folder -Name $vm。username# 以excel內的使用者名稱-業務型別-節點數字定義虛擬機器名稱$vmname=$vm。username+“-”+$vm。application+“-”+$vm。nodenumber$vmtemplate=Get-Template -Name $vm。template$vmswitch=Get-VirtualSwitch -Server $server | Where-Object {$_。Name -eq $vm。vswitch}# VLanID值為0,設定成預設的埠組“VM Network”if($vmswitch -eq “vSwitch0” -and $vm。vlan -eq 0 ){ $vmportgroup=Get-VirtualPortGroup -Name “VM Network” -Standard -VirtualSwitch $vmswitch $windows | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -AlternateGateway 223。5。5。5 $linux | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -Dns $vm。defaultgateway -AlternateGateway 223。5。5。5 }else{ # 根據VLAN號確定埠組 $vmportgroup=Get-VirtualPortGroup -VirtualSwitch $vmswitch | Where-Object {$_。VLanId -eq $vm。vlan} $vmportgroup | Write-Verbose }# 根據系統模板型別建立虛擬機器if($vm。template -like “rhel*” -or $vm。template -like “centos*”){ $linux | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP ` -IpAddress $vm。ipaddress ` -SubnetMask $vm。subnetmask ` -DefaultGateway $vm。defaultgateway -Verbose New-VM -Name $vmname ` -ResourcePool $vmcompute ` -Datastore $vmdatastore -Template $vmtemplate ` -OSCustomizationSpec $linux -Location $vmfolder -Notes $vm。remarks -Verbose }else{ $windows | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP ` -IpAddress $vm。ipaddress ` -SubnetMask $vm。subnetmask -DefaultGateway $vm。defaultgateway ` -Dns $vm。defaultgateway -Verbose New-VM -Name $vmname ` -ResourcePool $vmcompute ` -Datastore $vmdatastore -Template $vmtemplate ` -OSCustomizationSpec $windows -Location $vmfolder ` -Notes $vm。remarks -Verbose }$vmdeployed=Get-VM -Name $vmname# 配置虛擬機器計算資源$vmdeployed | Set-VM -NumCpu $vm。numcpu -CoresPerSocket ($vm。numcpu/2) -MemoryGB $vm。memory -Verbose -Confirm:$false# 配置虛擬機器網路埠組$mynetworkadapter=$vmdeployed | Get-NetworkAdapter -Name “Network adapter 1” if($mynetworkadapter。Name -ne $vmportgroup。Name){Set-NetworkAdapter -NetworkAdapter $mynetworkadapter -NetworkName $vmportgroup -Type Vmxnet3 -StartConnected:$true -WakeOnLan:$true -RunAsync -Verbose -Confirm:$false}# 配置虛擬機器的磁碟個數和容量$vmolddisksize= ($vmdeployed | Get-HardDisk)。CapacityGBif($vmolddisksize -lt $vm。disksize){ $count=1 while($count -le $vm。newdiskcounts ){ $vmeachdisksize=($vm。disksize-$vmolddisksize)/$vm。newdiskcounts if($vmeachdisksize -eq 0){ “You don‘t need new Harddisk” | Write-Verbose }else{ New-HardDisk -CapacityGB $vmeachdisksize -Datastore $vmdatastore -StorageFormat Thin -VM $vmdeployed } $count+=1 } }# 啟動虛擬機器$vmdeployed | Start-VM -RunAsync# Start-Sleep -Seconds 20# $vmdeployed | Select-Object -Property Name,{$_。Guest。IPaddress}}# 刪除臨時虛擬機器自定義配置規範並退出Get-OSCustomizationSpec -Type NonPersistent | Remove-OSCustomizationSpec -Verbose -Confirm:$falseDisconnect-VIServer -Server $server -Confirm:$false -Verbose

4、執行情況

虛擬機器資源表建立虛擬機器結果如下:

PowerCLI虛擬機器自定規範批次建立虛擬機器測試

powershell建立虛擬機器

以上,完成了透過虛擬機器自定義規範,透過powershell指令碼建立虛擬機器的目標,要建立其他業務的虛擬機器,可以透過修改Excel中的內容,再次執行即可,excel中的欄位內容儘量縮短,因為虛擬機器名稱和DNS有字元數限制。