Saturday, 5 May 2018

One-Liner PowerShell to Record/Log CIFS Share Access Availability

I like one liners where you don’t need a PowerShell script/PS1 file. It might be a bit of a cheat using semi-colons, but you can still paste it into your PowerShell and run it as a one liner.

This one liner can be used to test availability of a CIFS share. Change the path variable as required.

while($TRUE){$T1 = Get-Date; $P = Test-Path "\\192.168.0.120\vol1"; $T2 = Get-Date; $TS = (New-TimeSpan -Start $T1 -End $T2).Seconds; If($TS -gt 1){"DOWN for $TS seconds" >> log.txt}; $D = Get-Date -uformat %Y%m%d; $T = Get-Date -uFormat %T; "$D $T $P" >> log.txt; sleep 1}

It will run until you Ctrl+C to exit. This is what it does:

- Gets a time T1
- Tests the path to the CIFS share (Test-Path has a long timeout)
- Gets a time T2
- If T2 minus T1 is greater than 1 second we log “DOWN for $TS seconds”
- Finally, we log a formatted date with the result of Test-Path
- Sleep for 1 second and repeat

Image: Screenshot of the log showing DOWN caused by intentionally downing the data LIF (IP) in NetApp ONTAP clustershell

No comments:

Post a Comment