PowerCLI One-Liners to make your VMware environment rock out!

June 14th, 2013
by Christopher Kusek (PKGuild)

So I hadn’t touched PowerCLI in a long while (Hey, I hadn’t touched the CONSOLE OF ANYTHING near production after having been in management for so long!   So, the first thing I decided to do was, “Well, what data is important to me… and what will make my life easier!”   Below you’ll see examples of some of those very scenarios, One-Liners and collections of data points! If you haven’t worked with PowerCLI this is a good way to get started.  I’ll also explain WTF I did and why, so you have some good logic and reasoning behind why to use some of these measures!  Also if you happen to have any really cool one-liners and scripts you’ve used, feel free to toss them into the comments!

Let’s start with … Well, getting started!

Launch PowerCLI CMDLine as an elevated user (This is especially important if you have a different administrative acct than your login)

Connect-VIServer

// You can paste in all of the vCenter Names in order to execute a ‘command’ against all of them.

Example, You can simply launch Connect-VIServer, hit enter and then paste a list of vCenters to connect to.  This is especially important if you happen to be managing more than one vCenter.

Get-VMHost | Get-VMHostNetwork | Select Hostname, VMKernelGateway -ExpandProperty VirtualNic | Where {$_.ManagementTrafficEnabled} | Select Hostname, PortGroupName, IP, SubnetMask

// This will then dump the ESXi Hostnames, IPs and Subnets – For the Management Network

Get-VMHost | Get-VMHostNetwork | Select Hostname, VMKernelGateway -ExpandProperty VirtualNic | Where {$_.vMotionEnabled} | Select Hostname, PortGroupName, IP, SubnetMask

// This will then dump the ESXi Hostnames, IPs and Subnets – For the vMotion Network

Get-VMHost | Get-Cluster | Select Name, DrsEnabled, DrsMode, DrsAutomationLevel

// Dump DRS Status

Get-VMHost | Get-Cluster | Select Name, VMSwapfilePolicy

// Dump VMSwapfilePolilcy

Get-VMHost | Get-Cluster | Select Name, HAAdmissionControlEnabled

// Check status of HA Admission Control

Get-VMHost | Get-Cluster | Select Name, HAFailoverLevel, HARestartPriority, HAIsolationResponse

// Check HA Status Levels

Get-VMHost | Get-VMHostNetwork | Select Hostname, VMKernelGateway -ExpandProperty VirtualNic | Select Hostname, PortGroupName, IP, MTU

// Check for MTU Mismatches

Get-VirtualSwitch | Select VMHost, Name, MTU

// Shows what the MTU settings on the Virtual Switches are

Get-VMGuestNetworkInterface –VM VMNAME | Select VM, IP, SubnetMask, DefaultGateway, Dns

// Dumps a hosts Name, IP, Subnet, Gateway and DNS configuration

Append ‘| Export-Csv “c:\location\filename”’

// This will allow you to export the results to a CSV file  – This is called out so you’re aware of the syntax to do CSV type exports!

Get-VMHost | Get-ScsiLun | Select VMHost, ConsoleDeviceName, Vendor, MultipathPolicy

// This will dump the Multipath Policy of the storage on the systems to determine what the MP configuration is.

Get-VMHost | Get-ScsiLun | Select VMHost, ConsoleDeviceName, Vendor, MultipathPolicy | Where {$_.Vendor –eq “NETAPP”} | Select VMHost, ConsoleDeviceName, Vendor, MultipathPolicy

// This will dump the Multipath Policy of ONLY NetApp systems

Get-VMHost | Get-ScsiLun | Select VMHost, ConsoleDeviceName, Vendor, Model, LunType, MultipathPolicy | Export-CSV “C:\temp\MultipathPolicyFull.csv”

// This will dump the Multipath Policy into a CSV as it’ll be a tad bit longer with multiple attributes specified!

Get-ScsiLun –Hba [software iSCSI HBA] | Set-ScsiLun –MultipathPolicy “RoundRobin”

// You can use these parameters to change the LUNs from Fixed to RoundRobin

e.g.) Get-ScsiLun –Hba vmhba39 | Set-ScsiLun –MultipathPolicy “RoundRobin”

Get-VMhost | Get-SCSILun | Where {$_.Vendor –EQ “NETAPP”} | Select VMHost, Vendor, MultipathPolicy

// Identify the Netapp LUNs

Get-VMhost | Get-SCSILun | Where {$_.Vendor –EQ “NETAPP”} | Where {$_.MultipathPolicy -EQ "Fixed"} | Select VMHost, Vendor, MultipathPolicy

// Identify the Netapp LUNs which are “Fixed”

Get-VMhost | Get-SCSILun | Where {$_.Vendor –EQ “NETAPP”} | Set-SCSILun –MultipathPolicy “RoundRobin”

// Set the NetApp LUNs to RoundRobin

Get-VMhost | Get-SCSILun | Where {$_.Vendor –EQ “NETAPP”} | Where {$_.MultipathPolicy -EQ "Fixed"} | Set-SCSILun –MultipathPolicy “RoundRobin”

Get-VMHost | Sort Name | Select Name, @{N=”NTP”;E={Get-VMHostNtpServer $_}}

// This will dump NTP Configuration settings

Get-VMHost | Get-View | foreach {$_.Summary.Hardware.OtherIdentifyingInfo[3].IdentifierValue}

// This will dump the Dell Service Tags

Get-VMHost | Get-View | Select Name, @{N=”Service Tag”;E={$_.Summary.Hardware.OtherIdentifyingInfo[3].IdentifierValue}}

// This will dump the Host name and the Dell Service Tag

Get-VMHost | Sort Name | Get-View | Select Name, @{N=”Tag 3”;E={$_.Summary.Hardware.OtherIdentifyingInfo[3].IdentifierValue}}, @{N=”Tag 2”;E={$_.Summary.Hardware.OtherIdentifyingInfo[3].IdentifierValue}}, @{N=”Tag 1”;E={$_.Summary.Hardware.OtherIdentifyingInfo[3].IdentifierValue}}

// This will dump the Host name and the Dell Service Tag values across all 3 identifiers

Get-View -ViewType HostSystem | Sort Name | Select Name,@{N="BIOS version";E={$_.Hardware.BiosInfo.BiosVersion}}, @{N="BIOS date";E={$_.Hardware.BiosInfo.releaseDate}}

// This will dump the hosts BIOS version and date(s)

get-vmhost | Get-VMHostAdvancedConfiguration -Name Syslog.global.logHost

// Dump the current SYSLOG Configuration

You may notice that a lot of the Scripts identified in here are very selfishly scripts I’ve personally used… and I’ll tell you… that’s not all that bad ;)   I figure as time goes on, I’ll find other various switches and flags which are important and others ought to check out!  I’m constantly building and adding to this list as there are various scripts I’ll be running on a daily, weekly and monthly basis.   As I start to identify which items fall into the lists I’ll share my experiences with ya’ll here! Enjoy!

Tags: , , , , ,
Posted in PowerCLI, virt, Virtualization, vmware, vSphere | Comments (2)

  • I thought about updating this with additional scripts… but I thought.. I’ll just add them into the comments! ;) so here they go!

    Get-VM | Get-Snapshot | Select VM,Name,Created,sizemb

    // Dump the Snapshot status

    Get-VM | Get-View | Select Name, @{N=”ToolsVersion”;E={$_.config.tools.toolsVersion}}

    // Dump Tools version

    Get-NaSnapmirror | select *,@{name=”TransferRate(Mb/s)”;e={“{0:N2}” -f ($_.LastTransferSize * 8 / 1024 / 1024 / $_.LastTransferDuration)}}

    // Here is a quick one liner to return the Snapmirror Transfer Rate along with the other Snapmirror info:

    Get-VM | Get-HardDisk | where{$_.Persistence -eq “IndependentPersistent”} | Select FileName, Persistence

    // Find Persistent Disks listed as “Independent”

    Get-VM | Where {$_.PowerState -eq “PoweredOff” } | Where {$_.Version -ne “v8” } | Where {$_.HardDisks -ne “{}” } | Select Name, Version, PowerState

    Get-VM | Where {$_.PowerState -eq “PoweredOn” } | Where {$_.Version -eq “v4” } | Where {$_.HardDisks -ne “{}” } | Select Name, Version, PowerState

    SYSLOG

    Get-VMHost | Get-VMHostAdvancedConfiguration -Name “Config.HostAgent.log.level”

    Get-VMHost | Get-VMHostAdvancedConfiguration -Name “Vpx.Vpxa.config.log.level”

    // This will dump the current log level settings (may be Verbose)

    Get-VMHost | Set-VMHostAdvancedConfiguration -Name “Config.HostAgent.log.level” -Value “warning”

    Get-VMHost | Set-VMHostAdvancedConfiguration -Name “Vpx.Vpxa.config.log.level” -Value “warning”

    // This will set the current log level settings to Warning

    Setting Values in the SYSLOG

    #Get Each Host Connected to the vC

    foreach ($myHost in get-VMHost)

    {

    #Display the ESXi Host that you are applying the changes to

    Write-Host ‘$myHost = ‘ $myHost

    #Set the Syslog LogHost

    Set-VMHostAdvancedConfiguration -Name Syslog.global.logHost -Value ‘server.domain.com’ -VMHost $myHost

    #Use Get-EsxCli to restart the syslog service

    $esxcli = Get-EsxCli -VMHost $myHost

    $esxcli.system.syslog.reload()

    #Open the firewall on the ESX Host to allow syslog traffic

    Get-VMHostFirewallException -Name “syslog” -VMHost $myHost | set-VMHostFirewallException -Enabled:$true

    }

    Get-VMHost | Get-VMHostNetworkAdapter | Select VMHost, Name, BitRatePerSec

    // Dump the hosts BitRates to determine if any hosts are ‘offline’ with –eq “0”

  • Rasmus Haslund

    Loving these PowerCLI one liners.
    Makes me want to learn even more PowerCLI and Powershell for that matter.

  • Archives