首页 > 其他 > 详细

PowerCLI脚本批量和一些常用自动化操作脚本

时间:2018-08-02 16:49:51      阅读:277      评论:0      收藏:0      [点我收藏+]
一、使用指定模板批量创建虚拟机

#定义参数
param(
[string]$VMname,[string]$vmhostname,[string]$datastore,
[string]$template
)

#在命令窗口中添加powercli模块
try{
add-pssnapin vmware.vimautomation.core -ErrorAction SilentlyContinue
}
catch{}

#连接Vsphere
Connect-VIServer -server Vsphere -Protocol https -User user -Password password

foreach ($i in 1..5)
{
$fullname = $VMname +"-"+ $i
new-vm -name $fullname -template $template -host $vmhostname -datastore $datastore
}

disconnect-viserver -confirm:$false

执行文件时
.\scriptfile.ps1 VMname vmhost datastore template
#不声明参数时,必须按照param指定的顺序输入参数
or
.\screptfile.ps1 -VMname  vmname  -template template -vmhostname vmhost -datastore datastore
#对参数声明时,参数顺序可随意变动

二、批量重启正在运行具有名字相似可以进行匹配的的虚拟机

#在命令窗口中添加powercli模块
try{
add-pssnapin vmware.vimautomation.core -ErrorAction SilentlyContinue
}
catch{}

#连接Vsphere
Connect-VIServer -server Vsphere -Protocol https -User user -Password password

#定义正则表达式
$matchname="^[a-zA-Z]+\d{5}([a-zA-Z]{1,4})?(\w)?([a-zA-Z]{3})?(\d+)?"

#重启匹配的虚拟机
Get-Cluster -Name cluster |Get-VM |where {$_.Name -match $matchname -and $_.PowerState -eq "PoweredOn"} | Restart-VM -RunAsync

disconnect-viserver -confirm:$false

PowerCLI脚本批量和一些常用自动化操作脚本

原文:http://blog.51cto.com/forsk/2153655

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!