System.DirectoryServices.DirectorySearcher objectGUID 值不正确 System.Byte[] [英] System.DirectoryServices.DirectorySearcher objectGUID value incorrect System.Byte[]
问题描述
我有从这里得到的经过修改的代码:https://blog.schmijos.ch/2013/09/27/ad-export-with-get-qaduser-is-too-slow/
I have code that I have modified that I got from here: https://blog.schmijos.ch/2013/09/27/ad-export-with-get-qaduser-is-too-slow/
我修改的代码包含pagesize,所以可以抓取1000多个账号.它将日期值从某个非常大的数字更改为可读的日期/时间戳.我这里有整个修改后的代码...
The code I have modified includes pagesize, so it can grab more than 1000 accounts. It changes the date values from some really big number to a readable date/time stamp. The entire modified code I have here...
function CreateQueriedCsvDataSheet
{
$csvFileWithPath = 'C:\Scripts\Tests\testResults.csv'
$DomainControlConnector = 'www.example.com'
$DomainName = 'myDomain1'
$domainUserProperties = @('sAMAccountName', 'msRTCSIP-UserEnabled', 'msRTCSIP-OptionFlags', 'msRTCSIP-PrimaryUserAddress', 'msRTCSIP-PrimaryHomeServer',
'mail', 'msExchMasterAccountSid', 'homeMDB', 'proxyaddresses', 'legacyExchangeDN',
'lastLogonTimestamp', 'logonCount', 'lastLogoff', 'lastLogon', 'pwdLastSet', 'userAccountControl', 'whenCreated', 'whenChanged', 'accountExpires',
'sn', 'givenName', 'displayName', 'distinguishedName', 'initials', 'l', 'st', 'street', 'title', 'description', 'postalCode', 'physicalDeliveryOfficeName', 'telephoneNumber', 'facsimileTelephoneNumber', 'info', 'memberOf', 'co', 'department', 'company', 'streetAddress', 'employeeNumber', 'employeeType', 'objectGUID', 'employeeID', 'homeDirectory', 'homeDrive', 'scriptPath', 'objectSid', 'userPrincipalName', 'url', 'msDS-SourceObjectDN', 'manager', 'extensionattribute8')
Logger $LogFileWithPath "Querying $DomainName for user account attributes and exporting to csv file $csvFileWithPath..."
powershell -Command {
Param ([string]$domainControlConnector, [string]$csvOutFile, [string]$DomainName, [String[]]$domainUserProperties)
$domain = "LDAP://$domainControlConnector"
Write-Host "Searching Users Properties for domain $DomainNamein in AD..."
$adDomain = New-Object System.DirectoryServices.DirectoryEntry($domain)
$adSearcher = New-Object System.DirectoryServices.DirectorySearcher($adDomain)
$adSearcher.Filter = '(&(objectCategory=User)'
$adSearcher.PageSize=1000
$adSearcher.PropertiesToLoad.AddRange($domainUserProperties)
$userRecords = $adSearcher.FindAll()
Write-Host "Complete"
# The AD results are converted to an array of hashtables.
Write-Host "Exporting User Attributes to table..."
$userPropertiesTable = @()
foreach($record in $userRecords) {
$hashUserProperty = @{}
foreach($userProperty in $domainUserProperties){
if ($record.Properties[$userProperty]) {
$hashUserProperty.$userProperty = $record.Properties[$userProperty][0]
} else {
$hashUserProperty.$userProperty = $null
}
}
$userPropertiesTable += New-Object PSObject -Property $hashUserProperty
}
Write-Host "Complete."
$listOfBadDateValues = '9223372036854775807', '9223372036854770000', '0'
$maxDateValue = '12/31/1600 5:00 PM'
Write-Host "fixing table property titles and values for report"
#$userPropertiesTable[0] = $userPropertiesTable[0] -replace 'givenname','FirstName'
$tableFixedValues = $userPropertiesTable | % {
if ($_.lastLogonTimestamp) {
$_.lastLogonTimestamp = ([datetime]::FromFileTime($_.lastLogonTimestamp)).ToString('g')
}; if (($_.AccountExpires) -and ($listOfBadDateValues -contains $_.AccountExpires)) {
$_.AccountExpires = $null
} else {
if (([datetime]::FromFileTime($_.AccountExpires)).ToString('g') -eq $maxDateValue) {
$_.AccountExpires = $null
} Else {
$_.AccountExpires = ([datetime]::FromFileTime($_.AccountExpires)).ToString('g')
}
}; if (($_.lastLogon) -and ($listOfBadDateValues -contains $_.lastLogon)) {
$_.lastLogon = $null
} else {
if (([datetime]::FromFileTime($_.lastLogon)).ToString('g') -eq $maxDateValue) {
$_.lastLogon = $null
} Else {
$_.lastLogon = ([datetime]::FromFileTime($_.lastLogon)).ToString('g')
}
}; if (($_.pwdLastSet) -and ($listOfBadDateValues -contains $_.pwdLastSet)) {
$_.pwdLastSet = $null
} else {
if (([datetime]::FromFileTime($_.pwdLastSet)).ToString('g') -eq $maxDateValue) {
$_.pwdLastSet = $null
} Else {
$_.pwdLastSet = ([datetime]::FromFileTime($_.pwdLastSet)).ToString('g')
}
};$_}
Write-Host "Complete"
Write-Host "Exporting table to csv file $csvOutFile"
$tableFixedValues | Select-Object $domainUserProperties | Export-Csv $csvOutFile -NoTypeInformation -Force
Write-Host "Complete"
Write-Host "Changing column titles to expected format"
$data = Get-Content $csvOutFile
$data[0] = $data[0] -replace 'givenname', 'FirstName'
$data[0] = $data[0] -replace 'sn', 'LastName'
$data | Set-Content $csvOutFile -Force
Write-Host "Complete"
Write-Host ""
} -args $DomainControlConnector, $csvFileWithPath, $DomainName, $domainUserProperties
}
Function Main
{
CreateQueriedCsvDataSheet
}
Main
objectGUID
和 objectSid
列的值的输出打印为 System.Byte[]
.我怎样才能让它提取正确的值?
The output for the values of columns objectGUID
and objectSid
are printing as System.Byte[]
. How can I get it to extract the correct value?
推荐答案
值是正确的.您只需要将它们转换为您期望的形式即可.
The values are correct. You just need to convert them to the form you expect.
$stringGUID = ([guid]$binaryGUID).Guid
$stringSID = (New-Object Security.Principal.SecurityIdentifier($binarySID, 0)).Value
这篇关于System.DirectoryServices.DirectorySearcher objectGUID 值不正确 System.Byte[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!