VMware怎样用powershell批量部署虚机

发布时间:2021-11-08 17:37:09 作者:柒染
来源:亿速云 阅读:202

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

用CSV生成列表,名称为.csv,抬头如下:

Basevm
Datastore
VMhost
Custspec
VMname
Cpu
Memory
Disk1
Disk2
IPaddress
Subnet
Gateway
VLAN
Note
Centos_6.7_2017_moban_29_30
datastore-10.143.129.30
10.143.129.30
Centos6.7_Test
TA2930-01
2 4 40     65
10.143.129.*
255.255.255.0
10.143.129.1
VM Network
TA2930-01


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


配置文件:Config.ps1

  1. #

  2. # Module manifest for module 'CloneVMCentOS'

  3. #

  4. # Generated by: Guoyu Wang

  5. #

  6. # Email: guo***@cr***.cn

  7. #

  8. # Update: 2016/02/25

  9. #

  10. # Generated on:

  11. #

  12. # Version number of this module.

  13. $ModuleVersion = '1.8'

  14. # Author of this module

  15. $Author = 'Guoyu Wang'

  16. # Company or vendor of this module

  17. $CompanyName = ''

  18. # Copyright statement for this module

  19. $Copyright = ' (c) 2016 . All rights reserved.'

  20. # Description of the functionality provided by this module

  21. $Description = 'Profie of CloneVMCentOS.ps1'

  22. # The VMware vCenter Ip?? user, and password

  23. $VCIP='10.100.8.82'VC所在的虚机

  24. $VCuser='VC账号'

  25. $VCPassword='VC密码'

  26. # The Guest os user and Password

  27. $Guestuser ='root'

  28. $Guestpassword ='123456'

  29. # SMTP Server and Mail information.

  30. $EmailFrom = "******"

  31. $EmailTo = "管理员邮箱"

  32. $Subject = "VM  deploy task is finished  from VMware vCenter"

  33. $Body = "Please check all the Configuration information"

  34. $SmtpServer = "邮箱配置"

配置脚本:CloneVMCentOS6.ps1

  1. <#

  2. .SYNOPSIS

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

  4. .DESCRIPTION

  5. .PARAMETER ComputerName

  6. .INPUTS

  7. System.String

  8. .OUTPUTS

  9. .EXAMPLE

  10. .\CloneVMCentOS6.7

  11. .EXAMPLE

  12. .NOTES

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

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

  15. 20160225 Guoyu Wang v1.8 [+] Add configuration file.

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

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

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

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

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

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

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

  23. 20150922 Guoyu Wang v1.0 [+] Initial version

  24. #TAG:Creditease IT Dept

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

  26. #>

  27. #Step1 Load the PowerCLI

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

  29. Try {

  30. Write-Host "Loading PowerCLI plugin , Please wait a moment..." -foregroundcolor Yellow

  31. Add-PSSnapin VMware.VimAutomation.Core

  32. $PCLIVer = Get-PowerCLIVersion

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

  34. $null = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$false -Scope "Session"

  35. }

  36. }

  37. Catch {

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

  39. Read-Host "Press  to exit"

  40. Exit

  41. }

  42. }

  43. #Step2 Load the Config file

  44. $FilePath = (Get-Location).Path

  45. . $FilePath\Config.ps1

  46. #Step3 Login to the vcenter

  47. $Error.Clear()

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

  49. If ($Error.Count -ne 0){

  50. Write-Host "Error connecting vCenter server $vCenterServer, exiting" -ForegroundColor Red

  51. Exit

  52. }

  53. #Step4 Load the csv

  54. If($args.Count -eq 0){

  55. Write-Host "Usage:  .\CloneVMCentOS6.7.ps1 20150101.csv"

  56. Exit

  57. }

  58. Else {

  59. $Now = Get-Date  -Format "yyyyMMddhhmmss"

  60. $FileName = $FilePath+"\"+$Now+"-utf8.csv"

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

  62. $VMlist = Import-CSV $FileName

  63. }

  64. #Step5 Clone VM

  65. Foreach ($Item in $VMlist) {

  66. $Basevm = $Item.Basevm

  67. $Datastore = $Item.Datastore

  68. $VMhost = $Item.VMhost

  69. $Custspec = $Item.Custspec

  70. $VMname = $Item.VMname

  71. $Memory = $Item.Memory

  72. $Cpu = $Item.Cpu

  73. $Disk1 = $Item.Disk1

  74. $Disk2 = $Item.Disk2

  75. $IPaddr = $Item.IPaddress

  76. $Subnet = $Item.Subnet

  77. $Gateway = $Item.Gateway

  78. $VLAN = $Item.VLAN

  79. $Note = $Item.Note

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

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

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

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

  84. #Clone the Basevm with the adjusted Customization Specification

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

  86. #Change CPU and Memory size

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

  88. #Change Disk size

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

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

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

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

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

  94. Start-VM $VMname

  95. }


  96. #Step8 Logout

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

  98. Exit

在powershell中运行:CloneVMCentOS6.ps1 列表文件.csv

上述就是小编为大家分享的VMware怎样用powershell批量部署虚机了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. hpvm分配资源创建虚机
  2. Powershell 批量重命名

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vmware powershell 批量

上一篇:CentOS 7.4下安装nginx的步骤是什么

下一篇:docker怎样清理容器

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》