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


Start-Sleep 5

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! ^_^