Skip to content

Instantly share code, notes, and snippets.

@u1735067
Last active April 30, 2018 14:25
Show Gist options
  • Save u1735067/253fd026a45f86b5276e071e69113305 to your computer and use it in GitHub Desktop.
Save u1735067/253fd026a45f86b5276e071e69113305 to your computer and use it in GitHub Desktop.
First PowerShell script ; adaptation of query.sh (but with less redundant code)
# You might want Set-ExecutionPolicy -Scope Process RemoteSigned
if ($args.Count -eq 0) {
Write-Host "[*] Example: .\query.ps1 name[@[domain.com]] [..]"
exit 13
}
$dataDir=$PSScriptRoot+'\data'
# https://github.com/BurntSushi/ripgrep
$rg_bin=$PSScriptRoot+'\rg.exe'
$rg_args=@('-aiN')
foreach ($needle in $args) { # Multiple args
$searchPath = $dataDir
foreach ($charPos in 0..2) { # Max depth
$char = $needle[$charPos]
if ($char -match '[a-z0-9]') {
$searchPath += "\$char"
$stop = 0
} else {
$searchPath += "\symbols"
$stop = 1 # Symbols are dead-ends
}
if (Test-Path -PathType Leaf -Path $searchPath) {
if (Test-Path -PathType Leaf -Path $rg_bin) { # Use RipGrep if present: it is faster
& $rg_bin $rg_args "^${needle}" $searchPath
} else {
#Select-String -Pattern "^${needle}" -Path "$searchPath" | %{$_.Line}
Select-String -Pattern "^${needle}" -Path $searchPath | Select-Object -ExpandProperty Line
}
break
}
if ($stop) { break }
}
}
<#
https://community.spiceworks.com/topic/1970313-get-first-character-of-a-word-string-and-first-character-of-the-second-word
http://www.computerperformance.co.uk/powershell/powershell_syntax.htm
https://technet.microsoft.com/en-us/library/hh551144.aspx?f=255&MSPPError=-2147217396
https://docs.microsoft.com/en-us/powershell/scripting/getting-started/fundamental/windows-powershell-basics?view=powershell-6
https://stackoverflow.com/questions/16426688/passing-a-variable-to-a-powershell-script-via-command-line
https://www.reddit.com/r/PowerShell/comments/73wi5e/how_to_write_a_script_that_takes_a_variable/
https://stackoverflow.com/questions/2157554/how-to-handle-command-line-arguments-in-powershell
https://stackoverflow.com/questions/2035193/how-to-run-a-powershell-script
https://ss64.com/ps/syntax-run.html
https://ss64.com/ps/set-executionpolicy.html
https://stackoverflow.com/questions/31888580/a-better-way-to-check-if-a-path-exists-or-not-in-powershell
https://github.com/PowerShell/PowerShell/issues/1970
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-6
https://kevinmarquette.github.io/2017-07-31-Powershell-regex-regular-expression/
https://docs.microsoft.com/fr-fr/powershell/module/Microsoft.PowerShell.Utility/Select-String?view=powershell-6
https://stackoverflow.com/questions/5466329/whats-the-best-way-to-determine-the-location-of-the-current-powershell-script
https://serverfault.com/questions/555439/passing-arguments-with-variables-in-powershell-to-native-application
http://www.rlmueller.net/PowerShellEscape.htm
http://www.itprotoday.com/management-mobility/windows-powershell-range-operator-tricks
https://stackoverflow.com/questions/21129746/multiline-comment-in-powershell
http://blog.itsnotfound.com/2014/01/truefalse-boolean-values-in-powershell/
https://blogs.msdn.microsoft.com/powershell/2006/12/24/boolean-values-and-operators/
https://social.technet.microsoft.com/Forums/scriptcenter/en-US/6ab45444-ec09-48ea-84f6-200330448582/count-property?forum=ITCG
In PowerShell, arrays have a Length and Count property. The Count property in PowerShell is actually simply a reference to the Length property. You can observe this by viewing the "PSExtended" version of the result.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-6#outputs
https://msdn.microsoft.com/en-us/library/microsoft.powershell.commands.matchinfo_members(v=vs.85).aspx
https://github.com/PowerShell/PowerShell/issues/4767
https://communary.net/2014/11/10/grep-the-powershell-way/
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-6
https://stackoverflow.com/questions/21901377/powershell-return-error-exit-code-if-not-match-a-string
https://blogs.msdn.microsoft.com/powershell/2006/10/14/windows-powershell-exit-codes/
https://blogs.msdn.microsoft.com/powershell/2006/09/15/errorlevel-equivalent/
https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
foreach ($needle in $args)
{
$letter1 = $needle[0]
if (Select-String -Quiet -InputObject letter1 -Pattern '[a-zA-Z0-9]') {
if (Test-Path -PathType Leaf -Path "${dataDir}\${letter1}") {
& $rg_bin $rg_args "^${needle}" "${dataDir}\${letter1}"
} else {
$letter2 = $needle[1]
if (Select-String -Quiet -InputObject letter2 -Pattern '[a-zA-Z0-9]') {
if (Test-Path -PathType Leaf -Path "${dataDir}\${letter1}\${letter2}") {
& $rg_bin $rg_args "^${needle}" "${dataDir}\${letter1}\${letter2}"
} else {
}
} else {
}
}
} else {
}
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment