Get-Date folder and make an action

Welcome Forums General PowerShell Q&A Get-Date folder and make an action

Viewing 1 reply thread
  • Author
    Posts
    • #207177
      Participant
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

      Good morning, everyone,

      I’m new to PowerShell.
      I would like to do an action based on the last modification date of a directory as follows:

      If the last modification of the directory is equal or superior to the last hour, write “No file for more than 1 hour”.

      If the last modification of the directory is less than the last hour, write “No file for less than 1 hour”.

      I have the code below :

      […]

      elseif (Get-ChildItem $folder | Where {$folder.LastWriteTime -ge $Date}) {
      Write-Host “Aucun fichier depuis plus d’1 heure”
      exit 2
      }

      else {(Get-ChildItem $folder | Where {$folder.LastWriteTime -lt $Date})
      Write-Host “Aucun fichier depuis moins d’ 1 heure”
      exit 0
      }

      The exit code is for Centreon.

      $Folder refers to “C:\Directory\”

       

      Someone have an idea ?

    • #207225
      Participant
      Topics: 5
      Replies: 321
      Points: 436
      Helping Hand
      Rank: Contributor
      $folder = Get-ChildItem -Path “C:\Directory\”
      
      If ($folder.LastWriteTime -lt (Get-Date).AddHours(-1)){
          Write-Verbose "No file less than last hour" -Verbose
      } ElseIf ($folder.LastWriteTime -ge (Get-Date).AddHours(-1)) {
          Write-Verbose "No file more or equal last hour" -Verbose
      }
      
Viewing 1 reply thread
  • You must be logged in to reply to this topic.