连接至vCenter Server或ESXi



Connect-VIServer -Server serverip -username root -Password password #这个在通过程序菜单启动PowerCLI的时候就会提示的


----------------------------
使PowerShell启动时自动加载PowerCLI插件,将以下代码保存为
%UserProfile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 
 
  
if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null) {
	try {
		Write-Host "Loading PowerCLI plugin, this may take a little while" -foregroundcolor "cyan"
		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 <Enter> to exit"
		exit
	}
}
 
  

----------------------------
查找vSphere环境中存在快照的虚拟机(Get-VM后面加上指定虚机名称即可查询指定虚机的快照)
 
  
Get-VM | Get-Snapshot     #显示快照列表
Get-VM | Get-Snapshot | format-list    #详细显示快照信息
Get-VM | Get-Snapshot | format-table Created,VM,SizeMB    #指定输出列
Get-VM | Get-Snapshot | format-table Created,VM,@{label="SizeGB";Expression={"{0:N2}" -f ($_.SizeMB/1024)}}    #将输出单位改为GB,并指定小数位数为2位。5.1版本以后可以用SizeGB代替SizeMB/1024
 
  

----------------------------
将指定VM的E1000网卡替换为VMXNET3(KB2059053的临时方案)
 
  
Get-VM vmname | Get-NetworkAdapter | Where {$_.Type -eq "E1000"} | Set-NetworkAdapter -Type vmxnet3


esxi 网卡驱动ne1000 esxi i211网卡_运维


--------------------

获取存储对象,并排除掉本地存储(如需筛选输出列,使用Select-Object)


Get-Datastore| Where-Object {$_.Name -notmatch "local|datastore"}
 
  

--------------------
 
  
PowerCLI 脚本创建10个虚拟机
#************************************
#需要定义esxi主机IP host1
#需要定义esxi主机IP host1
#需要预先配置好模板 名称 Win2K3
#需要定义存储 vStorage1
#最后就是自定义规范定义好 $customization
#************************************
Connect-VIServer -Server 192.168.1.11 -User testuser -Password pwd
Get-VMHost
$host1 = "192.168.1.2"
$template = "Win2K3"
$datastore = "vStorage1"
$customization = "Win2K3"
for($i = 1; $i -le 10; $i++)
{
$tempvm = "vServer-"+$i
$array += $tempvm
}
foreach ($vm in $array)
{
$vm=New-VM -Name $vm -Template $template -Host $host1 -Datastore $datastore
-OSCustomizationSpec $customization -Confirm:$false
}
 
  

--------------------
 
  
# 用来获取vSphere环境中ESXi主机一周内的CPU、内存、磁盘、网络最大使用情况
# 保存为 Weekly_Report.ps1 以 Weekly_Report.ps1 vcenterserver 的格式执行

# 连接vCenter方法1,通过参数指定vCenter的方式
param( [string] $VIServer )
Write-Host "Connecting to VI Server"
Connect-VIServer $VIServer
if ($VIServer -eq ""){
	Write-Host
	Write-Host "Please specify a vCenter Server eg...."
	Write-Host "powershell.exe Weekly_Report.ps1 10.113.7.211"
	Write-Host
	Write-Host
	exit
}

# 连接vCenter方法2,根据提示输入vCenter连接信息的方式
#$VCServer = Read-Host "Enter the vCenter server name" 
#$Username = Read-Host "Enter the username" 
#$Password = Read-Host "Enter password" 
#Connect-VIServer $VCServer -User $username -Password $password -port 443 
#if ($VIServer -eq ""){
#	Write-Host
#	Write-Host "Please input a valid credential"
#	Write-Host
#	exit
#}

#指定以csv格式,以当前日期命名报告文件
$Reportfile = "C:\" + (Get-Date -uformat "%Y-%m-%d") + ".csv"
$Report = @()

#获取主机列表
$VMHs = Get-VMHost | Sort-Object Name
ForEach ($vmh in $vmhs)
	{
	$myObj = "" | Select-Object HostName, CpuMaxUsage, MemMaxUsage, DiskMaxLatency_ms, DiskMaxUsage_KBps, NetMaxUsage_KBps
	$myObj.HostName = (Get-VMHost -name $vmh).Name
	$myObj.CpuMaxUsage = "{0:N2}%" -f (Get-VMHost -name $VMH | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Cpu.Usage.Average | Measure-Object -Maximum -Property Value).Maximum
	$myObj.MemMaxUsage = "{0:N2}%" -f (Get-VMHost -name $VMH | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Mem.Usage.Average | Measure-Object -Maximum -Property Value).Maximum
	$myObj.DiskMaxLatency_ms = (Get-VMHost -name $VMH | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Disk.maxtotallatency.latest | Measure-Object -Maximum -Property Value).Maximum
	$myObj.DiskMaxUsage_KBps = (Get-VMHost -name $VMH | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Disk.Usage.Average | Measure-Object -Maximum -Property Value).Maximum
	$myObj.NetMaxUsage_KBps = (Get-VMHost -name $VMH | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Net.Usage.Average | Measure-Object -Maximum -Property Value).Maximum
	$Report += $myObj
	Write-Host "$vmh Done!"
	}
#输出主机一周性能信息到报告文件
$Report | Export-Csv -NoTypeInformation -UseCulture -path $Reportfile
"" >> $Reportfile

#获取群集的一周性能信息
$Report = @()
$myObj = "" | Select-Object HostName, CpuMaxUsage, MemMaxUsage
$myObj.HostName = (Get-VM VC01*).Name
$myObj.CpuMaxUsage = "{0:N2}%" -f (Get-VM VC01* | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Cpu.Usage.Average | Measure-Object -Maximum -Property Value).Maximum
$myObj.MemMaxUsage = "{0:N2}%" -f (Get-VM VC01* | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Mem.Usage.Average | Measure-Object -Maximum -Property Value).Maximum
$Report += $myObj
Write-Host "VC01 Done!"

$myObj = "" | Select-Object HostName, CpuMaxUsage, MemMaxUsage
$myObj.HostName = (Get-Cluster OP0*).Name
$myObj.CpuMaxUsage = "{0:N2}%" -f (Get-Cluster OP0* | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Cpu.Usage.Average | Measure-Object -Maximum -Property Value).Maximum
$myObj.MemMaxUsage = "{0:N2}%" -f (Get-Cluster OP0* | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Mem.Usage.Average | Measure-Object -Maximum -Property Value).Maximum
$Report += $myObj
Write-Host "OP0 Done!"

$myObj = "" | Select-Object HostName, CpuMaxUsage, MemMaxUsage
$myObj.HostName = (Get-Cluster OP1*).Name
$myObj.CpuMaxUsage = "{0:N2}%" -f (Get-Cluster OP1* | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Cpu.Usage.Average | Measure-Object -Maximum -Property Value).Maximum
$myObj.MemMaxUsage = "{0:N2}%" -f (Get-Cluster OP1* | Get-Stat -Start ((Get-Date).adddays(-7)) -Finish (Get-Date) -Stat Mem.Usage.Average | Measure-Object -Maximum -Property Value).Maximum
$Report += $myObj
Write-Host "OP1 Done!"

#输出到报告文件
$Report | ConvertTo-Csv -NoTypeInformation >> $Reportfile
"" >> $Reportfile

#获取存储的使用情况,忽略以local和datastore命名的存储(本地存储的命名,根据情况修改)
Get-Datastore | Where-Object {$_.Name -notmatch "local|datastore"} | Select Name,@{N="TotalSpace_TB";E={"{0:N2}" -f ($_.ExtensionData.Summary.Capacity/1024/1024/1024/1024)}},@{N="ProvisionedSpace_TB";E={"{0:N2}" -f (($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1024/1024/1024/1024)}},@{N="FreeSpace_TB";E={"{0:N2}" -f ($_.ExtensionData.Summary.FreeSpace/1024/1024/1024/1024)}} | Sort-Object Name | ConvertTo-Csv -NoTypeInformation >> $Reportfile
Write-Host "Datastore Done!"

# 断开连接
Disconnect-VIServer -Confirm:$False
 
  

--------------------
通过smtp服务器发送邮件
 
  
#指定报告附件
$Reportfile = C:\report.csv
#指定SMTP服务器地址
$smtpServer = "Mail_Server_Address"

#创建邮件对象、附件
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($Reportfile)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#设置发件人地址、收件人地址
$msg.From = "Sender_Mail_Address"
$msg.To.Add("Recipient_Mail_Address")
#设置主题、正文,添加附件
$msg.Subject = "VMware Report_" + (Get-Date -uformat "%Y-%m-%d")
$msg.Body = "附件为vSphere性能检查报告,请查收"
$msg.Attachments.Add($att)
#发送邮件
$smtp.Send($msg)
 
  

----------------
查询虚拟机创建事件(前提是日志信息在有效期内未被清理),可获取虚拟机创建者信息,创建日期
 
  
Get-VM $vmname | Get-VIEvent -Types Info | Where { $_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent" -or $_.Gettype().Name -eq "VmClonedEvent"}
 
  

---------------
检查所有虚机的内存和CPU预留情况
## 给定虚机的内存预留情况
 
  
Get-Cluster “myCluster” | Get-VM | Get-VMResourceConfiguration | select VM,MemReservationMB
 
  
## 给定虚机的CPU预留情况
 
  
Get-Cluster “myCluster” | Get-VM | Get-VMResourceConfiguration | select VM,CPUReservationMhz
 
  
设置所有虚机的内存或CPU预留
## 设置给定虚机的内存预留为0
 
  
Get-Cluster “myCluster” | Get-VM | Get-VMResourceConfiguration | Set-VMResourceConfiguration -MemReservationMB 0 -Confirm:$false
 
  
##设置给定虚机的CPU预留为0
 
  
Get-Cluster “myCluster” | Get-VM | Get-VMResourceConfiguration | Set-VMResourceConfiguration -CPUReservationMhz 0 -Confirm:$false
 
  

----------------------------
利用PowerCLI修改虚拟机的硬件版本
 
  
Set-VM -VM vm01 -version v9 -confirm:$false
 
  

----------------------------
利用PowerCLI将文件传输到VM里去
 
  
$vm=Get-VM -Name vmname
Get-Item "C:\Temp\*.*" | Copy-VMGuestFile -Destination -"C:\Temp\" -VM $vm -LocalToGuest -HostUser root -HostPassword password -GuestUser administrator -GuestPassword guestpassword
 
  

----------------------------
利用PowerCLI创建NFS数据存储
 
  
Get-Cluster clustername | Get-VMHost| New-Datastore -Nfs -Name NFS01 -Path "/mnt/NFS01/" -NfsHost 192.168.1.101