powercli創建虛擬機步驟及批量創建腳本


文檔中心
https://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.powercli.cmdletref.doc%2FSet-OSCustomizationSpec.html
https://blogs.vmware.com/PowerCLI/2014/05/working-customization-specifications-powercli-part-1.html
http://powershelldistrict.com/powercli-oscustomizationspec/
https://powercli-core.readthedocs.io/en/latest/cmd_new.html#new-oscustomizationspec
官方文檔中心

添加一塊新硬盤

get-vm {vmname}|New-HardDisk -CapacityGB 300

批量設置硬件

Get-VM -name {hostname}*|set-VM -MemoryGB 4 -NumCPU 2

基於模板創建虛擬機

new-VM -Name {hostname}  -Template template-centos76 -VMHost 10.112.131.5  -OSCustomizationspec TestLinux

設置規范模板


Get-OSCustomizationSpec TestLinux|Get-OSCustomizationNicMapping|Set-OSCustomizationNicMapping \
-IpMode UseStaticIP \
-SubnetMask 255.255.255.0 \
-DefaultGateway 10.11.121.254 \
-IpAddress 10.11.121.203

Get-OSCustomizationSpec TestLinux|Get-OSCustomizationNicMapping|Set-OSCustomizationNicMapping \
-IpMode UseStaticIP \
-SubnetMask 255.255.255.0 \
-DefaultGateway 10.11.131.254 \
-IpAddress 10.11.131.64

獲得規范信息

Get-OSCustomizationSpec liunx

創建一個新的規范

New-OSCustomizationSpec -OSType "Linux" -Name linux1 -Type Persistent –DnsServer "8.8.8.8"  –Domain "vmware.com"

批量創建腳本

$vserver="10.111.111.22"
$vuser="youname"
$vpwd="yourpasswd"


filter Convert-IP2Decimal 
{
    ([IPAddress][String]([IPAddress]$_)).Address
}

filter Convert-Decimal2IP
{
	([System.Net.IPAddress]$_).IPAddressToString
}

$IPPrefix="10.111.31"
$startIp=$args[0]
$startHostname=$args[1]
$num=$args[2]
$gateway="10.111.31.254"
$netmask="255.255.255.0"
$cpu=4
$mem=8
$template="template-centos7"

$vhost17=,"10.111.17.1","10.111.17.2","10.111.17.3","10.111.17.4"
$vhost20=,"10.111.20.1","10.111.20.2","10.111.20.3","10.111.20.4"
$vhost31=,"10.111.31.1","10.111.31.2","10.111.31.3","10.111.31.4"


function LoginVSphere($server,$user,$pwd){
	Connect-VIServer -Server $server[0] -User $server[1] -Password $server[2] -EA "Stop"
}

LoginVSphere($vserver, $vuser, $vpwd)
$temp=Get-OSCustomizationSpec TestLinux 






for($n=[int]$startIp;$n -le [int]$num; $n++)
{
	$ipaddr=-Join($IPPrefix,'.',$n)
	$hostname=-Join($startHostname,$n)
	$temp|Get-OSCustomizationNicMapping|Set-OSCustomizationNicMapping -IpMode UseStaticIP -SubnetMask $netmask -DefaultGateway $gateway -IpAddress $ipaddr
	new-VM -Name $hostname -Template $template -VMHost 10.111.31.5 -OSCustomizationspec TestLinux -Confirm:$false
	Get-VM -Name $hostname|Set-VM -MemoryGB $mem -NumCPU $cpu -Confirm:$false
}

Reference
https://communities.vmware.com/thread/567585
腳本參考
https://blogs.vmware.com/PowerCLI/2014/05/working-customization-specifications-powercli-part-1.html
http://powershelldistrict.com/powercli-oscustomizationspec/
https://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.powercli.cmdletref.doc%2FSet-OSCustomizationSpec.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM