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