Dismiss Notice
Guest, I have a big favor to ask you. We've been working very hard to establish ourselves on social media. If you like/follow our pages it would be a HUGE help to us. SoSH on Facebook and Inside the Pylon Thanks! Nip

powershell help

Discussion in 'BYTE ME: Technology discussion' started by AlNipper49, Sep 13, 2017.

  1. AlNipper49

    AlNipper49 Huge Member Dope

    Messages:
    40,202
    I don't know powershell at all, nor am I a programmer. This is for sosh so I figured I'd ask here.

    Every night I pull down a database backup of sosh. I'd like to write a powershell script that will return the hours since the last file appeared in that directory, which I can then pipe into my monitoring system. I've gotten as far as getting the date of the most recent file using this:

    Code:
    gci e:\backup\sosh\db | sort LastWriteTime | select -last 1 | select LastWriteTime 
    ^
    |---this works perfectly

    In my stupid programming mind this is what my logical next step would say (but this obviously does not work)

    Code:
    $startdate=gci e:\backup\sosh\db | sort LastWriteTime | select -last 1 | select LastWriteTime
    $enddate=get-time
    $difference=TimeDifference($endate,$startdate)
    $what-nip-wants=ToHours($difference)[code]
    
    ^
    |---this obviously does not work
     
  2. krobe

    krobe Member SoSH Member

    Messages:
    120
    try
    $startdate=gci e:\backup\sosh\db | sort LastWriteTime | select -last 1 | select -ExpandProperty LastWriteTime
    $difference = New-TimeSpan -Start $startdate
    $what-nip-wants = $difference.TotalHours
     
  3. AlNipper49

    AlNipper49 Huge Member Dope

    Messages:
    40,202
    nailed it!!!! @krobe

    Code:
    $startdate=gci e:\backup\sosh\db | sort LastWriteTime | select -last 1 | select -ExpandProperty LastWriteTime
    $difference = New-TimeSpan -Start $startdate
    $good = $difference.TotalHours
    $good
    works perfectly!!! (I had to change what-nip-wants, it didn't like the dashes).

    One follow-up question -- do you know how to not print the LastWriteTime? Right now it's spitting out

    Code:
    PS Z:\> z:\soshdate
    0.305036793722222
    
    LastWriteTime
    -------------
    9/13/2017 9:24:07 AM
    
     
  4. AlNipper49

    AlNipper49 Huge Member Dope

    Messages:
    40,202
    got it - you are magic!!!

    Code:
    $test=New-TimeSpan -Start $(gci e:\Download |sort LastWriteTime | select -last 1 |select -ExpandProperty LastWriteTime ) | %{ $_.TotalHours}
    echo $test
     

Share This Page