-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check_timestamp.ps1
56 lines (40 loc) · 1.96 KB
/
Check_timestamp.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# __main__
# check timestamp
$TimeStampFileName = "SFTPGO_CONNECTION_ONGOING.txt"
# KEEP_ALIVE_TIME: can be set to make the server alive (not going sleep).
# Normally a movie is 2-hours long. So I set the time as 2.5hr (150min)
#
$KEEP_ALIVE_TIME = "150" #unit: minutes
# Check if file existed, if no, means no connection ongoing, exit.
$TimeStampFilePath = "$PSScriptRoot\$TimeStampFileName"
# Write-Host $TimeStampFilePath
if (Test-Path -path $TimeStampFilePath -PathType Leaf ) {
Write-Host "The file exists" -f green
# check if the TimeStampFile is expired
$FileDate = [datetime](Get-Item $TimeStampFilePath).LastWriteTime
$ThresholdDate = $FileDate.AddMinutes($KEEP_ALIVE_TIME)
$CurrentDate = Get-Date
$TimeDiff = ($ThresholdDate - $CurrentDate).TotalMinutes
$SleepTime = [Math]::Ceiling($TimeDiff)
$SleepTimeSecond = $SleepTime *60 #convert to seconds
# Write-Host $ThresholdDate #DEBUG
Write-Host "File Date: $FileDate"
Write-Host "ThresholdDate: $ThresholdDate"
Write-Host "CurrentDate: $CurrentDate"
Write-Host "Target TimeDiff: $TimeDiff"
Write-Host "Target SleepTime (sec): $SleepTimeSecond"
if ((Get-Date) -gt $ThresholdDate) {
Write-Host "The file is expired."
Write-Host "Removing TimeStampFile..."
Remove-Item $TimeStampFilePath
} else {
Write-Host "The file is not expired."
Write-Host "Firing up Suspend-Powerplan function for $SleepTimeSecond seconds ..."
Start-Process -FilePath "powershell" -ArgumentList "$PSScriptRoot\Suspend-Powerplan.ps1 -SleepTimeSecond $SleepTimeSecond"
#& "$PSScriptRoot\Suspend-Powerplan.ps1" -SleepTimeSecond $SleepTimeSecond
#& "$PSScriptRoot\Suspend-Powerplan.ps1"
#Start-Process -FilePath "powershell" -ArgumentList "Get-ExecutionPolicy >>d:\tmp\log.txt; $PSScriptRoot\Suspend-Powerplan.ps1 -SleepTimeSecond $SleepTimeSecond" -RedirectStandardError "d:\tmp\log2.txt"
}
} else {
Write-Host "The file does not exist" -f yellow
}