Powershell is COUNTING on you – just WATCH – and listen

These are NOT power programming techniques, but sometimes good enough is … good enough.

First, i want to time something.  I can $start =  get-date and $finish = $get-date and do a diff on it.  And even put a -format or -displayhint date/time/datetime or something similar.

There is also a dignostics.stopwatch method in dotNet.  You evoke it and start a stopwatch this way:

[System.Reflection.Assembly]::LoadWithPartialName(“System.Diagnostics”)
$sw = new-object system.diagnostics.stopwatch
$sw.start()

At this point, the Royal Smart Programmer would call get-member on $sw to see what else you can do.  Short-cut:  $sw.Stop() or $sw.Reset() to stop or clear the stop watch.  If you get the elapsed time (which is a property of $sw.elapsed), and $sw.start again, you continue to increase the value.  (I presume that while stopped, the timer does not increment as get-date would). You can also extend the .elapsed property to

$sw.elapsed.seconds

to get the number of seconds in the counter.  Or ticks.

Now, let’s COUNT something.  I am archiving files in Blackboard.  I am writing the archives (ok, so i am NOT, the process is — but i do control where the archives are written) to a network drive.  I can see it from my desktop.  So i can do a dir on that directory and and get the length – which is the number of archives.  This does not include . or ..  .  If this operation is inside a loop, i can get and output or test the value of the length, sleep and do it again.  Until i notice that the right number is reached, or that it is not getting any larger.

Hey – what if i ring a bell at that point!

$sound = new-Object System.Media.SoundPlayer;
$sound.SoundLocation="c:\WINDOWS\Media\notify.wav";
$sound.Play();

I can ring it once and change the loop status and leave, or after the right value, i can continue to loop and play the sound every number of sleep seconds, or be really mean and shorten the sleep and play the sound insistently. I decided to just keep running until i manually stop the program

$status = $true
do
{
get-date
$xdrive = dir "X:\qu10SP"
$xdrive.length
if ($xdrive.length -gt 68){
$sound = new-Object System.Media.SoundPlayer;
$sound.SoundLocation="c:\WINDOWS\Media\notify.wav";
$sound.Play();
}
Start-Sleep -s 600
} while ($status)
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.