PowerCLI Alternative to Linux Watch Command

Using the watch command from the ESXi command line is a great way to monitor snapshot removal, as shown in this KB article.

# watch -d 'ls -luth | grep -E "delta|flat|sesparse"'

The command above will display, and refresh every 2 seconds, a listing of all the files with delta, flat or sesparse in the filename. This means we can monitor the files easily, taking note of the last modified time stamp, as snapshots are removed.

I use this command reasonably often, and it’s great for determining whether the snapshot removal is working as expected, but the downside is that you need to be able to SSH to the host in order to run the command which often means needing to enable the service. This got me thinking as to whether you could achieve the same thing using PowerCLI, without connecting directly to a host.

Using PS-Drives we can browse our datastores to get a file listing. So, for example, after connecting the PowerCLI session to vCenter, running the following command will allow you to browse the VMFS datastores from PowerCLI:

c:\>cd vmstore:\

You can then browse to your virtual machines directory, so in my example:

vmstore:\ha-datacenter\datastore1\Centos>

Running the Get-Childitem cmdlet here will list all the virtual machines files:

PowerCLI vmstore:\ha-datacenter\datastore1\Centos> get-childitem

   Datastore path: [datastore1] Centos

            LastWriteTime            Type       Length Name
            -------------            ----       ------ ----
     19/12/2013     19:49    VmConfigFile         2910 Centos.vmx
     19/12/2013     19:49      VmDiskFile          542 Centos.vmdk
     28/11/2013     09:21     VmNvramFile         8684 Centos.nvram
     10/10/2013     16:02       VmLogFile       240259 vmware-1.log
     10/10/2013     16:32       VmLogFile       201730 vmware-2.log
     10/10/2013     20:12       VmLogFile       122177 vmware-3.log
     28/11/2013     20:04       VmLogFile       136362 vmware.log
     28/11/2013     09:14            File          261 Centos.vmxf
     19/12/2013     19:49            File           43 Centos.vmsd
     28/11/2013     20:04            File  17179869184 Centos-flat.vmdk
     28/11/2013     09:13            File            0 Centos.vmx.lck
     19/12/2013     19:49            File           13 Centos-aux.xml

However, we are only interested in the delta/flat files, so we can run:

PowerCLI vmstore:\ha-datacenter\datastore1\Centos> get-childitem | where {$_.name -like "*flat*" -or $_.name -like "*delta*"}

   Datastore path: [datastore1] Centos

            LastWriteTime            Type       Length Name
            -------------            ----       ------ ----
     28/11/2013     20:04            File  17179869184 Centos-flat.vmdk

Finally, we can use the While command to mimic the Watch command:

PowerCLI vmstore:\ha-datacenter\datastore1\Centos> while ('0' -eq '0') {get-childitem | where {$_.name -like "*flat*" -or $_.name -like "*delta*"};sleep 2;cls}

This will display all the files with ‘flat’ or ‘delta’ in their name, refreshing every 2 seconds. Taking a snapshot whilst the command is running, we can see that another file is listed:

The ‘While’ command isn’t only limited to monitoring snapshot removal and creation though. Another task I’ve used it for recently was to monitor connections to a vCenter server. For example, to see a list of connections to the server on a particular port, refreshing every few seconds you could run something like:


while ('0' -eq '0') {netstat -an|findstr 80; sleep 5;cls}

Related posts

A Beginner’s Guide to the Netstat Command

How to Concatenate Strings with Powershell

How To Move a File Using Powershell

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More