VMware Powercli常用脚本.doc_第1页
VMware Powercli常用脚本.doc_第2页
VMware Powercli常用脚本.doc_第3页
VMware Powercli常用脚本.doc_第4页
VMware Powercli常用脚本.doc_第5页
已阅读5页,还剩46页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

VMware Powercli常用脚本深圳市XXX有限公司2016年12月 目 录1.1修改ESXI主机的root密码51.2将本地文件传输到VM51.3修改虚拟机硬件版本51.4查看虚拟机快照61.5更改虚拟机网卡61.6批量开关虚拟机61.7批量模板化创建虚拟机61.8删除告警71.9更新vCenter上的集群81.10将主机添加进vCenter111.11为主机添加及配置iSCSI端口111.12添加及配置ESXI主机131.13创建VMFS datastore191.14检索ESXI的网络信息211.15修改虚拟机绑定的端口组251.16创建新的vDS271.17创建新的分布式交换机端口组271.18查看分布式交换机绑定的主机271.19添加主机到分布式交换机281.20配置分布式交换机端口组VLAN281.21创建新的虚拟机281.22为虚拟机添加VMXNET3网卡291.23为虚拟机添加磁盘291.24通过模板部署虚拟机291.25通过模板及自定义规范部署虚拟机291.26通过模板及自定义规范部署虚拟机,并检查是否有足够的可用空间301.27重新注册虚拟机311.28导入虚拟机信息表321.29批量创建虚拟机321.30更改虚拟机默认网关331.31批量修改虚拟机IP信息331.32Windows静默安装vmtools341.33Linux静默安装vmtools351.34批量安装vmtools371.35更新vmtools381.36将虚拟机转换成模板381.37将虚拟机克隆成模板381.38通过自定义规范为Windows绑定静态的IP地址381.39更新虚拟机硬件版本391.40关机更改虚拟机内存和vCPU391.41通过磁盘拷贝的方式将磁盘从厚模式转换成瘦模式431.42将一个数据存储上的所有虚拟机迁移到另一个数据存储461.43查找创建时间超过两个星期的快照及创建者49目 录1.1 修改ESXI主机的root密码$strOldRootPassword=vmware1!$strNewRootPassword=vmware2!$arrHostsWithErrors=()Get-VMHost | ForEach-Object $ConnectVIServer=Connect-VIServer -Server $_.Name -User root -Password $strOldRootPassword $VMHostAccount=$null $VMHostAccount=Set-VMHostAccount -Server $_.Name -UserAccount (Get-VMHostAccount -Server $_.Name -User root) -Password $strNewRootPassword if ($VMHostAccount -eq $null) -or ($VMHostAccount.GetType().Name -ne HostUserAccountImpl) $arrHostsWithErrors +=$_.Name Disconnect-VIServer -Server $_.Name -Confirm:$false1.2 将本地文件传输到VM$vm=Get-VM-NamevmnameGet-ItemC:Temp*.*|Copy-VMGuestFile-Destination-C:Temp-VM$vm-LocalToGuest-HostUserroot-HostPasswordpassword-GuestUseradministrator-GuestPasswordguestpassword1.3 修改虚拟机硬件版本Set-VM-VMvm01-versionv9-confirm:$false1.4 查看虚拟机快照Get-VM|Get-Snapshot#显示快照列表Get-VM|Get-Snapshot|format-list#详细显示快照信息1.5 更改虚拟机网卡Get-VMvmname|Get-NetworkAdapter|Where$_.Type-eqE1000|Set-NetworkAdapter-Typevmxnet31.6 批量开关虚拟机$scope=1.50$namestart=vm#打开50台虚拟机(从 vm1 到vm50)foreach ($v in $scope) $name=$namestart+$vGet-VM -Name $name | Start-VM -Confirm false 1.7 批量模板化创建虚拟机New-vm -vmhost -Name SVR02 -Template WIN2008R2_Template -Datastore datastore1 -OSCustomizationspec WIN2008R2_Template命令中参数说明: -vmhost:VM生成的目标ESXi主机; -Name:生成的VM的名字; -Template:用于生成VM的模板主机; -Datastore:生成的虚拟机的存放数据存储; -OSCustomizationspec:定制化部署VM的prep目录1.8 删除告警function Remove-Alarm Remove-Alarm -Name Book: My Alarm.EXAMPLE PS Remove-Alarm -Name Book:*# param( string$Name ) process $alarmMgr = Get-View AlarmManager $alarmMgr.GetAlarm($null) | % $alarm = Get-View $_ if($alarm.Info.Name -like $Name) $alarm.RemoveAlarm() 1.9 更新vCenter上的集群function Update-vCenterCluster CmdletBinding() Param( parameter(Mandatory=$True , HelpMessage=Name of cluster to patch ) String $ClusterName , parameter(Mandatory=$True , HelpMessage=Name of baseline to use for patching ) String $BaselineName )$baseline = Get-Baseline -Name $BaselineName# Find VUM server$extMgr = Get-View ExtensionManager$vumExt = $extMgr.ExtensionList | where $_.Key -eq com.vmware.vcIntegrity$vumURL = ($vumExt.Server | where $_.Type -eq SOAP).Url$vumSrv = ($vumUrl.Split(/)2).Split(:)0$vumSrvShort = $vumSrv.Split(.)0$vumVM = Get-VM -Name $vumSrvShort# Find VC server$vcSrvShort = $extMgr.Client.ServiceUrl.Split(/)2.Split(.)0$vcVM = Get-VM -Name $vcSrvShort# Patch the cluster nodes$hostTab = Get-Cluster -Name $ClusterName | Get-VMHost | %$hostTab$_.Name = $_$hostTab.Values | %$vm = $nullif($_.Name -eq $vumVM.Host.Name)$vm = $vumVMif($_.Name -eq $vcVM.Host.Name)$vm = $vcVMif($vm)$oldNode = $_$newNode = $hostTab.Keys | where $_ -ne $oldNode.Name | Select -First 1$vumVM = $vumVM | Move-VM -Destination $newNode -Confirm:$falseRemediate-Inventory -Entity $_ -Baseline $baseline1.10 将主机添加进vCenter# Add our host to vCenter, and immediately enable lockdown mode!$VMhost = Add-VMHost -Name vSphere03.vSphere.local -User root -Password pa22word -Location (Get-Datacenter) -Force | Set-VMHostLockdown Enable1.11 为主机添加及配置iSCSI端口# Add iSCSI VMkernel vNIC$vSwitch = Get-VirtualSwitch -VMHost $VMHost -Name vSwitch0# we have to first create a portgroup to bind our vNIC to.$vPG = New-VirtualPortGroup -Name iSCSI -VirtualSwitch $vSwitch -VLanId 55# Create our new vNIC in the iSCSI PG we just created$vNIC = New-VMHostNetworkAdapter -VMHost $VMHost -PortGroup iSCSI -VirtualSwitch $vSwitch -IP -SubnetMask # Enable the software ISCSI adapter if not already enabled.$VMHostStorage = Get-VMHostStorage -VMHost $VMhost | Set-VMHostStorage -SoftwareIScsiEnabled $True #sleep while iSCSI starts upStart-Sleep -Seconds 30 # By default vSphere will set the Target Node name to# .vmware:- the# following cmd will remove everything after the hostname, set# Chap auth, and add a send Target.# Example .vmware:esx01-165435 becomes# .vmware:esx01# Note that if your hostname has dashes in it, you抣l# need to change the regex below.$pattern = .vmware:w*Get-VMHostHba -VMHost $VMHost -Type IScsi | Where-Object $_.IScsiName -match $pattern | Set-VMHostHba -IScsiName $Matches0 | Set-VMHostHba -ChapName vmware -ChapPassword password -ChapType Required | New-IScsiHbaTarget -Address -Port 3260 | Out-Null1.12 添加及配置ESXI主机Function ConfigureVMHost cmdletbinding() Param( Parameter( Mandatory=$true , ValueFromPipelineByPropertyname=$true ) String $IPAddress , Parameter( Mandatory=$true , ValueFromPipelineByPropertyName=$True ) String $Cluster , Parameter( ValueFromPipelineByPropertyName=$True ) string $User = root , Parameter( ValueFromPipelineByPropertyName=$True ) string $password ) # while static enough to not be parameterized well still # define our advanced iSCSI configuration up front thereby # simplifying any future modifications. $ChapName = vmware $ChapPassword =password $ChapType =Required $IScsiHbaTargetAddress =00,01 $IScsiHbaTargetPort = 3260 # well use the last octet of the IPAddress as the ID for # the host. $ESXID = $IPaddress.split(.)3 # Get the actual cluster object for our targeted cluster. $ClusterImpl = Get-Cluster -Name $Cluster # Get the parent folder our cluster resides in. $Folder = Get-VIObjectByVIView $ClusterImpl.ExtensionData.Parent Write-Verbose Adding $($IPAddress) to vCenter # Add our host to vCenter, and immediately enable # lockdown mode! $VMHost = Add-VMHost -Name $IPAddress -User $user -Password $Password -Location $Folder -Force -EA STOP | Set-VMHostLockdown -Enable # Enter Maintenance mode $VMHost = Set-VMHost -State Maintenance -VMHost $VMHost | Move-VMHost -Destination $Cluster #$VMHost = Get-VMHost -Name $IPAddress # Get the Host profile attached to that cluster $Hostprofile = Get-VMHostProfile -Entity $Cluster # attach profile to our new host Apply-VMHostProfile -Entity $VMHost -Profile $HostProfile -AssociateOnly -Confirm:$false | Out-Null # Apply our host profile to gather any required values $AdditionConfiguration = Apply-VMHostProfile -Entity $VMHost -Profile $HostProfile -ApplyOnly -Confirm:$false # If we have a hashtable then there are additional config # Items that need to be defined. Loop through and attempt # to fill them in, prompting if we come across something # were not prepared for. if ($AdditionConfiguration.gettype().name -eq Hashtable) #Create a new hashtable to hold our information $Var = # Loop through the collection switch ($AdditionConfiguration.GetEnumerator() $_.name -like *iSCSI*.address $var +=$_.Name = $( -f $ESXID) $_.name -like *iSCSI*.subnetmask $var += $_.Name = $_.name -like *vMotion*.address $var +=$_.Name = $( -f $ESXID) $_.name -like *vMotion*.subnetmask $var += $_.Name = Default $value = Read-Host Please provide a value for $($_.Name) $var += $_.Name = $value # Apply our profile with the additional config info $VMHost = Apply-VMHostProfile -Entity $VMHost -Confirm:$false -Variable $var Else # Apply our profile. $VMHost = Apply-VMHostProfile -Entity $VMHost -Confirm:$false # update vCenter with our new Profile compliance status Test-VMHostProfileCompliance -VMHost $VMHost | out-null # Enable the software ISCSI adapter if not already enabled. $VMHostStorage = Get-VMHostStorage -VMHost $VMhost | Set-VMHostStorage -SoftwareIScsiEnabled $True #sleep while iSCSI starts up Start-Sleep -Seconds 30 # By default vSphere will set the Target Node name to # .vmware:- This # script will remove everything after the hostname, set Chap # auth, and add a send Target. # # Note that if your hostname has dashes in it, you抣l # need to change the regex below. $pattern = .vmware:w* $HBA = Get-VMHostHba -VMHost $VMHost -Type IScsi | Where $_.IScsiName -match $pattern If ($HBA.IScsiName -ne $Matches0) $HBA = Set-VMHostHba -IScsiHba $HBA -IScsiName $Matches0 Set-VMHostHba -IScsiHba $HBA -ChapName $ChapName -ChapPassword $ChapPassword -ChapType $ChapType | New-IScsiHbaTarget -Address $IScsiHbaTargetAddress -Port $IScsiHbaTargetPort | Out-Null1.13 创建VMFS datastorefunction New-PartitionDatastore param (parameter(ValueFromPipeline = $true,Position=1)ValidateNotNullOrEmpty()VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl$VMHost,parameter(Position=2)ValidateNotNullOrEmpty()PSObject$Partition,parameter(Position=3)ValidateNotNullOrEmpty()String$Name)process$esx = $VMHost | Get-View$storMgr = Get-View $esx.ConfigManager.DatastoreSystem$lunExt = $storMgr.QueryAvailableDisksForVmfs($null)$device = $lunExt | where $_.DeviceName -eq $Partition.DeviceName$dsOpt = $storMgr.QueryVmfsDatastoreCreateOptions($Partition.DeviceName) | where $_.Info.VmfsExtent.Partition -eq $Partition.Partition$spec = $dsOpt.Spec$spec.Vmfs.VolumeName = $Name$spec.Extent += $spec.Vmfs.Extent$dsMoRef = $storMgr.CreateVmfsDatastore($spec)Get-Datastore (Get-View $dsMoRef).Name1.14 检索ESXI的网络信息function Get-HostDetailedNetworkInfo CmdletBinding()param( String$VMHost, String $Cluster ,parameter(Mandatory=$True , HelpMessage=Name of File to Export ) String $filename)Write-Host Gathering VMHost objectsif ($Cluster)$vmhosts = Get-Cluster $Cluster | Get-VMHost | Where-Object $_.State -eq Connected | Get-Viewelse $vmhosts = Get-VMHost $VMHost | Get-View$MyCol = ()foreach ($vmwarehost in $vmhosts) $ESXHost = $vmwarehost.Name Write-Host Collating information for $ESXHost $networkSystem = Get-View $vmwarehost.ConfigManager.NetworkSystem foreach($pnic in $networkSystem.NetworkConfig.Pnic) $pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device) foreach($Hint in $pnicInfo) $NetworkInfo = | Select-Object Host, PNic, Speed, MAC, DeviceID, PortID, Observed, VLAN $NetworkInfo.Host = $vmwarehost.Name $NetworkInfo.PNic = $Hint.Device $NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId $NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId $record = 0 Do If ($Hint.Device -eq $vmwarehost.Config.Network.Pnic$record.Device) $NetworkInfo.Speed = $vmwarehost.Config.Network.Pnic$record.LinkSpeed.SpeedMb $NetworkInfo.MAC = $vmwarehost.Config.Network.Pnic$record.Mac $record + Until ($record -eq ($vmwarehost.Config.Network.Pnic.Length) foreach ($obs in $Hint.Subnet) $NetworkInfo.Observed += $obs.IpSubnet + Foreach ($VLAN in $obs.VlanId) If ($VLAN -eq $null) Else $strVLAN = $VLAN.ToString() $NetworkInfo.VLAN += $strVLAN + $MyCol += $NetworkInfo $Mycol | Sort-Object Host,PNic | Export-Csv $filename -NoTypeInformation1.15 修改虚拟机绑定的端口组function Move-ToNewPortGroup CmdletBinding() Param( parameter(Mandatory=$True , HelpMessage=Name of Port Group to move from ) String $Source , parameter(Mandatory=$True , HelpMessage=Name of Port Group to move to ) String $Target, String $Cluster )$SourceNetwork = $Source$TargetNetwork = $Targetif ($Cluster)Get-Cluster $Cluster | Get-VM | Get-NetworkAdapter | Where-Object $_.NetworkName -eq $SourceNetwork | Set-NetworkAdapter -NetworkName $TargetNetwork

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论