PowerCLI one-liners - Part 3


After several months of working on PowerCLI scripting and many more vSphere queries I thought appropriately enough another round of PowerCLI one-liners were appropriate.  Take note that these start with connect-viserver vCenterserver  

- This one searches your vm guest list for a specific IP and displays the hostname.
$($($(get-vm | get-view).guest | ? Ipaddress -match 10.12.160.13).Hostname
This one was used when I had a client that had a large environment with multiple vSphere environments, this was perfect to find if the IP happened to be within the specific vSphere environment.

- With the introduction of PowerNSX, Nick Branford brings NSX scripting to the forefront. Get the PowerNSX here - https://powernsx.github.io/
With more and more NSX deployments happening, the more a scripting presence is needed  :)
Connecting to NSX
connect-nsxserver -credential $(Get-credential) -vCenterServer vCenterServer 
Pretty simple to connect to the NSX-Server using the vCenter Server credentials

- If one is using the distributed firewall, and a check of the rules is a needed function.  What I found is that if one desires to pull the configuration in XML format it doesn't include a good portion of the security groups, ip sets, mac sets, service groups and services.  This configuration dump is much longer than a one-liner, so I will only list a couple of the commands to help in the comparison.
$NSXFWRule = Get-NSXFirewallRule 
 write "Name is $($NSXFWRule.Name) and its ID is $($NSXFWRule.ID)"

This lists the firewall rules outside of the default section Layer3

- With the invent of PowerCLI v. 6.3 the whole version moved to modules.  So I began doing this at the start of all my scripts
Get-Module -listavailable | ? Name -match "VMware" | Import-Module

The nice part is that if you run this in the ISE or from a basic PowerShell window, it adds the appropriate modules to query your vSphere environment.

- Setting credentials each and every run is painful and unnecessary.  So I started running this at the beginning of all my scripts. I have been using a read-host to confirm if the current credentials are desired to be used.
If ($global:cred) {
  Switch ($(Read-Host "Use existing credentials $($Global:cred.username)?>")){
        n {$Global:cred = Get-Credential}
    Default{Write-Host -foregroundcolour "Green" "Using $($Global:cred.Username)"
  }

Although this doesn't look like a one-liner, it is and it does help me save some time running the script during the debugging stage of the creation.  Ideally, there should be another if statement after that catches if there is no $Global:Cred variable. 
If (!$Global:Cred) {$Global:Cred = Get-Credential}


- Get uptime on a windows server (shows last boot time)
$(invoke-command -scriptblock {net statistics server} | ? {$_ -match "Statistics since"}) -replace "Statistics since "


- Run a Tail on a log file with a specific query search
Get-Content <filename> -wait -tail 10 | ? {$_ -match "Query"}

More to come...



Comments