Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions utilities/powershell/Invoke-BHCypherLibrary.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
function Invoke-BHCypherLibrary {
[CmdletBinding()]
param (
# Get single Cypher from YAML file
[Parameter(ParameterSetName = 'Import')]
[Parameter(ParameterSetName = 'Run')]
[Parameter(Mandatory, ParameterSetName = 'SingleYAML')]
[string]$YAMLFilePath,

# Get all Cyphers from JSON
[Parameter(ParameterSetName = 'Import')]
[Parameter(ParameterSetName = 'Run')]
[Parameter(Mandatory, ParameterSetName = 'AllJSON')]
[switch]$AllFromJSON,
[Parameter(ParameterSetName = 'AllJSON')]
[string]$JSONPath = ".\Cypher.json",

# Get all Cyphers from YAML
[Parameter(ParameterSetName = 'Import')]
[Parameter(ParameterSetName = 'Run')]
[Parameter(Mandatory, ParameterSetName = 'AllYAML')]
[switch]$AllFromYAML,
[Parameter(ParameterSetName = 'AllYAML')]
[string]$YAMLDirectoryPath = ".\Cypher",

# Import
[Parameter(Mandatory, ParameterSetName = 'Import')]
[switch]$Import,

# Run
[Parameter(Mandatory, ParameterSetName = 'Run')]
[switch]$Run
)

if ($YAMLFilePath) {
if (-not (Get-Module powearshell-yaml)) {
Write-Error "Missing module 'powershell-yaml'"
return
}

$CypherArray = @()
if ($YAMLFilePath.EndsWith('.yml') -or $YAMLFilePath.EndsWith('.yaml')) {
$CypherArray += Get-Content $YAMLFilePath | ConvertFrom-Yaml
} else {
Write-Error "Filetype not .yml or .yaml"
return
}
} elseif ($AllFromYAML) {
if (-not (Get-Module powearshell-yaml)) {
Write-Error "Missing module 'powershell-yaml'"
return
}

$CypherArray = @()
Get-ChildItem -Path $YAMLDirectoryPath -Recurse -Include *.yml,*.yaml | % {$CypherArray += Get-Content $_ | ConvertFrom-Yaml }
} elseif ($AllFromJSON) {
$CypherArray = Get-Content $JSONPath | ConvertFrom-Json
}

foreach ($Cypher in $CypherArray) {
switch ($Cypher.Platform) {
"Active Directory" {$Platform = "AD: "}
"Azure" {$Platform = "AZ: "}
}
$Name = ($Platform + $Cypher.Category + ' // ' + $Cypher.Name)

if ($Import) {
New-BHPathQuersy -Name $Name -Description $Cypher.Description -Query $Cypher.Query
} elseif ($Run) {
Write-Host "# Running query: $Name" -ForegroundColor Yellow
try {
$o = Invoke-BHCypher -Query $Cypher.Query -Minimal
} catch {
Write-Host $_
}
#pause
}
}
}
37 changes: 37 additions & 0 deletions utilities/python/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing_extensions import Annotated
from pathlib import Path
import json
import glob
import typer
import yaml

app = typer.Typer()


@app.command()
def to_json(
input_dir: Annotated[
Path,
typer.Argument(
exists=True,
file_okay=True,
dir_okay=True,
readable=True,
resolve_path=True,
),
],
output_file: Annotated[typer.FileTextWrite, typer.Argument()]
):
cypher_queries = glob.glob(f"{input_dir}/**/*.yml", recursive=True)
typer.echo(f"Converting Queries {len(cypher_queries)} to combined JSON")
all_objects = []
for cypher_query in cypher_queries:
with open(cypher_query, "r") as yaml_file:
all_objects.append(yaml.safe_load(yaml_file))

output_file.write(json.dumps(all_objects, indent=2))
typer.echo(f"Finished converting Cypher queries to JSON to {output_file.name}")


if __name__ == "__main__":
app()