Timestamp dinamically

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.

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? :wink: … that even works for February!! :stuck_out_tongue:

Thanks Olaf, I’ll give it a try.

Another way

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

$CurrentMonth = (Get-Date).AddDays(-$(Get-Date).Day)
$CurrentMonth

Thanks Simon for your advise.

@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. :wink:
But you inspired my to another approach for the current month:

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

@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 :frowning:

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

31 May 2018 16:26:17

Yours works better :slight_smile:

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)
}
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.

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

Here’s a static daysinmonth() method:

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

31

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.