VMware怎样用powershell批量部署虚机


这期内容当中小编将会给大家带来有关VMware怎样用powershell批量部署虚机,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。用CSV生成列表,名称为.csv,抬头如下:

Basevm:模板名称
#Datastore:本地存储名称
#VMhost:生成的虚机所在主机
#Custspec:在VC中配开发云主机域名置(主页-管理-自定义规范管理器)
#VMname:主机名
#CPUMemoryDisk:配置
#IPaddress:IP
#Subnet:子网掩码
#Gateway:网关
#VLAN:VLAN
#Note:标记

配置文件:Config.ps1
## Module manifest for module ‘CloneVMCentOS’## Generated by: Guoyu Wang## Email: guo***@cr***.cn## Update: 2016/02/25## Generated on:## Version number of this module.$ModuleVersion = ‘1.8’# Author of this module$Author = ‘Guoyu Wang’# Company or vendor of this module$CompanyName = ”# Copyright statement for this module$Copyright = ‘ (c) 2016 . All rights reserved.’# Description of the functi开发云主机域名onality provided by this module$Description = ‘Profie of CloneVMCentOS.ps1’# The VMware vCenter Ip?? user, and password$VCIP=’10.100.8.82’VC所在的虚机$VCuser=’VC账号’$VCPassword=’VC密码’# The Guest os user and Password$Guestuser =’root’$Guestpassword =’123456’# SMTP Server and Mail information.$EmailFrom = “******”$EmailTo = “管理员邮箱”$Subject = “VM deploy task is finished from VMware vCenter”$Body = “Please check all the Configuration information”$SmtpServer = “邮箱配置”配置脚本:CloneVMCentOS6.ps1

.SYNOPSIS

Clone VM with VMware vCenter 5.x , Setup the VM’s IP Address , Startup VM.

.DESCRIPTION

.PARAMETER ComputerName

.INPUTS

System.String

.OUTPUTS

.EXAMPLE

.CloneVMCentOS6.7

.EXAMPLE

.NOTES

20160603 Guoyu Wang v2.0 [+] Update to PowerCLI v6.3 and VC 6.0U2

20160331 Guoyu Wang v1.9 [+] Set only 1 nic

开发云主机域名 20160225 Guoyu Wang v1.8 [+] Add configuration file.

20151209 Guoyu Wang v1.7 [+] Add send email function.

20151130 Guoyu Wang v1.6 [+] Change the script to add networking test.

20151030 Guoyu Wang v1.5 [+] Change the script to collect the VM’s network and disk information.

20151022 Guoyu Wang v1.4 [+] Change the Copy-VMGuestFile command parameters.

20151014 Guoyu Wang v1.3 [+] Update to change the harddisk1 and harddisk2 at the same time

20151013 Guoyu Wang v1.2 [+] Add VM notes information and change the data vmdk size

20150925 Guoyu Wang v1.1 [+] Add a new NetworkAdapter

20150922 Guoyu Wang v1.0 [+] Initial version

#TAG:Creditease IT Dept

Email: gu***@***e.cn

#>

#Step1 Load the PowerCLI

If ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null) {

Try {

Write-Host “Loading PowerCLI plugin , Please wait a moment…” -foregroundcolor Yellow

Add-PSSnapin VMware.VimAutomation.Core

$PCLIVer = Get-PowerCLIVersion

If ((($PCLIVer.Major * 10 ) + $PCLIVer.Minor) -ge 51) {

$null = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$false -Scope “Session”

}

}

Catch {

Write-Host “Unable to load the PowerCLI plugin. Please verify installation or install VMware PowerCLI and run this script again.”

Read-Host “Press to exit”

Exit

}

}

#Step2 Load the Config file

$FilePath = (Get-Location).Path

. $FilePathConfig.ps1

#Step3 Login to the vcenter

$Error.Clear()

Connect-VIServer -Server $VCIP -Protocol https -User $VCuser -Password $VCPassword -ErrorAction SilentlyContinue

If ($Error.Count -ne 0){

Write-Host “Error connecting vCenter server $vCenterServer, exiting” -ForegroundColor Red

Exit

}

#Step4 Load the csv

If($args.Count -eq 0){

Write-Host “Usage: .CloneVMCentOS6.7.ps1 20150101.csv”

Exit

}

Else {

$Now = Get-Date -Format “yyyyMMddhhmmss”

$FileName = $FilePath+””+$Now+”-utf8.csv”

Get-Content $args | Out-File $FileName -Encoding utf8

$VMlist = Import-CSV $FileName

}

#Step5 Clone VM

Foreach ($Item in $VMlist) {

$Basevm = $Item.Basevm

$Datastore = $Item.Datastore

$VMhost = $Item.VMhost

$Custspec = $Item.Custspec

$VMname = $Item.VMname

$Memory = $Item.Memory

$Cpu = $Item.Cpu

$Disk1 = $Item.Disk1

$Disk2 = $Item.Disk2

$IPaddr = $Item.IPaddress

$Subnet = $Item.Subnet

$Gateway = $Item.Gateway

$VLAN = $Item.VLAN

$Note = $Item.Note

#Get the Specification and set the Nic Mapping (Apply 2 DNS/WINS if 2 are present)

#Get-OSCustomizationSpec $Custspec | Get-OSCustomizationNicMapping | Where-Object {$_.Position -match “1” } |Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $Ipaddr -SubnetMask $Subnet -DefaultGateway $Gateway

Get-OSCustomizationNicMapping -Spec $Custspec | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $Ipaddr -SubnetMask $Subnet -DefaultGateway $Gateway # -Dns 172.16.1

#Get-OSCustomizationSpec $Custspec | Get-OSCustomizationNicMapping | Where-Object {$_.Position -match “2” } | Set-OSCustomizationNicMapping -IpMode UseDhcp

#Clone the Basevm with the adjusted Customization Specification

New-VM -Name $VMname -Datastore $Datastore -VMhost $VMhost -OSCustomizationSpec $Custspec -Template $Basevm -Notes $Note

#Change CPU and Memory size

Set-VM -VM $VMname -MemoryGB $Memory -NumCpu $Cpu -Confirm:$false

#Change Disk size

#Get-VM $VMname | Get-Harddisk | Where-Object {$_.Name -match “Hard disk 1”} | Set-HardDisk -CapacityGB $Disk1 -Confirm:$false

#Get-VM $VMname | Get-Harddisk | Where-Object {$_.Name -match “2”} | Set-HardDisk -CapacityGB $Disk2 -Confirm:$false

#Set the Network Name (I often match PortGroup names with the VLAN name)

Get-VM -Name $Vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Vlan -StartConnected:$true -Confirm:$false

#Get-VM -Name $Vmname | Get-NetworkAdapter -NAME “*2″| Set-NetworkAdapter -NetworkName $Vlan2 -StartConnected:$true -Confirm:$false

Start-VM $VMname

}

#Step8 Logout

Disconnect-VIServer -Server $VCIP -Force -Confirm:$false

Exit
在powershell中运行:CloneVMCentOS6.ps1 列表文件.csv
上述就是小编为大家分享的VMware怎样用powershell批量部署虚机了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注开发云行业资讯频道。

相关推荐: 好域名怎么取

这期内容当中小编将会给大家带来有关好域名怎么取,文章内容丰富且以专业的角度为大家开发云主机域名分析和叙述,阅读完这篇文章希望大家可以有所收获。每个人都喜欢有个好听的名字,我们在注册域名时,也是一样,都希望能取个好名称,无论是自己使用,还是作为投资收开发云主机域…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 03/28 21:45
下一篇 03/28 21:45

相关推荐