我需要將結果導出到基於列的Excel文件或逗號分隔文件(CSV)中,以便能夠在SQL Server 2008 R2中處理結果。審計本地帳戶狀態,名稱,全名,組成員和描述
我需要獲取所有本地用戶帳戶的名稱,全名,組成員及其描述。
我一直在谷歌上搜索這個安靜了一下,發現我可以通過使用ADSI,讓所有但在某種程度上狀態如下面的修改後的腳本提出:
clear
Get-Content "C:\scripts\Servers.txt" | ForEach-Object {
$Comp = $_
if (test-connection -computername $Comp -count 1 -quiet) {
([ADSI]"WinNT://$comp").Children | ? {$_.SchemaClassName -eq 'user'} | % {
$groups = $_.Groups() | % {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
$_ | Select @{n='Användarnamn:';e={$_.Name}},
@{n='Fullständigt Namn:';e={$_.FullName}},
@{n='Senast Använt:';e={$_.LastLogin}},
@{n='Tillhör grupp(er):';e={$groups -join ';'}},
@{n='Beskrivning:';e={$_.Description}}
}
} else {
Write-Warning "Server '$Comp' is Unreachable hence Could not fetch data"
}
} | Export-Csv -NoTypeInformation -Encoding UTF8 -Delimiter ";" -Path "C:\scripts\LocalUsers.csv"
[Source]
我知道這個腳本是爲了查詢servers.txt
文檔中的幾個服務器而構建的,但是到目前爲止,我只在其中輸入了「localhost」。但是,我保留這一點以備將來需要時使用。
但是,當它達到獲得「禁用」或「活動」帳戶狀態的ADSI值時,我撞牆了。它似乎以一種讓我感到驚訝的方式輸出價值。
然而,在Google上搜索並找出它可能會或可能不會通過逐位轉換來獲得此結果,我開始查看PowerShell中的Get-WmiObject
命令,將「disabled」Get-WmiObject
值與上述ADSI命令,從下面的腳本。
Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" |
Select Name, FullName, Disabled, Lockout, PasswordRequired, PasswordChangeable |
Export-Csv -NoTypeInformation -Encoding UTF8 -Delimiter "," -Path "C:\scripts\AccountDisabled.csv"
不過,我不確定如何將結果合併(或解決ADSI轉換價值),因此它進入同一基於列的Excel文件或CSV文件。
謝謝......它爲我解決了這個問題,現在我完成了腳本以獲得所需的所有信息。 –