PowerShell 管理VMWare

VMWare 提供了一套以VCenter为核心的虚拟化解决方案.操作VCenter的方式有很多,最常见的就是使用vSphere直接手动操作VCenter,实现创建DataCenter,Datastore,Cluster及Host/VM 等,这些操作其实都提供了相应的API供应用程序调用。API主要分为三类:检索,创建和删除,监测。


上图就是vSphere的客户端,这里我们看到的是主机和群集的模式,其实还有其他模式,如:网络模式,模版模式等等。

使用PowerShell可以很方便地管理这些对象(DataCenter,Cluster,Datastore等等),如果对PowerShell不了解,请参考

http://www.pstips.net/powershell-online-tutorials ,这里有非常详细的powershell教程

或者参照我之前的CSDN博客,里面也讲了很多关于powershell的东西。

使用PowerShell管理VMWare有两个前提条件:

1.安装VMware-PowerCLI组件,最新版本6.0,官方下载地址:https://my.vmware.com/cn/web/vmware/details/sdkwin41/ZHcqYmRwJXdidyVk

当然,您可能需要相关文档,官方下载:https://www.vmware.com/support/developer/PowerCLI/

2.安装PowerShell(一般操作系统WIN7,WIN8都自带了)

PS:建议安装一个PowerGUI,方便开发!

下面就是我整理的关于Powershell管理VMWare的一些函数

#Connect to VCenter

[string]$vSphere = "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts"
[string]$IP = "xxx.xxx.xx.xx"
[string]$User = "administrator"
[string]$Password = "xxxxxx"
$Global:DefaultVCenter = $null

cd $vSphere
./Initialize-PowerCLIEnvironment.ps1

function Init
{
	$DefaultVCenter = Connect-VCenter $IP $User $Password
}

function Connect-VCenter
{
	param ($IP, $User,$Password)
	return Connect-VIServer -Server $IP -Protocol https -User $User -Password $Password
}

function Set-DefaultVCenter($VCenter)
{
	$Global:DefaultVIServer = $VCenter
}

function Get-VirtualResource
{
	param($VMName)
	
	if($VMName -eq $null) { $VMs = Get-VM }
	else { $VMs = Get-VM -Name $VMName }
	
	foreach($item in $VMs)
	{
		Write-Host "current vm is "$item.Name
	}
	
	return $VMs
}

function Get-Datacenters
{
	param($DatacenterName)
	
	if($DatacenterName -eq $null) { $Datacenters = Get-Datacenter }
	else { $Datacenters = Get-Datacenter -Name $DatacenterName }
	
	foreach($item in $Datacenters)
	{
		Write-Host "Current Datacenter is "$item.Name
	}
	
	return $Datacenters
}

function Get-Datastores
{
	param($DatastoreName)
	
	if($DatastoreName -eq $null) { $Datastores = Get-Datastore }
	else { $Datastores = Get-Datastore -Name $DatastoreName }
	
	foreach($item in $Datastores)
	{
		Write-Host "current datastore is "$item.Name
	}
	
	return $Datastores
}

function Get-Clusters
{
	param($ClusterName)
	
	if($ClusterName -eq $null) { $Clusters = Get-Cluster }
	else { $Clusters = Get-Cluster -Name $ClusterName }
	
	foreach($item in $Clusters)
	{
		Write-Host "current cluster is "$item.Name
	}
	
	return $Clusters
}

function Get-VMHosts
{
	param($VMHostName)
	
	if($VMHostName -eq $null) { $Hosts = Get-VMHost }
	else { $Hosts = Get-VMHost -Name $VMHostName }
	
	foreach($item in $Hosts)
	{
		Write-Host "current host is "$item.Name
	}
	
	return $Hosts
}

function Get-VMGuests
{
	param($VMGuestName)
	$vm = Get-VirtualResource $VMGuestName
	$Guest = Get-VMGuest -VM $vm
	Write-Host $Guest.OSFullName
	#Write-Host $Guest.IPAddress
	return $Guest
}

function Get-ResourcePools
{
	param($PoolName)
	
	if($PoolName -eq $null) { $Pools = Get-ResourcePool }
	else { $Pools = Get-ResourcePool -Name $PoolName }
	
	foreach($item in $Pools)
	{
		Write-Host "current pool is "$item.Name
	}
	
	return $Pools
}

function Stop-VirtualMachine($vName)
{
	$vm = Get-VirtualResource $vName
	#WARNING:This way need to confirm before shutdown
	#Stop-VM -VM -Confirm 
	#This way will shutdown with no confirm
	Stop-VM -VM $vm -Kill -Confirm:$false
}

function Start-VirtualMachine($vName)
{
	$vm = Get-VirtualResource $vName
	Start-VM -VM $vm
}

function Stop-VirtualMachineGuest($vName)
{
	$vm = Get-VirtualResource $vName
	#WARNING:This way need to confirm before shutdown
	#Stop-VM -VM -Confirm 
	#This way will shutdown with no confirm
	Stop-VMGuest -VM $vm -Kill -Confirm:$false
}

function Create-VM($target,$vName)
{
	$myCluster = Get-Clusters $target
	New-VM -Name $vName -ResourcePool $myCluster
}

function Delete-VM($vName)
{
	$vm = Get-VirtualResource $vName
	Remove-VM -VM $vm -DeletePermanently -Confirm:$false -Server $DefaultVCenter 
}

function Reboot-VM($vName)
{
	try{
		$vm = Get-VirtualResource $vName
		Restart-VM -VM $vm -Confirm:$false -Server $DefaultVCenter 
		return $true
	}
	catch{
		return $false
	}
}

CLEAR
Init
#Get-VirtualResource 
#Get-Datacenters
#Get-Datastores
#Get-Clusters
#Get-VMHosts 
#Start-VirtualMachine "MyVM5"
#Stop-VirtualMachine "CNSHJSV777702"
#Get-ResourcePools
#Get-VMGuests "CNSHJSV777702"
#Create-VM "PRD-380G8-N01" "MyVM5"
#Delete-VM "MyVM2"
#Reboot-VM "CNSHJSV777702"
#Write-Host $DefaultVCenter.Name
说明:$vSphere是PowerCLI的安装目录,Initialize-PowerCLIEnvironment.ps1文件是初始化加载类库的文件.

关于Datastore的测试结果如下:

current datastore is  datastore2
current datastore is  nfs1
current datastore is  nfs2
current datastore is  nfs1
current datastore is  datastore1
current datastore is  nfs2
注意:连接VCenter使用的是ssl连接,最好安装下证书


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值