Add -module argument to process a single module file
This commit is contained in:
parent
e0c4d6361e
commit
b3d3ee6403
1 changed files with 51 additions and 36 deletions
87
cleanW10.ps1
87
cleanW10.ps1
|
@ -1,7 +1,7 @@
|
||||||
param (
|
param (
|
||||||
[cmdletbinding()]
|
[cmdletbinding()]
|
||||||
[string]$dir="modules.d",
|
[string]$dir="modules.d",
|
||||||
[string]$file
|
[string]$module
|
||||||
)
|
)
|
||||||
#requires -RunAsAdministrator
|
#requires -RunAsAdministrator
|
||||||
|
|
||||||
|
@ -425,7 +425,7 @@ function ExecCommand {
|
||||||
ParameterSetName="params",
|
ParameterSetName="params",
|
||||||
Position = 0
|
Position = 0
|
||||||
)]
|
)]
|
||||||
[object]$params
|
[object]$params
|
||||||
)
|
)
|
||||||
Write-Host -NoNewline "`tExecute : $($params.path) : "
|
Write-Host -NoNewline "`tExecute : $($params.path) : "
|
||||||
if ( -not (Test-Path $params.path) ) {
|
if ( -not (Test-Path $params.path) ) {
|
||||||
|
@ -439,8 +439,46 @@ function ExecCommand {
|
||||||
catch {
|
catch {
|
||||||
Write-Host -NoNewLine -ForegroundColor Red "`Error in DelFile`n`t"
|
Write-Host -NoNewLine -ForegroundColor Red "`Error in DelFile`n`t"
|
||||||
write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
|
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"
|
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
|
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Write-Host "Folder to process : $dir"
|
Write-Host "Folder to process : $module"
|
||||||
Get-ChildItem -Path $dir -Filter "*.conf" | foreach {
|
|
||||||
$module = ""
|
if ( $module -and $( Test-Path $module ) ) {
|
||||||
try {
|
$module | ProcessModuleFile
|
||||||
$filename = $_.FullName
|
}
|
||||||
$module = Get-Content $filename -Raw | ConvertFrom-Json
|
else {
|
||||||
}
|
Get-ChildItem -Path $dir -Filter "*.conf" | foreach {
|
||||||
catch {
|
$_.FullName | ProcessModuleFile
|
||||||
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"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#Unmount Registery
|
#Unmount Registery
|
||||||
try {
|
try {
|
||||||
|
|
Reference in a new issue