Timestamp dinamically

This topic contains 11 replies, has 5 voices, and was last updated by  Carlos 1 week, 1 day ago.

  • Author
    Posts
  • #103882

    Carlos
    Participant

    Hi

    I'm new to powershell, and I have gather a PS script to help me on a personal project where I need to zip, move logfiles monthly. However I need to find a way to dinamically timestamp the resulting zipped folder monthly.

    #create a temporary folder using today's date
    $tempFolderRoot = "C:\Temp_"
    $date = Get-Date
    $date = $date.ToString("yyMM01-30")
    $tempFinalFolder = "$tempFolderRoot$date"
    New-Item -ItemType directory -Path $tempFinalFolder -Force

    On this line $date = $date.ToString("yyMM01-30") this is the timestamp the zipped folder will have but I wonder if anyone can help me on how to make it work so if a month has 31 days or 30 days the timestamp will automatically name it according to that

    Below is the full script:

    #get the list of files in the original folder
    $rootFolder = "C:\Windows\System32\LogFiles\W3SVC1\Test Logs"
    $archivefolder = "E:\Logs\Intranet\Archive Logs\2018"
    $tempVariable = $rootFolder
    $files = Get-ChildItem -Path $rootFolder

    #create a temporary folder using today's date
    $tempFolderRoot = "C:\Temp_"
    $date = Get-Date
    $date = $date.ToString("yyMM01-30")
    $tempFinalFolder = "$tempFolderRoot$date"
    New-Item -ItemType directory -Path $tempFinalFolder -Force

    #decide how long back to go
    $timespan = new-timespan -days 0

    #move the files to a temporary location
    foreach($file in $files)
    {
    $fileLastModifieddate = $file.LastWriteTime
    if(((Get-Date) – $fileLastModifiedDate) -gt $timespan)
    {
    Move-Item "$rootFolder\$file" -destination $tempFinalFolder
    }
    }

    $CompressionToUse = [System.IO.Compression.CompressionLevel]::Optimal
    $IncludeBaseFolder = $false
    $zipTo = "{0}\ex{1}.zip" -f $archivefolder,$date

    #add the files in the temporary location to a zip folder
    Add-Type -AssemblyName "System.IO.Compression.FileSystem"
    [System.IO.Compression.ZipFile]::CreateFromDirectory($tempFinalFolder, $ZipTo, $CompressionToUse, $IncludeBaseFolder)

    So in theory the resulting naming of each zipped file should be ex180601-30, ex180501-31 and so on.

    Any help would be much appreciated.

  • #103885

    Olaf Soyk
    Participant

    There might be more sophisticated ways to do but you could get the last day of the current month with this snippet:

    (Get-Date -Day 1 -Month ((Get-Date).Date.Month +1) -Year (Get-Date).Year).AddDays(-1)

    You simply use the current month add 1 month take the first day of this month and substract 1 day ... easy, isn't it? 😉 ... that even works for February!! 😛

    • #103891

      Carlos
      Participant

      Thanks Olaf, I'll give it a try.

  • #103895

    Simon B
    Participant

    Another way

    $Lastmonth = ((Get-Date).AddDays(-$(Get-Date).Day).AddMonths(-1))
    $LastMonth
    
    $CurrentMonth = (Get-Date).AddDays(-$(Get-Date).Day)
    $CurrentMonth
    
    • #103900

      Carlos
      Participant

      Thanks Simon for your advise.

    • #103907

      Olaf Soyk
      Participant

      @simon B, very nice, but in my environment the variable names would be misleading. The snippet for $CurrentMonth provides me with the date of the last day of the last month. And the code for $LastMonth shows me the last day of the month before the last month. 😉
      But you inspired my to another approach for the current month:

      (((Get-Date).AddMonths(1)).AddDays(-$(Get-Date).Day))
  • #103909

    Simon B
    Participant

    @Olaf Nice
    Just checked mine again

    $Lastmonth = ((Get-Date).AddDays(-$(Get-Date).Day).AddMonths(-1))
    $LastMonth
    
    $CurrentMonth = (Get-Date).AddDays(-$(Get-Date).Day)
    $CurrentMonth
    
    $olaf = (Get-Date -Day 1 -Month ((Get-Date).Date.Month ) -Year (Get-Date).Year).AddDays(-1)
    $olaf
    

    This gives me

    
    30 May 2018 16:20:12
    30 June 2018 16:20:12
    30 June 2018 16:20:12
    

    Just noticed it does not work if the previous month had 31 days 🙁

    $olaf1 = (((Get-Date).AddMonths(-1)).AddDays(-$(Get-Date).Day))
    $olaf1
    
    
    31 May 2018 16:26:17
    

    Yours works better 🙂

  • #103918

    Gnart
    Participant

    I ran Olaf code as follow:

    Get-Date
    $olaf1 = (((Get-Date).AddMonths(-1)).AddDays(-$(Get-Date).Day))
    $olaf1
    

    Notice the output was one month off. It was the last day of May, not June:
    Wednesday, July 4, 2018 12:52:46 PM
    Thursday, May 31, 2018 12:52:46 PM

    If I may suggest the following code – Other experts may shorten it for you:
    It handles if the current day is the last day of the current month.
    It the current day is not the last day of the month, then get the last day of last month.

    # Get the current Month and add one day - to see if current date is the last day of the month
    $CurrentMonth = Get-Date | Select -ExpandProperty "Month"
    $ReCheckMonth = (Get-Date).AddDays(+1) | Select -ExpandProperty "Month"
    
    # If today is the last day of the month; then use the current last day of the month
    # Else get the last day of last month
    if ($ReCheckMonth -gt $CurrentMonth) {
       $LastDayOfMonth = Get-Date
    } else {
       $LastDayOfMonth = (Get-Date).AddDays(-(Get-Date).Day)
    }
    
    • #103930

      Olaf Soyk
      Participant

      I ran Olaf code as follow:

      (((Get-Date).AddMonths(-1)).AddDays(-$(Get-Date).Day))

      That is not the code I suggested!!! My code was this:

      (((Get-Date).AddMonths(1)).AddDays(-$(Get-Date).Day))

      ... and it should return the last day of the current month.

    • #103933

      Gnart
      Participant

      Hi Olaf,
      Just verified, not sure where the -1 came from.
      I copied-pasted the code and ran it, weird.
      My apology.

  • #104008

    js
    Participant

    Here's a static daysinmonth() method:

    PS C:\users\js> [datetime]::DaysInMonth(2018,7)
    
    31
    
  • #104255

    Carlos
    Participant

    Hi

    Thank you all for your input which made the PS script worked as I wanted. Now, I need to add two additional functions to the script previously posted on 4th July:

    1. Email notification: Once the script has completed running an email need to be sent to a mailbox.
    2. Enable Logging: Once the script has completed running a log file to be generated confirming that script successful or failed.

    Many thanks in advance for all your help.

You must be logged in to reply to this topic.