Friday, March 26, 2010

My first go around with PowerShell

Figured I’d start with math since I was good at it in school…

Well, school was a long time ago!  And I’ve come to find out DateTime math via computers, really isn’t my forte.  What I’m trying to do is setup a simple stop-watch like function.  When I start any of my scripts, I always love to have time involved~~~ somehow, someway…  This particular script (when completed) will give me the ability to set the start time of a script and the end time.  With those variables set, I’ll then be able to do a time difference to determine how long the script ran.  Here’s my first go around:

Function StartTimeD {

    [int] $day = (Get-Date).Day
    Return $day
}

Function StartTimeH {

    [int] $hour = (Get-Date).Hour
    Return $hour
}

Function StartTimeM {

    [int] $minute = (Get-Date).Minute
    Return $minute
}

Function StartTimeS {

    [int] $second = (Get-Date).Second
    Return $second
}

Function EndTimeD {

    [int] $endday = (Get-Date).Day
    Return $endday
}

Function EndTimeH {

    [int] $endhour = (Get-Date).Hour
    Return $endhour
}

Function EndTimeM {

    [int] $endminute = (Get-Date).Minute
    Return $endminute
}

Function EndTimeS {

    [int] $endsecond = (Get-Date).Second
    Return $endsecond
}

 

Function HelloWorld {

    $startday = StartTimeD
    $starthour = StartTimeH
    $startminute = StartTimeM
    $startsecond = StartTimeS
}

Function EndOfTime {

    $endday = EndTimeD
    $endhour = EndTimeH
    $endminute = EndTimeM
    $endsecond = EndTimeS

}

HelloWorld
Start-Sleep 5
EndOfTime

Write-Output $startday $starthour $startminute $startsecond EndTimes are $endday $endhour $endminute $endsecond

New-TimeSpan $(Get-Date -day $startday -hour $starthour -minute $startminute -second $startsecond) $(Get-Date -day $endday -hour $endhour -minute $endminute -second $endsecond)

 
 

Unfortunately for me, it’s not working properly.  The Write-Output spits out the same numbers for both StartTime and EndTime functions.  There’s gotta be a much simpler way…  Maybe there’s something already under the hood for what I’m trying to do.  So far, I haven’t found it, but I am working on it! ^_^

5 comments:

xcud said...

Try this:

$start = get-date

# do something ...
start-sleep 5

$end = get-date
$end - $start

It outputs something similar to this:

Days : 0
Hours : 0
Minutes : 0
Seconds : 5
Milliseconds : 70
Ticks : 50702900
TotalDays : 5.8683912037037E-05
TotalHours : 0.00140841388888889
TotalMinutes : 0.0845048333333333
TotalSeconds : 5.07029
TotalMilliseconds : 5070.29

Unknown said...

Well that was easy... I definitely over thought this one. Thanks for the tip!

Unknown said...

Or, use the provided cmdlet Measure-Command, as
Measure-Command {sleep 5}

Unknown said...

Thanks Larry, your comment has also been very helpful!

Unknown said...

Please join us over in newsgroup microsoft.public.windows.powershell
available on newsserver
news.microsoft.com
or via
http://groups.google.com/group/microsoft.public.windows.powershell/topics?hl=en&lnk