Skip to content

Instantly share code, notes, and snippets.

@maphew
Last active January 25, 2022 22:02
Show Gist options
  • Save maphew/027d0d9d8913afcc562a to your computer and use it in GitHub Desktop.
Save maphew/027d0d9d8913afcc562a to your computer and use it in GitHub Desktop.
Generates a Query Directory Service file which runs a search for the %USERNAME% environment variable in the Windows integrated search tool. Adapted from "How to generate a QDS files which runs a user or group search" by FS76. In support of http://community.spiceworks.com/topic/278782-allow-users-to-update-their-own-information-in-active-directory
cls
# Author: Frank Schneider
# Date: 2015-04-20
# Version:1.0
# Synopsis:
# this sample script generates a QDS file which runs a search for the
# string "Domain Users" in the Windows integrated search tool.
# https://gallery.technet.microsoft.com/scriptcenter/How-to-generate-a-QDS-05fe4ff5
#-- start of define functions --#
# 'Convert-ByteArrayToHexString' was found at http://cyber-defense.sans.org/blog/2010/02/11/powershell-byte-array-hex-convert
function Convert-ByteArrayToHexString
{
################################################################
#.Synopsis# Returns a hex representation of a System.Byte[] array as
# one or more strings. Hex format can be changed.
#.Parameter ByteArray
# System.Byte[] array of bytes to put into the file. If you
# pipe this array in, you must pipe the [Ref] to the array.
# Also accepts a single Byte object instead of Byte[].
#.Parameter Width# Number of hex characters per line of output.
#.Parameter Delimiter# How each pair of hex characters (each byte of input) will be
# delimited from the next pair in the output. The default
# looks like "0x41,0xFF,0xB9" but you could specify "&#92x" if
# you want the output like "&#92x41&#92xFF&#92xB9" instead. You do
# not have to worry about an extra comma, semicolon, colon
# or tab appearing before each line of output. The default
# value is ",0x".#.Parameter Prepend
# An optional string you can prepend to each line of hex
# output, perhaps like '$x += ' to paste into another
# script, hence the single quotes.
#.Parameter AddQuotes
# An switch which will enclose each line in double-quotes.
#.Example# [Byte[]] $x = 0x41,0x42,0x43,0x44
# Convert-ByteArrayToHexString $x#
# 0x41,0x42,0x43,0x44#.Example
# [Byte[]] $x = 0x41,0x42,0x43,0x44
# Convert-ByteArrayToHexString $x -width 2 -delimiter "&#92x" -addquotes#
# "&#92x41&#92x42"
# "&#92x43&#92x44"
###############################################################
#
[CmdletBinding()] Param
(
[Parameter(Mandatory = $True, ValueFromPipeline = $True)] [System.Byte[]] $ByteArray,
[Parameter()] [Int] $Width = 10,
[Parameter()] [String] $Delimiter = ",0x",
[Parameter()] [String] $Prepend = "",
[Parameter()] [Switch] $AddQuotes
)
if ($Width -lt 1) { $Width = 1 }
if ($ByteArray.Length -eq 0) { Return }
$FirstDelimiter = $Delimiter -Replace "^[&#92,\&#92:&#92t]",""
$From = 0
$To = $Width - 1
Do
{
$String = [System.BitConverter]::ToString($ByteArray[$From..$To])
$String = $FirstDelimiter + ($String -replace "&#92-",$Delimiter)
if ($AddQuotes) { $String = '"' + $String + '"' }
if ($Prepend -ne "") { $String = $Prepend + $String }
$String
$From += $Width
$To += $Width
} While ($From -lt $ByteArray.Length)
}
#-- end of define functions --#
#define search string and file name
#$string = "Domain Users"
$string = $env:USERNAME
$attachment = ".\"+$string
#define help variables
$newline = "`n"
$convert= ""
$checksum = 0
#convert a string to hex values
for ($i=0;$i -le $string.Length;$i++)
{
#get ASCII code for a character
$acsii = [BYTE][CHAR]$string[$i]
#convert character to a HEX value
$hex = Convert-ByteArrayToHexString $acsii -delimiter ""
#add to more digits to the HEX value and concatenate the results
$convert=$convert+$hex+"00"
#sum all ACSII charater values in a checksum value
$checksum=$checksum+$acsii
}
#if the checksum in hex would have more than 2 digits then only keep the last two of them
#restict the hexchecksum to the last 2 digits
if ($checksum -gt 8192) {$checksum=$checksum-8192}
if ($checksum -gt 4096) {$checksum=$checksum-4096}
if ($checksum -gt 2048) {$checksum=$checksum-2048}
if ($checksum -gt 1024) {$checksum=$checksum-1024}
if ($checksum -gt 512) {$checksum=$checksum-512}
if ($checksum -gt 256) {$checksum=$checksum-256}
#convert decimal checksum into hex
$checksumhex=Convert-ByteArrayToHexString $checksum -delimiter ""
#create anrvalue
$anrvalue = $convert + $checksumhex
#create anrLenght
$stringlength = $string.Length +1
$anrLength = Convert-ByteArrayToHexString $stringlength -delimiter ""
$stranrLength = "anrLength=" + $anrLength + "000000" + $anrLength
$stranrValue = "anrValue=" + $anrvalue
#Write-Host $convert
Write-Host $stranrLength
Write-Host $stranrValue
#generate output text
$filetxt = ""
$filetxt = "[CommonQuery]" + $newline
$filetxt = $filetxt + "Handler=5EE6238AC231D011891C00A024AB2DBBC1" + $newline
$filetxt = $filetxt + "Form=E23FEE83D957D011B93200A024AB2DBBE5" + $newline
$filetxt = $filetxt + "[DsQuery]" + $newline
$filetxt = $filetxt + "ViewMode=0413000017" + $newline
$filetxt = $filetxt + "EnableFilter=0000000000" + $newline
$filetxt = $filetxt + "[Microsoft.People]" + $newline
$filetxt = $filetxt + $stranrLength + $newline
$filetxt = $filetxt + $stranrValue + $newline
$filetxt = $filetxt + "[Microsoft.PropertyWell]" + $newline
$filetxt = $filetxt + "Items=0000000000" + $newline
#write output text to file
Write-output $filetxt | Out-File $attachment".qds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment