From 27614e531d255e9c1fa72be17a654e68d013f996 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 25 Mar 2018 21:14:03 +0200 Subject: [PATCH] ProcessModule() display a message if action does not exist --- cleanW10.ps1 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cleanW10.ps1 b/cleanW10.ps1 index e00c84d..f2121c9 100755 --- a/cleanW10.ps1 +++ b/cleanW10.ps1 @@ -486,17 +486,26 @@ function ProcessModuleFile { foreach( $p in $_.psobject.properties.name ){ $current_action[$p] = $_.$p } + if ( -not $current_action.ContainsKey('action') ) { + Write-Host -ForegroundColor Red "`tError : action not found" + return + } # 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" + Write-Host -ForegroundColor Red "`tError in $($mod.name) : file $action_file not found`n" return } $current_action.file = $action_file } # Invoke function - Invoke-Expression "$($_.action) `$current_action" + if (Get-Command $($_.action) -ErrorAction SilentlyContinue ){ + Invoke-Expression "$($_.action) `$current_action" + } + else { + Write-Host -ForegroundColor Red "`tError in $($mod.name) : action $($_.action) not exist" + } } }