Created
March 1, 2018 13:27
-
-
Save jmelosegui/bf3c933a19340f2b3860d29bcb131b30 to your computer and use it in GitHub Desktop.
The script will receive a path to a text file containing one GUID per line, loop over each line and search for the user info (Full Name) then generate a CSV file with the user info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory=$true)] | |
$filePath | |
) | |
$accountsFilter= "" | |
Get-Content -Path $filePath | ForEach-Object { | |
if($_.Trim()){ | |
$accountsFilter += "(sAMAccountName=$_)" | |
} | |
} | |
$resultFile = Join-Path (Split-Path $filePath -Parent) "$([io.path]::GetFileNameWithoutExtension($filePath))_Completed.csv" | |
$strDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() | |
$strRoot = $strDomain.GetDirectoryEntry() | |
$adoConnection = New-Object -comObject "ADODB.Connection" | |
$adoCommand = New-Object -comObject "ADODB.Command" | |
$adoConnection.Open("Provider=ADsDSOObject;") | |
$adoCommand.ActiveConnection = $adoConnection | |
$adoCommand.Properties.Item("Page Size") = 100 | |
$adoCommand.Properties.Item("Timeout") = 30 | |
$adoCommand.Properties.Item("Cache Results") = $False | |
$strBase = $strRoot.distinguishedName | |
$strAttributes = "sAMAccountName,displayname" | |
$strScope = "subtree" | |
$strFilter = "(&(objectCategory=person)(|$accountsFilter))" | |
$strQuery = "<LDAP://$strBase>;$strFilter;$strAttributes;$strScope" | |
$adoCommand.CommandText = $strQuery | |
$adoRecordset = $adoCommand.Execute() | |
$result = @() | |
Do | |
{ | |
$object = New-Object -TypeName Object | |
$object | Add-Member -MemberType NoteProperty -Name Guid -Value ($adoRecordset.Fields.Item("sAMAccountName") | Select-Object -ExpandProperty Value) -PassThru | | |
Add-Member -MemberType NoteProperty -Name FullName -Value ($adoRecordset.Fields.Item("displayname") | Select-Object -ExpandProperty Value) | |
$result += $object | |
$adoRecordset.MoveNext() | |
} Until ($adoRecordset.EOF) | |
$result | Export-Csv -Path $resultFile -NoTypeInformation | |
$adoRecordset.Close() | |
$adoConnection.Close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment