I need to export result into column-based Excel file or comma separated file (CSV) to be able to process the result in SQL Server 2008 R2.
I am in need to get all local user account's names, full names, group membership and their description.
I have been googling this quiet a bit and found out that i could get all but status in a way by using ADSI, as presented in the following modified script:
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]
I am aware that this script is built to query several servers in the servers.txt
document, but as of now I have only "localhost" entered in there. However, I am keeping it as this in case of need in the future.
However, when it got to the ADSI value of getting the account status of "disabled" or "active" I hit a wall. It seems to output the value in a way that surprises me a bit.
However, after googling this and finding out it may or may not be able to get this by converting bitwise-something, I started looking at the Get-WmiObject
command in PowerShell to combine the "disabled" Get-WmiObject
value with the above ADSI command, from the script below.
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"
However, I am unsure about how to combine the result (or solve the ADSI convert value) so that it gets into the same column-based Excel file or CSV file.