👤

重置密码

发送中...
返回登录

使用PowerShell自动克隆虚拟机

虚拟化 19 浏览 2 分钟阅读

概述

有时候需要大量创建虚拟机,这时候如果手动克隆等话比较耗时耗力,如果能自动化克隆需要的虚拟机,无疑是事半功倍的操作,这里记录并分享一下使用PowerShell来自动克隆虚拟机的方式,效果如下图所示

脚本

$vc = '192.168.1.103' #VCenter IP
Connect-VIServer -Server $vc -username "myUsrName" -Password "myPassword"

$vmhost="192.168.1.11" #esxi host
$namestart="myClient"
$template="myTemplate1"
$datastore="datastore2"
$custsysprep = Get-OSCustomizationSpec myCustSpec
$ipstart="192.168.1."
$endipscope=100..150


#循环生成50台虚拟机
foreach ($endip in $endipscope) 
{
    $ip=$ipstart+$endip
    $name=$namestart+$endip
    $custsysprep | Set-OScustomizationSpec -NamingScheme fixed -NamingPrefix $name
    $custsysprep | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $ip -SubnetMask 255.255.255.0 -Dns 192.168.1.1 -DefaultGateway 192.168.1.1
    
    New-vm -vmhost $vmhost -Name $name -Template $template -Datastore $datastore -OSCustomizationspec $custsysprep
}