diff --git a/utilities/powershell/Invoke-BHCypherLibrary.ps1 b/utilities/powershell/Invoke-BHCypherLibrary.ps1 new file mode 100644 index 0000000..b80ab6b --- /dev/null +++ b/utilities/powershell/Invoke-BHCypherLibrary.ps1 @@ -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 + } + } +} \ No newline at end of file diff --git a/utilities/python/convert.py b/utilities/python/convert.py new file mode 100644 index 0000000..62a44dc --- /dev/null +++ b/utilities/python/convert.py @@ -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()