Run Powershell If Statement Between Business Hours

Run PowerShell if statement between business hours

Tags: powershell Answers: 1 | Viewed 12,102 times



I'm looking to run a conditional statement based on if it's business hours or not. Business hours for us are 08:00 to 17:00. I have the script below, but it's not working.


I'm trying to compare (Get-Date).tostring('%H') to the hour number.


I have also tried ((Get-Date).hour -ge 17) and it still failed.


Any thoughts?


while ($loop -eq 1) {
Write-Host "Running"
# Get last write timestamp
$lastwrite = [datetime](Get-ItemProperty -Path $source -Name LastWriteTime).lastwritetime
if ( ((Get-Date).tostring('%H') -le "8" ) -and ( (Get-Date).tostring('%H') -ge "17" ) ) {
# Do nothing, it is outside of the time window
Write-Host "Nothing to do, outside of business hours"
} elseif (($lastwrite -le (Get-Date).addMinutes(-$ageMinutes)) -and ((Get-Date).tostring('%H') -ge "8" -and (Get-Date).tostring('%H') -le "17")) {
# If it's older than $ageMinutes variable above, send an email
notify
$oldTimestampFound = 1
# Sleep for 4 minutes to not flood inboxes (5 minute sleep total with the while loop)
Write-Host "Alert sent. Sleeping for 4 minutes..."
Start-Sleep -s 300
} elseif (($lastwrite -ge (Get-Date).addMinutes(-$ageMinutes)) -and ($oldTimestampFound -eq 1)) {
$oldTimestampFound = 0
Write-Host "All clear"
notifyAllClear
}
Write-Host "Sleeping for 60 seconds..."
Start-Sleep -s 60
}

I put those Write-Hosts in there to try and debug, but my output is


Running
Sleeping for 60 seconds...


Asked by Pat

Answers:

TheMadTechnician answer at 2014-03-12 9



You're formatting the Get-Date wrong, use the -Format HH option for Get-Date Personally I'd set it once as an integer for easy comparisons like this:


[int]$hour = get-date -format HH
If($hour -lt 8 -or $hour -gt 17){ <do nothing> }
Else{
If($lastwrite -le (Get-Date).addMinutes(-$ageMinutes)){ <send email, set oldTimeStampFound flag, and sleep> }
Else{
<Clear oldTimeStampFound flag>
}
}

* The answers/resolutions are collected from stackoverflow, are licensed under CC BY-SA 3.0

Some Code Answers


while ($loop -eq 1) {  Write-Host "Running"
# Get last write timestamp
$lastwrite = [datetime](Get-ItemProperty -Path $source -Name LastWriteTime).lastwritetime
if ( ((Get-Date).tostring('%H') -le "8" ) -and ( (Get-Date).tostring('%H') -ge "17" ) ) {
# Do nothing, it is outside of the time window
Write-Host "Nothing to do, outside of business hours"
} elseif (($lastwrite -le (Get-Date).addMinutes(-$ageMinutes)) -and ((Get-Date).tostring('%H') -ge "8" -and (Get-Date).tostring('%H') -le "17")) {
# If it's older than $ageMinutes variable above, send an email
notify
$oldTimestampFound = 1
# Sleep for 4 minutes to not flood inboxes (5 minute sleep total with the while loop)
Write-Host "Alert sent. Sleeping for 4 minutes..."
Start-Sleep -s 300
} elseif (($lastwrite -ge (Get-Date).addMinutes(-$ageMinutes)) -and ($oldTimestampFound -eq 1)) {
$oldTimestampFound = 0
Write-Host "All clear"
notifyAllClear
}
Write-Host "Sleeping for 60 seconds..."
Start-Sleep -s 60 }

Running Sleeping for 60 seconds... 

[int]$hour = get-date -format HH If($hour -lt 8 -or $hour -gt 17){ <do nothing>
} Else{
If($lastwrite -le (Get-Date).addMinutes(-$ageMinutes)){ <send email, set oldTimeStampFound flag, and sleep>
}
Else{
<Clear oldTimeStampFound flag>
} }

More Answers Related Run Powershell If Statement Between Business Hours

1 day ago Run PowerShell if statement between business hours. Ask Question Asked 8 years, 6 months ago. Modified 5 months ago. Viewed 13k times 3 1. I'm looking to run a conditional statement

Reviews: 5

Show details

1 week ago Oct 18, 2021  · Nested conditional statements basically cycle through the statement until either a statement is returned true or until all statements are returned false. There is a couple of …

Show details

3 days ago Jan 11, 2020  · The If Statement in PowerShell allows the programmer to control the flow of execution of the program, the” IF” statement defines if a program has to execute a section of …

Estimated Reading Time: 4 mins

Show details

1 week ago When using if, elseif, else statements there are a few points to keep in mind. An if can have zero or one else's and it must come after any elseif's. An if can have zero to many elseif's and they …

Show details

See also: Shell

1 week ago Dec 21, 2018  · Since the current time is newer than this we can simply add in a full 8 hours. We do the same for the 3rd. Finally on the 4th, since the current time is 1:41PM, that means we …

Show details

1 week ago If database d1 is inactive than connect to another database d2.So to write a program for this we can use the if-else statement. In the if statement we will check the status of database d1 if it …

Show details

See also: Database

1 week ago Speichern Sie Powershell Shell Studio 2015 in TFS - powershell, powershell-studio Führe PowerShell aus, ohne gVim [duplicate] zu verlassen - powershell, vim, plugins Profil auf …

Show details

See also: Powershell Shell

2 days ago May 06, 2009  · The Windows PowerShell Scripting Hub is a good place to start using Windows PowerShell. An excellent book for learning Windows PowerShell is the Microsoft Press book, …

Show details

See also: Excel

3 days ago Usando o perfil no PowerShell não no ISE - powershell, powershell-v5.0 Por que o powershell está me dizendo que eu preciso? antes de pip - powershell, python-2.7, pip Posso usar a …

Show details

See also: Python Powershell Python-2.7 Shell

4 days ago Jan 31, 2021  · When the if statement runs, PowerShell runs through each condition evaluating if the code returns true or false. A summary of the behavior is below: PowerShell evaluates the …

Show details

6 days ago Powershell - Nested If Else Statement, It is always legal to nest if-else statements which means you can use one if or elseif statement inside another if or elseif statement. ... 35 Lectures 2.5 …

Show details

See also: Shell

1 week ago Nov 11, 2021  · When an IF statement is run, the conditional expression is evaluated as either true or false. If is true, runs, and it exits the If statement. If is false, does not run, and it exits …

Show details

See also: Express

Please leave your answer here: