我編寫了一個腳本,該腳本可以獲取機器上所有用戶的所有登錄信息,並過濾掉超過30天的任何內容。我們希望使用它來更準確地計算我們按用戶費率付費的一些許可證。該腳本還會過濾掉我們與系統和服務帳戶一起使用的管理員帳戶。獲取將上次登錄時間與日期進行比較的錯誤
這是腳本。
#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
該腳本按預期工作,但在運行時收到此錯誤。
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
我試圖比較$ .ConvertToDateTime($ .LastLogon)和$ AuditDate不同的方式。這種方式可行,但我想擺脫$ .ConvertToDateTime($ .LastLogon)的錯誤。有沒有更好的方法來獲得這個日期,並比較它的過濾?
只是爲了提高效率 - 就不是一個單一的-notmatch結合所有的用'$ _。FullName'進行測試會加速嗎? 'Where-Object -FilterScript {$ _。FullName -notmatch'Agvance | Sophos | SSI |^AgvAdmin $ |^$'} ' – LotPings