Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Need help simplifying get-date

$
0
0

I have a script that returns all users from AD who logged in from the last 30 days, but I need to adjust it to make it for the current calendar month. I've got the following code but I could do with some help simplifying it.

$month = (Get-Date -format "MM")
$year = (Get-Date -format "yyyy")

switch ($month)		#if month has less than 31 days
#start switch statement 
{

04 { $myvar = '30' }
06 { $myvar = '30' }
09 { $myvar = '30' }
11 { $myvar = '30' }
02 { if ([datetime]::IsLeapYear($year)){	#if current year is a leap year
	$myvar = '29'
}
	else {
	$myvar = '28'}
	}
}#end switch statement

So $myvar is the variable it uses to query AD users and say Where-Object {$_.LastLogon -le $myvar}. I haven't implemented this code yet but the script works fine at the moment with a variable set to 30 so let's put Active Directory to one side, because this is adapted from the first PS script I ever wrote and is very sloppy. Can anyone help in simplifying it at all?

Thanks in advance


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>