0% found this document useful (0 votes)
1 views7 pages

Windows PowerShell

The document contains a PowerShell script that defines several functions for handling actions such as searching a knowledge base, opening URLs, folders, and processes, as well as reading and writing files. It includes error handling and policy checks to determine if actions are allowed based on specified policies. Additionally, it provides a mechanism for outputting results and errors in a structured format.

Uploaded by

ateliers.bereksi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Windows PowerShell

The document contains a PowerShell script that defines several functions for handling actions such as searching a knowledge base, opening URLs, folders, and processes, as well as reading and writing files. It includes error handling and policy checks to determine if actions are allowed based on specified policies. Additionally, it provides a mechanism for outputting results and errors in a structured format.

Uploaded by

ateliers.bereksi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

$ErrorActionPreference = "Stop"

function Get-ArgValue($ActionArgs,[string]$Name){

if($null -eq $ActionArgs){ return $null }

if($ActionArgs -is [hashtable]){

if($[Link]($Name)) { return $ActionArgs[$Name] }

if($[Link]($[Link]())) { return $ActionArgs[$[Link]()] }

if($[Link]($[Link]())) { return $ActionArgs[$[Link]()] }

try {

$p = $[Link] | Where-Object { $_.Name -eq $Name } | Select-


Object -First 1

if($p){ return $[Link] }

$p = $[Link] | Where-Object { $_.Name -ieq $Name } | Select-


Object -First 1

if($p){ return $[Link] }

} catch {}

return $null

function Is-ActionAllowed($Policy,[string]$Name){

try{

if($null -eq $Policy){ return $false }

if($[Link] -and $[Link].allow_actions){

return ($[Link].allow_actions -contains $Name)

}
} catch {}

return $false

function Invoke-OrionKBSearch($args) {

try {

$q = $null

if ($args -ne $null) {

if ($[Link] -contains "q") { $q = $args.q }

elseif ($[Link] -contains "query") { $q = $[Link] }

elseif ($[Link] -contains "text") { $q = $[Link] }

if ([string]::IsNullOrWhiteSpace($q)) { $q = "" }

$py = "E:\ORION\.venv\Scripts\[Link]"

if (!(Test-Path $py)) { $py = "python" }

$script = "E:\ORION\bin\kb_search_offline.py"

if (!(Test-Path $script)) { throw "Script introuvable: $script" }

# On capture stdout+stderr

$out = & $py $script $q 2>&1 | Out-String

return @{ ok=$true; result=$out }

catch {

return @{ ok=$false; error=("ERROR kb_search: " + $_.[Link]) }

}
function Invoke-OSAction($Policy,[string]$Name,$ActionArgs){

# IMPORTANT: kb_search est une recherche LOCALE OFFLINE -> autorisée même si policy
est stricte

if ($Name -eq "kb_search") {

return Invoke-OrionKBSearch $ActionArgs

if(-not (Is-ActionAllowed $Policy $Name)){

return @{ ok=$false; error=("Action refusée par policy: " + $Name) }

try {

switch ($Name) {

"open_url" {

$url = [string](Get-ArgValue $ActionArgs "url")

if(!$url){ $url = [string](Get-ArgValue $ActionArgs "URL") }

if(!$url){ throw "url manquant (attendu: {`"url`":`"[Link] }

Start-Process $url | Out-Null

return @{ ok=$true; result=("Ouvert: " + $url) }

"open_folder" {

$p = [string](Get-ArgValue $ActionArgs "path")

if(!$p){ throw "path manquant (attendu: {`"path`":`"E:\...`"})" }

Start-Process "[Link]" $p | Out-Null


return @{ ok=$true; result=("Dossier ouvert: " + $p) }

"start_process" {

$file = [string](Get-ArgValue $ActionArgs "file")

$argl = [string](Get-ArgValue $ActionArgs "args")

if(!$file){ throw "file manquant (attendu: {`"file`":`"...exe`",`"args`":`"...`"})" }

if($argl){ Start-Process -FilePath $file -ArgumentList $argl | Out-Null }

else { Start-Process -FilePath $file | Out-Null }

return @{ ok=$true; result=("Process lancé: " + $file) }

"list_dir" {

$p = [string](Get-ArgValue $ActionArgs "path")

if(!$p){ throw "path manquant (attendu: {`"path`":`"E:\ORION`"})" }

$items = Get-ChildItem -Path $p -Force | Select-Object


Name,Mode,Length,LastWriteTime

return @{ ok=$true; result=$items }

"read_text_file" {

$p = [string](Get-ArgValue $ActionArgs "path")

if(!$p){ throw "path manquant (attendu: {`"path`":`"E:\...\[Link]`"})" }

$txt = Get-Content -Path $p -Raw

return @{ ok=$true; result=$txt }

"ensure_dir" {
$p = [string](Get-ArgValue $ActionArgs "path")

if(!$p){ throw "path manquant (attendu: {`"path`":`"E:\ORION\memory`"})" }

New-Item -ItemType Directory -Force -Path $p | Out-Null

return @{ ok=$true; result=("Dossier assuré: " + $p) }

"write_text_file" {

$p = [string](Get-ArgValue $ActionArgs "path")

$c = [string](Get-ArgValue $ActionArgs "content")

if(!$p){ throw "path manquant (attendu:


{`"path`":`"E:\...\[Link]`",`"content`":`"...`"})" }

if($null -eq $c){ $c = "" }

Set-Content -Encoding UTF8 -Path $p -Value $c

return @{ ok=$true; result=("Écrit: " + $p) }

"append_text_file" {

$p = [string](Get-ArgValue $ActionArgs "path")

$c = [string](Get-ArgValue $ActionArgs "content")

if(!$p){ throw "path manquant (attendu:


{`"path`":`"E:\...\[Link]`",`"content`":`"...`"})" }

if($null -eq $c){ $c = "" }

Add-Content -Encoding UTF8 -Path $p -Value $c

return @{ ok=$true; result=("Ajouté: " + $p) }

"read_json_file" {

$p = [string](Get-ArgValue $ActionArgs "path")

if(!$p){ throw "path manquant (attendu: {`"path`":`"E:\...\[Link]`"})" }


$raw = Get-Content -Raw -Path $p

return @{ ok=$true; result=$raw }

"write_json_file" {

$p = [string](Get-ArgValue $ActionArgs "path")

$obj = (Get-ArgValue $ActionArgs "object")

if(!$p){ throw "path manquant (attendu: {`"path`":`"E:\...\[Link]`",`"object`":...})" }

# Toujours JSON top-level (array si enumerable, sinon objet unique)

if($obj -is [[Link]]){

$json = ConvertTo-Json -Depth 12 -InputObject $obj

elseif(($obj -is [[Link]]) -and -not ($obj -is [string]) -and -


not ($obj -is [[Link]])){

$json = ConvertTo-Json -Depth 12 -InputObject $obj

else {

$json = ConvertTo-Json -Depth 12 -InputObject @($obj)

Set-Content -Encoding UTF8 -Path $p -Value $json

return @{ ok=$true; result=("JSON écrit: " + $p) }

default {

return @{ ok=$false; error=("Action inconnue: " + $Name) }

}
}

catch {

return @{ ok=$false; error=$_.[Link] }

$ACTIONS["say"] = {

param($args)

if (-not $[Link]) {

throw "say: missing 'text'"

Write-Host ""

Write-Host "ORION >>> $($[Link])" -ForegroundColor Cyan

You might also like