(資料圖)
需要實(shí)現(xiàn)通過Powershell腳本給VMware虛擬機(jī)批量掛載磁盤并且初始化需要滿足如下要求。
1 Windows系統(tǒng)上需要導(dǎo)入VMware Powershell模塊。建議導(dǎo)入用戶個(gè)人Powershell模塊目錄下。
#Powershell模塊版本名稱為:VMware-PowerCLI-13.0.0-20829139.zip 需要解壓后把解壓內(nèi)容放置到如下創(chuàng)建Modules目錄下。PS C:\Users\dengpeng> $env:PSModulePath #查看路徑C:\Users\dengpeng\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules#根據(jù)如上輸出結(jié)果,\WindowsPowerShell\Modules需要在Documents目錄下創(chuàng)建。#輸入命令 Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $True#輸入命令導(dǎo)入模塊 Import-Module VMware.PowerCLI 即可導(dǎo)入成功
?2 虛擬機(jī)都需要安裝VMTloos工具才行。
具體Powershell腳本內(nèi)容如下。
param( [Parameter(Mandatory=$true, HelpMessage="The Name prefix of Citrix virtual machine", Position=0)] [string] $vmprefix, [Parameter(Mandatory=$true, HelpMessage="The number of the fisrt virtual machine", Position=1)] [int] $startnum, [Parameter(Mandatory=$true, HelpMessage="The number of the last virtual machine", Position=2)] [int] $endnum, [Parameter(Mandatory=$true, HelpMessage="The number of the last virtual machine", Position=2)] [int] $diskszie )#the script to initilize D drive in virtual machine#Get-Partition -DriveLetter "E" | Set-Partition -NewDriveLetter "P" |Out-Null$script = @"Get-Disk | Where PartitionStyle -eq "raw" | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "個(gè)人數(shù)據(jù)盤""@#######################################################################################VC$VC_IP="176.10.10.10" #vCenter登錄IP$VC_USER="administrator@vsphere.local" #vCenter登錄用戶名$VC_PWD="password" #vCenter登錄密碼#$DataStore="Local-01" #指定存儲(chǔ)添加磁盤,若跟虛擬機(jī)存儲(chǔ)位置保值一致,注釋掉即可。#window$GuestUser="administrator" #添加磁盤虛擬機(jī)本地管理員賬號(hào)$GuestPassword="password" #密碼#$diskszie=""#disk$diskcounts="2" #1代表未添加磁盤前,系統(tǒng)磁盤數(shù)量。MCS批量發(fā)布后,默認(rèn)兩塊#######################################################################################the file to save virtual machine uuid to activate Chrome$datevalue = get-date -Format "yyyyMMddHHmm"#$filename = "C:\Users\citrixadmin\Desktop\VM-disk\" + $datevalue + "chrome.csv"echo "Serial Number (mandatory),Asset Tag" > $filenameif ( $startnum -le $endnum ) { #Connect-VIServer hk-hco-vcsa-01 $VC = connect-viserver -server $VC_IP -user $VC_USER -password $VC_PWD for($i = $startnum; $i -le $endnum; $i++) { $vmname = $vmprefix + "{0:d3}" -f $i #表示虛擬機(jī)名稱最后幾位數(shù)例如CVAD-VDI-### 代表3 $nu = "{0:d3}" -f $i #表示虛擬機(jī)名稱最后幾位數(shù)例如CVAD-VDI-### 代表3 $Exists = get-vm -name $vmname -ErrorAction SilentlyContinue If ($Exists) { $vmuuid = (get-vm -Name $vmname | Get-VIew).config.uuid $vmuuid = $vmuuid.replace("-", "") $uuidlen = ${vmuuid}.length $result = "VMware-" for ($a = 0; $a -le $uuidlen; $a = $a + 2) { if ($a -eq 14) { $middle = "-" } elseif ($a -eq 30) { $middle = "," + $vmname } else { $middle = " " } $result = $result + $vmuuid[$a] + $vmuuid[$a + 1] + $middle } echo ${result} >> $filename $diskcount = (Get-HardDisk -vm $vmname).count if ($diskcount -eq $diskcounts) { #$dsname = (Get-HardDisk -vm $vmname | Get-Datastore).Name #New-HardDisk -VM $vmname -CapacityGB $diskszie -StorageFormat Thin -DataStore $DataStore |Out-Null #數(shù)據(jù)盤和系統(tǒng)不同存儲(chǔ) New-HardDisk -VM $vmname -CapacityGB 20 -StorageFormat Thin | Out-Null #數(shù)據(jù)盤和系統(tǒng)同一存儲(chǔ) $OSversion = (get-vm -Name $vmname | Get-view).config.GuestFullName if ( $OSversion -like "*Windows*") { $vm = Get-VM -Name $vmname if ($vm.PowerState -eq "PoweredOff") { Start-VM -VM $vm -Confirm:$false Start-Sleep -s 60 } #Invoke-VMScript -ScriptText $script -VM $vmname -GuestUser $GuestUser -GuestPassword $GuestPassword -ScriptType PowerShell |Out-Null Invoke-VMScript -ScriptText $script -VM $vmname -Server $VC_IP -ScriptType PowerShell Start-Sleep -s 2 Write-Host [1] VM數(shù)據(jù)盤添加列表: -ForegroundColor Green Write-Host ${vmname} 添加數(shù)據(jù)盤完成 -ForegroundColor Green } } else { Write-Host [2] VM數(shù)據(jù)盤已完成列表: -ForegroundColor Green #echo "Please check if the virtual machine ${vmname} is with 200 GB drive, or you can add the disk manually." Write-Host ${vmname} 已完成數(shù)據(jù)盤完成 } } else { #echo "${vmname} does not exist" #Write-Host [3] VM 列表: -ForegroundColor Green Write-Host $nu ${vmname} 沒有查詢到 -ForegroundColor Red } }}else { echo "The start number should be less than end number!"}
運(yùn)行顯示效果如下,表示虛擬機(jī)前綴為CVAD-VDI- 最后表示三位### 在腳本里面已經(jīng)設(shè)置。設(shè)置開始位數(shù)和結(jié)束位數(shù)即可執(zhí)行。重復(fù)添加也無(wú)影響,腳本已經(jīng)做了相應(yīng)判斷,已添加磁盤會(huì)自動(dòng)略過。200為磁盤容量,默認(rèn)GB為單位。