【Vmwere】用powershell批量部署虛機
用CSV生成列表,名稱為***.csv,抬頭如下:
Basevm:模板名稱
#Datastore:本地儲存名稱
#VMhost:生成的虛機所在主機
#Custspec:在VC中配置(主頁-管理-自定義規範管理器)
#VMname:主機名
#CPU\Memory\Disk:配置
#IPaddress:IP
#Subnet:子網掩碼
#Gateway:閘道器
#VLAN:VLAN
#Note:標記
配置檔案:Config.ps1
配置指令碼:CloneVMCentOS6.ps1
在powershell中執行:CloneVMCentOS6.ps1 列表檔案.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
- #
- # 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
- $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 = "郵箱配置"
- <#
- .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
- . $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 "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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31442725/viewspace-2136000/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 用PowerShell在China Azure建立ARM虛擬機器虛擬機
- PowerShell管理Hyper-V虛擬機器虛擬機
- WindowsServer8Tips(二)Hyper-V3:用PowerShell匯出和匯入虛擬機器WindowsServer虛擬機
- 【虛擬機器】Windows(x86)上部署ARM虛擬機器(Ubuntu)虛擬機WindowsUbuntu
- 使用虛擬機器部署vcenter的理由虛擬機
- 【持續部署】批量部署工具,總結、對比
- 分散式爬蟲的部署之Scrapyd批量部署分散式爬蟲
- KVM 一鍵批量建立虛擬機器 自動設定IP虛擬機
- 快速批量檢查所有虛擬機器的各項指標虛擬機指標
- Azure 基礎:使用 powershell 建立虛擬網路
- OA專案-虛擬機器上部署專案虛擬機
- 虛擬機器部署 Sentinel 服務錯誤記錄虛擬機
- Tomcat—部署配置及優化(安裝部署;虛擬主機配置;優化)Tomcat優化
- 另一種批量裝機神器-----使用PXE批量裝機
- 虛擬機器網路啟用虛擬機
- thinkphp5專案如何在雲虛擬主機部署PHP
- Python動態規劃實現虛擬機器部署Python動態規劃虛擬機
- 虛擬主機上部署SSL證書如何選擇品牌
- 開機自啟動Powershell指令碼指令碼
- powershell 生成隨機使用者資訊隨機
- ansible-playbook 批量部署lnmp環境LNMP
- 使用變數替換批量部署GoldenGate變數Go
- Powershell tricks::Powershell RemotingREM
- 用 Docker Machine 建立 Azure 虛擬主機DockerMac
- 批量ping主機
- 批量機器登入
- Windows下使用vagrant以及Oracle VM Virtualbox部署虛擬機器WindowsOracle虛擬機
- web專案部署上線(無虛擬主機,待學習)Web
- 部署虛擬機器環境安裝RHEL7系統虛擬機
- kvm虛擬化管理平臺WebVirtMgr部署-完整記錄(安裝Windows虛擬機器)-(4)WebWindows虛擬機
- kvm虛擬化管理平臺WebVirtMgr部署-完整記錄(安裝ubuntu虛擬機器)-(5)WebUbuntu虛擬機
- 不停機條件下部署 Django 應用Django
- Docker容器與虛擬化技術:OpenEuler 部署 docker容器應用Docker
- 使用虛擬機器在CentOS上安裝部署資料庫使用虛擬機CentOS資料庫
- 群暉下虛擬機器編譯部署WOW服務端TrinityCore虛擬機編譯服務端
- Azure 基礎:用 PowerShell 自動登入
- 部署KVM虛擬化平臺
- PowerShell 中,您可以透過遠端連線來管理其他計算機。以下是一些常見的用於管理遠端主機的 PowerShell 命令計算機