Reputation: 13
I have a script written that will get all of the logons for all of the users on a machine and filter out anything over 30 days old. We want to use it for more accurate count of some licenses that we pay a per user rate on. The script also filter out the Admin accounts we use along with the System and Service account.
This is the script.
#This script will check which users have logged on in the last X days
#Set Variables
#Change the number in the parenthesis after adddays to change how far back to filter
#example (get-date).adddays(-30) gets all logins for the last 30 days from today (-60) would be the last 60 days
$AuditDate = (get-date).adddays(-30)
$ComputerName = $env:COMPUTERNAME
$CurrentDate = Get-Date -UFormat "%Y-%m-%d"
#Delete any previously created files
Get-ChildItem -Path "C:\PowerShellScripts\LastLogon" -Recurse |
Where-Object CreationTime -lt (Get-Date).AddDays(-0) | Remove-Item - ErrorAction SilentlyContinue
#The Login Profile is filtered here
Get-WmiObject -class Win32_NetworkLoginProfile |
Where-Object -FilterScript {$_.FullName -notlike "*Agvance*"} |
Where-Object -FilterScript {$_.FullName -notlike "*Sophos*"} |
Where-Object -FilterScript {$_.FullName -notlike "*SSI*"} |
Where-Object -FilterScript {$_.FullName -ne "AgvAdmin"} |
Where-Object -FilterScript {$_.FullName -ne ""} |
Where-Object -FilterScript {$_.Name -notlike "*SYSTEM*"} |
Where-Object -FilterScript {$_.Name -notlike "*SERVICE*"} |
Where-Object -FilterScript {($_.ConvertToDateTime($_.LastLogon)) -ge $AuditDate} |
Select-Object Name,@{label='LastLogon';expression={$_.ConvertToDateTime($_.LastLogon)}} | Export-Csv C:\PowerShellScripts\LastLogon.csv -NoTypeInformation
#The user count is created here
$number = (Import-Csv C:\PowerShellScripts\LastLogon.csv | measure | % { $_.Count})
#The file is renamed to include computername, date, and user count
rename-item -path C:\PowerShellScripts\LastLogon.csv -NewName
C:\PowerShellScripts\LastLogon-$ComputerName-$CurrentDate-UserCount-$number.csv
The script works as intended but when it is run I receive this error.
Exception calling "ConvertToDateTime" with "1" argument(s): "Exception calling "ToDateTime" with "1" argument(s): "Specified
argument was out of the range of valid values.
Parameter name: dmtfDate""
At C:\Agvance Updates\LastLogon.ps1:22 char:29
+ Where-Object -FilterScript {$_.ConvertToDateTime($_.LastLogon) -ge $AuditDate} |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ScriptMethodRuntimeException
I have tried to different ways of comparing the $.ConvertToDateTime($.LastLogon) and the $AuditDate. This way works but I would like to get rid of the error with the $.ConvertToDateTime($.LastLogon). Is there a better way to get this date and compare it for the filtering?
Upvotes: 1
Views: 809
Reputation: 32180
LastLogon
might be empty, and ConvertToDateTime()
doesn't accept empty values.
To exclude those entries with no value for LastLogon
(if, for example, the profile was never logged on to), try:
Get-WmiObject -class Win32_NetworkLoginProfile |
[...]
Where-Object -FilterScript {![System.String]::IsNullOrWhiteSpace($_.LastLogon)} |
Where-Object -FilterScript {($_.ConvertToDateTime($_.LastLogon)) -ge $AuditDate} |
[...]
Upvotes: 2