1
我想創建一個腳本,可以獲得未在30天內登錄特定計算機的用戶配置文件不使用活動目錄,但我的腳本沒有工作。我使用PowerShell版本3。這是我的代碼:得到最後一次用戶登錄時間沒有公元
netsh advfirewall firewall set rule group="Windows Management Instrumentation (WMI)" new enable=yes
$ComputerList = Get-Content C:\temp\Computers1.txt
$myDomain = Get-Content C:\temp\Domain.txt
$csvFile = 'C:\temp\Profiles.csv'
# Create new .csv output file
New-Item $csvFile -type file -force
# Output the field header-line to the CSV file
"HOST,PROFILE" | Add-Content $csvFile
# Loop over the list of computers from the input file
foreach ($Computer in $ComputerList) {
# see if ping test succeeds for this computer
if (Test-Connection $Computer -Count 3 -ErrorAction SilentlyContinue) {
$ComputerFQDN = $Computer + $myDomain
$Profiles = Get-WmiObject -Class Win32_UserProfile -Computer $ComputerFQDN | Where{$_.LocalPath -notlike "*$env:SystemRoot*"}
foreach ($profile in $profiles) {
try {
$objSID = New-Object System.Security.Principal.SecurityIdentifier($profile.LocalPath) | Where {((Get-Date)-$_.lastwritetime).days -ge 30}
#| Where-Object {$_.LastLogonDate -le $CurrentDate.AddDays(-60)}
$objuser = $objsid.Translate([System.Security.Principal.NTAccount])
$objusername = $objuser.value
} catch {
$objusername = $profile.LocalPath
}
switch($profile.status){
1 { $profileType="Temporary" }
2 { $profileType="Roaming" }
4 { $profileType="Mandatory" }
8 { $profileType="Corrupted" }
default { $profileType = "LOCAL" }
}
$User = $objUser.Value
#output profile detail for this host
"$($Computer.toUpper()), $($objusername)" | Add-Content $csvFile
}
} else {
#output failure message for this host
"$($Computer.toUpper()), PING TEST FAILED" | Add-Content $csvFile
}
#LOOP
}
我試圖改變-ge在該行$objSID = New-Object System.Security.Principal.SecurityIdentifier($profile.LocalPath) | Where {((Get-Date)-$_.lastwritetime).days -ge 30}
到-le,以及之後改變的範圍內,但它仍然給了我同樣的列表無論我的改變如何
我想你的代碼,但得到這個錯誤:LastChanged:術語「LastChanged未被識別爲cmdlet,函數,腳本文件或可操作的程序的名稱。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然後重試。 在C:\ Users \ test \ SOF.ps1:59 char:28 + Where-Object {LastChanged -lt(Get-Date).AddDays(-30)} + ~~~~~~~~~~ 〜 + CategoryInfo:ObjectNotFound:(LastChanged:String)[],CommandNotFoundException + FullyQualifiedErrorId:CommandNotFoundException –
您正在使用哪個版本的PowerShell?你沒有這麼說,我只是用簡單的測試來對付我的版本(5)。 –
我更新了問題,我正在使用版本3 –