Category Archives: Scripts

Scripting in action

Daily health check script for VMAX storage

One of the challenge for our cloud team is daily checks. Not coming from a storage background, we did our daily checks by logging on to the storage web gui daily. Interestingly, there is almost no daily check scripts to be found. However, I found a reference in the EMC forum a list of commands to run for a generic health checks.

symcfg list -sid $vmax_sid -env_data -v
symcfg list -sid $vmax_sid -dir ALL
symdev -sid $vmax_sid list -v
symevent -sid $vmax_sid list -error -fatal
symdisk list -sid $vmax_sid -failed

The next task now is to put these into a health check script and figure out the output messages to check for errors.  In general, you just need to check for devices that reports degraded, failed, error or one of standard errors. However for the symdisk command, the execution is different:

symdisk list -sid $vmax_sid -failed

Read the rest of this entry »

Leave a comment

Posted by on July 6, 2018 in powershell


Tags: , , , , , ,

PS: Continuous ping of a list of VMs

Generally, continuous pings are very easy, we can even do it from Windows command prompt. However, recently, I need a more generic script used to ping a list of VMs and color code status and it expanded from there to include other stuff I want to with the script.

Note that the script is used to ping a set of VMs in my environment that is why the input CSV has VMHost as a column. You can always change this to suit your needs.

Param (
  [ValidateScript({If(Test-Path $_){$true}<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>else{Throw "Invalid file given: $_"}})]

$ListOfVMs = import-csv $inputfile
if ($first -eq 0) {$first = $ListOfVMs.count}
$ListofVMs = $ListofVMs | select -first $first -skip $skip
do {

  Write-Host "Checking Status ... "
  Write-Host " "

  foreach ($VM in $ListOfVMs) {
    $VMHost = $VM.VMHost
    $VMName = $VM.Name
    $IPAddress = $VM.IPAddress

    if ($v) {
       Write-Host "Pinging... $VMHost : $VMName with $IPAddress" -ForegroundColor yellow
       Test-Connection $IPAddress -EA SilentlyContinue
    else {
       if (Test-Connection $IPAddress -count $count -quiet -EA SilentlyContinue) {
         Write-Host "$VMHost : $VMName with $IPAddress is UP" -foregroundcolor Green
       else {
         Write-Host "$VMHost : $VMName with $IPAddress is DOWN" -foregroundcolor Red


  Write-Host " "

  if ([console]::KeyAvailable) {
    $x = [System.Console]::ReadKey()
    if ($x.key -match "Q") {
	  Write-Host "Keystroke Q detected, quiting loop"

  if ($loop -and !$Endloop) {
    Write-Host "Continuous ping enabled, press Q to end loop ... "
    Write-Host " "
	sleep 1

} while($loop -and !$Endloop)

Write-Host " "

Leave a comment

Posted by on April 20, 2018 in powershell



PS: One-line create a custom object list

The one-liner challenge for me today:

  • Get a list of random VMs from each ESXi host in a cluster
  • Send the host name, vm name and IP address to a CSV file

Normally, this is straight forward, but because we only need grab 2 VMs from each host, it is not possible to just  pipe the VM objects into a CSV file since the loop breaks between each host. The solution is to store all records into a array of custom objects and then pipe that into a CSV file at the end.

$ListOfVMs = @() ;
get-cluster | ? { $ -match "cluster1" } |
get-vmhost | % { $_ | get-vm | ? { $ -match "dev" } |
select -first 2 |
% { $t = @{} |
    select VMhost,Name,IPAddress ;
    $t.VMHost = $_.VMHost;
    $t.Name = $_.Name ;
    $t.IPAddress = $_.guest.IPAddress[0] ;
    $ListOfVMs += $t }
} ; $ListOfVMs | export-csv .\mylist.csv -NoTypeInformation


Leave a comment

Posted by on April 18, 2018 in powershell


Tags: , , ,

Formatting XML output XML file

Below is a format-xml script which a lot of helpful scripters posted on the net. You can make a REST API call and use it to indent the output.

For example, you can indent the XML from an NSX REST API call.

#assumming all other pre-requisites for this call is done
$head = #this contains the authorisations string
$Request = "https://nsxmrg/api/3.0/edges"
[xml]$r = Invoke-WebRequest -Uri $Request -Headers $head -ContentType "application/xml" -ErrorAction:Stop


Read the rest of this entry »

Leave a comment

Posted by on April 17, 2018 in powershell


Tags: , ,

PS: Making a generic NSX REST api calls

Here is a script that I picked up and mod to make generic GET calls to NSX via REST api. Why did I wrap the rest api call into a function? This is to allow you to build out other logic for processing the output after making the call. For example, you may want to modify the script to get a list of edges and check their statuses.

   Executes NSX Rest api GET method and returns the XML results
   Use this script to execute rest api calls to NSX
   File Name  : Get-NSXQuery.ps1
   Author     : Not me!!
   Version    : 1.0
   XML formatted object from NSX's REST api call
   FQDN or IP of NSX manager
   api request string, e.g. "/api/3.0/edges"
   secure string credentials from get-credentials
.\Get-NSXQuery.ps1  -nsxmgr nsxmgr_fqdn -reqtext "/api/3.0/edges" -cred $cred

function Get-NSXRestCall {


    Process {

    ### Ignore TLS/SSL errors
    add-type @"
        using System.Net;
        using System.Security.Cryptography.X509Certificates;
        public class TrustAllCertsPolicy : ICertificatePolicy {
            public bool CheckValidationResult(
                ServicePoint srvPoint, X509Certificate certificate,
                WebRequest request, int certificateProblem) {
                return true;
    [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

    ### Create authorization string and store in $head
    $auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Username + ":" + $Password))
    $head = @{"Authorization"="Basic $auth"}

    ### Connect to NSX Manager via API
    $r = Invoke-WebRequest -Uri $Request -Headers $head -ContentType "application/xml" -ErrorAction:Stop
    if ($r.StatusCode -eq "200") {Write-Host -BackgroundColor:Black -ForegroundColor:Green Status: Connected to NSX successfully.}
    else {Write-Host -BackgroundColor:Black -ForegroundColor:Red Status: Error connecting to NSX!}
    return [xml]$r.Content

    } # End of process


if (!$Cred) {
  $Cred = get-credential -Message "Please enter your credentials"

$request = "https://" + $NSXMgr + $reqtext
$rxml = Get-NSXRestCall -request $request -username $Cred.Username -password $Cred.GetNetworkCredential().password

return [xml]$rxml
Leave a comment

Posted by on April 17, 2018 in powershell


Tags: ,

PS: One-liner to check IP range usage

Not as quick as command prompt, but you get more formatting options (if you want)

for ( $i=200; $i -ne 254 ; $i++)
 { $IPAddress = "10.164.144.$i" ; 
   $InUse = test-connection $IPAddress -Quiet -count 1 ; 
    Write-host $IPAddress`t$InUse }

With color coding

for ( $i=200; $i -ne 254 ; $i++)
 { $IPAddress = "10.164.144.$i" ; 
   $InUse = test-connection $IPAddress -Quiet -count 1 ; 
    Write-host $IPAddress -NoNewLine ;
    if ($InUse) {Write-Host `t$InUse -ForegroundColor green} 
     else {Write-host `t$Inuse -ForegroundColor Red}  
Leave a comment

Posted by on April 17, 2018 in powershell



Getting security warnings from PS scripts

One of the pesky problem of trying to run Powershell scripts downloaded from the internet, like from GitHub, is that you always get security warning whether you set execution policy as unrestricted or bypass. This is especially true in security strict companies like a financial institution.

The main reason for this is ADS [Alternate Data Streams]. The fix is to unblock the files

Get-ChildItem -Filter *.ps1 | Unblock-File

This is explained here:

Leave a comment

Posted by on April 15, 2018 in powershell