Add -module argument to process a single module file

This commit is contained in:
Yorick Barbanneau 2018-03-23 23:33:08 +01:00
parent e0c4d6361e
commit b3d3ee6403

View file

@ -1,7 +1,7 @@
param (
[cmdletbinding()]
[cmdletbinding()]
[string]$dir="modules.d",
[string]$file
[string]$module
)
#requires -RunAsAdministrator
@ -425,7 +425,7 @@ function ExecCommand {
ParameterSetName="params",
Position = 0
)]
[object]$params
[object]$params
)
Write-Host -NoNewline "`tExecute : $($params.path) : "
if ( -not (Test-Path $params.path) ) {
@ -439,8 +439,46 @@ function ExecCommand {
catch {
Write-Host -NoNewLine -ForegroundColor Red "`Error in DelFile`n`t"
write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
}
}
}
function ProcessModuleFile {
param (
[Parameter(
Mandatory=$true,
ValueFromPipeline=$True,
ParameterSetName="path"
)]
[string]$path
)
try {
$mod = Get-Content $(Get-ChildItem $path).FullName -Raw | ConvertFrom-Json
}
catch {
Write-Host -ForegroundColor Red "Error While Loading JSON : $path `n`n"
#Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
return
}
Write-Host -ForegroundColor White "`nProcess Module $($mod.name) `n"
$mod.actions | Foreach {
$action_file = ""
$current_action = @{}
foreach( $p in $_.psobject.properties.name ){
$current_action[$p] = $_.$p
}
# If action content a file element, need to test if file exist
if ( $current_action.ContainsKey('file')) {
$action_file = $(Get-ChildItem $path).DirectoryName + "\" + $(Get-ChildItem $path).BaseName + "\" + $current_action.file
if ( -not (Test-Path $action_file) ) {
Write-Host -ForegroundColor Red "`tError in $($mod.name) : file $action_file not found"
return
}
$current_action.file = $action_file
}
# Invoke function
Invoke-Expression "$($_.action) `$current_action"
}
}
Write-Output "`nIt's time to kick ass and chew bubble gum"
@ -461,38 +499,15 @@ catch {
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
return
}
Write-Host "Folder to process : $dir"
Get-ChildItem -Path $dir -Filter "*.conf" | foreach {
$module = ""
try {
$filename = $_.FullName
$module = Get-Content $filename -Raw | ConvertFrom-Json
}
catch {
Write-Host -NoNewline -ForegroundColor Red "Error While Loading JSON : $filename `n`n"
#Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
return
}
Write-Host -ForegroundColor White "`nProcess Module $($module.name) `n"
$module_dir = $_.Directory.FullName + "\" + $_.BaseName + "\"
$module.actions | Foreach {
$action_file = ""
$current_action = @{}
foreach( $p in $_.psobject.properties.name ){
$current_action[$p] = $_.$p
}
# If action content a file element, need to test if file exist
if ( $current_action.ContainsKey('file')) {
$action_file = $module_dir + $current_action.file
if ( -not (Test-Path $action_file) ) {
Write-Host -ForegroundColor Red "`tError in $($module.name) : file $($_.file) not found"
return
}
$current_action.file = $action_file
}
# Invoke function
Invoke-Expression "$($_.action) `$current_action"
}
Write-Host "Folder to process : $module"
if ( $module -and $( Test-Path $module ) ) {
$module | ProcessModuleFile
}
else {
Get-ChildItem -Path $dir -Filter "*.conf" | foreach {
$_.FullName | ProcessModuleFile
}
}
#Unmount Registery
try {