Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy virtual machines in batches with powershell by VMware

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how VMware uses powershell to deploy virtual machines in batches. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Generate a list with the name .csv using CSV, with the following header:

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

244065

10.143.129.*

255.255.255.0

10.143.129.1

VM Network

TA2930-01

Basevm: template name

# Datastore: local storage name

# VMhost: host where the generated virtual machine resides

# Custspec: configure in VC (Home Page-Administration-Custom Specification Manager)

# VMname: host name

# CPU\ Memory\ Disk: configuration

# IPaddress:IP

# Subnet: subnet mask

# Gateway: gateway

# VLAN:VLAN

# Note: tag

Configuration file: 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 functionality provided by this module

$Description = 'Profie of CloneVMCentOS.ps1'

# The VMware vCenter Ip?? User, and password

Virtual machine where $VCIP='10.100.8.82'VC is located

$VCuser='VC account'

$VCPassword='VC password'

# The Guest os user and Password

$Guestuser = 'root'

$Guestpassword = '123456'

# SMTP Server and Mail information.

$EmailFrom = "*"

$EmailTo = "Administrator mailbox"

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

$Body = "Please check all the Configuration information"

$SmtpServer = "mailbox configuration"

Configuration script: CloneVMCentOS6.ps1

# 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

. $FilePath\ Config.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 "Harddisk 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

Run in powershell: CloneVMCentOS6.ps1 list file. csv

This is how VMware uses powershell to deploy virtual machines in batches. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report