VMware PowerCLI
Rough notes on connecting to vcenters and getting vm info.
Setup
There's no order to this, you may need to run all of them or none of them.
Get-Module -Name vmware*
Import-Module -Name VMware.PowerCLI
Get-VICommand
Disconnect-VIServer
I've had to use this on some servers, it's some weird cert issue, possible because it's not using ssl.
Get-PowerCLIConfiguration
Set-PowerCLIConfiguration -Scope User -InvalidCertificateAction warn
Connect to VCenter server
When it's looking good, try and connect to a vcenter:
Connect-VIServer VcenterServer -User username -password password
Shows which server you're connected to. | gm has interesting stuff.
$global:DefaultVIServer
Disconnect from VCenter server
Get VMs
If you're connecting to multiple vcenters and getting info, I think you need to disconnect from each one first, otherwise the vm count gets weird.
(get-vm).count
Get powered on VMs
poweredoff is...aah yep.
(Get-VM | where {$_.PowerState -eq "poweredon"}).count
Connect to VCenter and get VMs
Note the where statement.
$file=""$vcenter="VCenterServer"
Connect-VIServer -Server $vcenter -User USERNAME -password PASSWORD
#$a=Get-VM | sort name
#$a=Get-VM | where {$_.PowerState -eq "poweredon" -and $_.guest -match "Microsoft Windows Server"}
$a=Get-VM | where {$_.guest -match "Microsoft Windows Server"}
foreach($i in $a) {
$fqdn=$i.ExtensionData.Guest.HostName
Disconnect-VIServer -Server $vcenter -Confirm: $false
$file | out-file $output
Connect to multiple VCenters and get VMs
Note the where statement. If you put username and password (and you have access) this is very handy.
Connect to multiple VCenters and get VM count
$vcenters=@("vcenter servers")$output=@()
foreach($vcenter in $vcenters) {
switch -wildcard ($vcenter) {
"*domain account 1*" {Connect-VIServer -Server $vcenter -User domain\account -password blah}
"*domain account 2*" {Connect-VIServer -Server $vcenter -User domain\account -password blah}
}$vmcount=(Get-VM | where {$_.guest -match "Microsoft Windows Server"}).count
$output+="$vcenter`: $vmcount"
Disconnect-VIServer -Server $vcenter -Confirm: $false
}
$output
Get-VMGuest
You need vmtools for this.
Options to get FQDN (not all VMs will have it so might be blank).
Criteria: Powered on, Server OS, return FQDN only:
(Get-VM | where {$_.PowerState -eq "poweredon" -and $_.guest -match "Microsoft Windows Server"}).ExtensionData.Guest.HostName
(Get-VM -Name VM).ExtensionData.Guest.HostName
(Get-VM -Name VM | Get-VMGuest).ExtensionData.hostname
Comments
Post a Comment