title: Run PowerShell if statement between business hours
body: <p>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. </p>
  
  <p>I'm trying to compare <code>(Get-Date).tostring('%H')</code> to the hour number. </p>
  
  <p>I have also tried <code>((Get-Date).hour -ge 17)</code> and it still failed.</p>
  
  <p>Any thoughts?</p>
  
  <pre><code>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
  
  }
  </code></pre>
  
  <p>I put those <code>Write-Hosts</code> in there to try and debug, but my output is </p>
  
  <pre><code>Running
  Sleeping for 60 seconds...
  </code></pre>