VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

Find the beginning and ending of a month using get-date

I'm currently using get-date to run reports on past events by subtracting the number of days from the current day.  I'd like to make the report run so that it automatically determines what the previous month was, and captures all events for that date range.

For example, if it is February 6th, make the report capture data from January 1st to January 31st.  If it is March 23rd, make the report capture events from February 1st to February 28th. 

Is this possible?  here is my current statement:

Get-VIEvent -Start (Get-Date).AddDays(-35) -Finish (Get-Date).AddDays(-5)

Thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$now = Get-Date -Hour 0 -Minute 0 -Second 0

$daysBack = - $now.Day + 1

$Start = $now.AddMonths(-1).AddDays($daysBack)

$DaysInMonth = [DateTime]::DaysInMonth($Start.Year,$Start.Month)

$Finish = $Start.AddDays($DaysInMonth)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$now = Get-Date -Hour 0 -Minute 0 -Second 0

$daysBack = - $now.Day + 1

$Start = $now.AddMonths(-1).AddDays($daysBack)

$DaysInMonth = [DateTime]::DaysInMonth($Start.Year,$Start.Month)

$Finish = $Start.AddDays($DaysInMonth)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can use:

$Finish = Get-Date -Day 1 -Hour 0 -Minute 0 -Second 0

$Start = $Finish.AddMonths(-1)

Get-VIEvent -Start $Start -Finish $Finish

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
TheVMinator
Expert
Expert
Jump to solution

OK great thanks again

0 Kudos