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

Disconnect-VIServer VcenterServer

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
$file+="NAME^FQDN^DOMAIN^OS^POWER`r"
$output="C:\temp\VM_List_" + $vcenter + ".csv"
#$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
$domain=$i.ExtensionData.Guest.IpStack.DnsConfig.DomainName
$os=$i.Guest.OSFullName
$power=$i.PowerState
write-host "$i^$fqdn^$domain^$os^$power"
$file+="$i^$fqdn^$domain^$os^$power`r"
}
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.

$file+="VCENTER^NAME^FQDN^DOMAIN^OS^POWER^ESXHOST`r"
$now=Get-Date -format yyyyMMddHHmmss
$output="C:\temp\VM_List_All_$now.csv"
$vcenters=@("vcenter servers")
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}
}
$vms=Get-VM | where {$_.guest -match "Microsoft Windows Server"}
foreach($vm in $vms) {
$fqdn=$vm.ExtensionData.Guest.HostName
$domain=$vm.ExtensionData.Guest.IpStack.DnsConfig.DomainName
$os=$vm.Guest.OSFullName
$power=$vm.PowerState
$esxhost=$vm.VMHost
write-host "$vcenter^$vm^$fqdn^$domain^$os^$power^$esxhost"
$file+="$vcenter^$vm^$fqdn^$domain^$os^$power^$esxhost`r"
}
Disconnect-VIServer -Server $vcenter -Confirm: $false
}
$file | out-file $output

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

Some others:

(Get-VM -Name VM).ExtensionData.Guest.HostName

(Get-VM -Name VM | Get-VMGuest).ExtensionData.hostname

Comments