通过vSpherePowerCLI脚本批量自动部署虚拟机1.下载并安装PowerCLI2.修改PowerCLI执行脚本的策略,允许执行任意脚本;初次运行PowerCLI时提示以下报错通过get-executionpolicy查看本地执行策略,Restricted是禁止执行任何脚本;修改本地执行策略set-executionpolicyRemoteSigned,允许执行任意脚本;关闭窗口,重新运行PowerCLI3.创建脚本$vc='10.0.66.7'#VCenterIPConnect-VIServer-Server$vc-usernameadministrator@vsphere.local-Passwordvmware$vmhost=192.168.1.10#esxihost$namestart=test#vm名称前缀$template=win2012_temp#vm模板$datastore=64.170#存储lun$custsysprep=Get-OSCustomizationSpecWin#自定义规范文件$ipstart=192.168.1.#IP前缀$endipscope=100..150#IP后缀#循环生成50台虚拟机foreach($endipin$endipscope){$ip=$ipstart+$endip$name=$namestart+$endip$custsysprep|Set-OScustomizationSpec-NamingSchemefixed-NamingPrefix$name$custsysprep|Get-OSCustomizationNicMapping|Set-OSCustomizationNicMapping-IpModeUseStaticIP-IpAddress$ip-SubnetMask255.255.255.0-Dns192.168.1.1-DefaultGateway192.168.1.1New-vm-vmhost$vmhost-Name$name-Template$template-Datastore$datastore-OSCustomizationspec$custsysprep}4.执行脚本将上面的脚本保存为“test.ps1”,然后打开VMWarevSpherePowerCLI,切换到存放脚本的目录运行.\test.ps1回车即可运行。在执行脚本时,请不要关闭PowerCLI窗口,脚本执行完成后再关闭。