アクティブなディレクトリを使用せずに30日以内に特定のコンピュータにログオンしていないユーザープロファイルを取得できるスクリプトを作成しようとしましたが、スクリプトが機能しませんでした。私はこれが私のコードでPowerShellのバージョン3を使用しています:ADなしの最後のユーザーログオン時間を取得
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
}
を私はライン$objSID = New-Object System.Security.Principal.SecurityIdentifier($profile.LocalPath) | Where {((Get-Date)-$_.lastwritetime).days -ge 30}
に-leする-geを変更しようとしただけでなく、それの後に範囲を変更するが、それはまだ私に同じリストを与えました私の変更にかかわらずコンピュータの
私はあなたのコードを試してみましたが、このエラーを得た:LastChanged:この用語は、「LastChanged」は、コマンドレット、関数、スクリプトファイル、または操作可能なプログラムの名前として認識されません。名前のスペルを確認するか、パスが含まれている場合は、パスが正しいことを確認してから、もう一度やり直してください。 Cで :\ユーザー\テスト\のSOF.ps1:59文字:28 + 場合、オブジェクト{LastChanged -lt(GET-日).AddDays(-30)} + ~~~~~~~~~~ 〜 + CategoryInfo:ObjectNotFound:(LastChanged:文字列)[]、CommandNotFoundException + FullyQualifiedErrorId:CommandNotFoundException使用しているのPowerShellのバージョン –
?あなたは私のバージョン(5)に対して簡単なテストを行っただけなので、言わなかった。 –
私は質問を更新しました。私はバージョン3を使用しています –