powershell help

AlNipper49

Huge Member
Dope
Apr 3, 2001
40,489
Mtigawi
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
 

krobe

Member
SoSH Member
Apr 4, 2006
122
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
 

AlNipper49

Huge Member
Dope
Apr 3, 2001
40,489
Mtigawi
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
 

AlNipper49

Huge Member
Dope
Apr 3, 2001
40,489
Mtigawi
got it - you are magic!!!

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