Error message details display with Write-Debug

This commit is contained in:
Yorick Barbanneau 2018-05-02 22:57:36 +02:00
parent 88b48f5c66
commit d7329ab94e

View file

@ -18,91 +18,91 @@ $ProgressPreference = "SilentlyContinue"
#Thanks to https://gist.github.com/markembling/173887 #Thanks to https://gist.github.com/markembling/173887
function BlockHost { function BlockHost {
param( param(
[Parameter( [Parameter(
ValueFromPipeline=$False, ValueFromPipeline=$False,
ParameterSetName="params", ParameterSetName="params",
Position = 0 Position = 0
)] )]
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
if ( -not $params.ContainsKey('firewall') -or $params.firewall -eq "" ) { if ( -not $params.ContainsKey('firewall') -or $params.firewall -eq "" ) {
$params.firewall = $false $params.firewall = $false
}
Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | Foreach{ BlockHost -params @{host=$_;firewall=$params.firewall} }
} }
Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | Foreach{ BlockHost -params @{host=$_;firewall=$params.firewall} }
}
elseif ( $params.ContainsKey('host') -and $params.host -ne "" ) { elseif ( $params.ContainsKey('host') -and $params.host -ne "" ) {
Write-Host "`n`tBlock host $($params.host) : " Write-Host "`n`tBlock host $($params.host) : "
try { try {
if ( $(IsHostAlreadyBlocked $HOST_FILE $params.host) ){ if ( $(IsHostAlreadyBlocked $HOST_FILE $params.host) ){
#If host is inhosts.conf, verify that ip is blocked in FW #If host is in hosts.conf, verify that ip is blocked in FW
if ( $params.ContainsKey('firewall') -and $params.firewall -eq $true ) { if ( $params.ContainsKey('firewall') -and $params.firewall -eq $true ) {
$tmp = Get-Content $HOST_FILE | Where { $_ -ne "$HOST_IP`t`t$($params.host)" } $tmp = Get-Content $HOST_FILE | Where { $_ -ne "$HOST_IP`t`t$($params.host)" }
Set-Content $HOST_FILE $tmp Set-Content $HOST_FILE $tmp
BlockHostByIP $params.host
$HOST_IP + "`t`t" + $params.host | Out-File -encoding ASCII -append $HOST_FILE
}
Write-Host -ForegroundColor Yellow "`t`tHost Already blocked"
}
else {
if ( $params.ContainsKey('firewall') -and $params.firewall -eq $true ) {
BlockHostByIP $params.host BlockHostByIP $params.host
} $HOST_IP + "`t`t" + $params.host | Out-File -encoding ASCII -append $HOST_FILE
$HOST_IP + "`t`t" + $params.host | Out-File -encoding ASCII -append $HOST_FILE
Write-Host -ForegroundColor Green "`t`tHost blocked"
} }
Write-Host -ForegroundColor Yellow "`t`tHost Already blocked"
} }
else {
if ( $params.ContainsKey('firewall') -and $params.firewall -eq $true ) {
BlockHostByIP $params.host
}
$HOST_IP + "`t`t" + $params.host | Out-File -encoding ASCII -append $HOST_FILE
Write-Host -ForegroundColor Green "`t`tHost blocked"
}
}
catch { catch {
Write-Host -NoNewline -ForegroundColor Red "`t`terror`n`t`t" Write-Host -ForegroundColor Red "`t`tError"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
} }
} }
else { else {
Write-Host -ForegroundColor Red "`tError : No name or file for action $($MyInvocation.MyCommand.Name)" Write-Host -ForegroundColor Red "`tError : No name or file for action $($MyInvocation.MyCommand.Name)"
} }
} }
function BlockHostByIP { function BlockHostByIP {
param( param(
[string]$hostname [string]$hostname
) )
#$resolv = [system.net.Dns]::GetHostAddresses($hostname) | Select IPAddressToString #$resolv = [system.net.Dns]::GetHostAddresses($hostname) | Select IPAddressToString
$resolv = Resolve-DnsName $hostname -ErrorAction SilentlyContinue | Where { $_.type -match "^A{1,4}$" } | select Address $resolv = Resolve-DnsName $hostname -ErrorAction SilentlyContinue | Where { $_.type -match "^A{1,4}$" } | select Address
$resolv | Foreach { $resolv | Foreach {
Write-Host -NoNewLine "`t" Write-Host -NoNewLine "`t"
$ip = $_.Address $ip = $_.Address
Write-Debug "Found a valid IP $ip" Write-Debug "Found a valid IP $ip"
$rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $ip } | Get-NetFirewallRule $rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $ip } | Get-NetFirewallRule
if ( $rule ) { if ( $rule ) {
write-host -NoNewLine "`tFW Rule exist : " Write-Host -NoNewLine " exist "
write-host -ForegroundColor yellow $rule.name Write-Host -ForegroundColor yellow $rule.name
}
else {
FwBlockOutputIP @{
ip=$ip;
name="H_$hostname"
}
}
} }
else {
FwBlockOutputIP @{
ip=$ip;
name="H_$hostname"
}
}
}
} }
function IsHostAlreadyBlocked { function IsHostAlreadyBlocked {
param([string]$filename, [string]$hostname) param([string]$filename, [string]$hostname)
$c = Get-Content $filename | where { $_ -eq "$HOST_IP`t`t$hostname" } $c = Get-Content $filename | where { $_ -eq "$HOST_IP`t`t$hostname" }
Write-Debug "`tMatch hostname on host file : $c" Write-Debug "`tMatch hostname on host file : $c"
if ( $c ) { if ( $c ) {
return $true return $true
} }
return $false return $false
} }
function FwBlockOutputIP { function FwBlockOutputIP {
param( param(
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | Foreach { FwBlockOutputIP @{ip=$_} } Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | Foreach { FwBlockOutputIP @{ip=$_} }
} }
elseif ( $params.ContainsKey('ip') ) { elseif ( $params.ContainsKey('ip') ) {
if (-not $params.ContainsKey('name') -or $params.name -eq "" ) { if (-not $params.ContainsKey('name') -or $params.name -eq "" ) {
$name = $FW_RULE_NAME_PREFIX + "_IP_" + $params.ip $name = $FW_RULE_NAME_PREFIX + "_IP_" + $params.ip
@ -113,18 +113,18 @@ function FwBlockOutputIP {
Write-Host -NoNewline "`tAdd FW IP rule $name ($($params.ip)) : " Write-Host -NoNewline "`tAdd FW IP rule $name ($($params.ip)) : "
$rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $params.ip } | Get-NetFirewallRule $rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $params.ip } | Get-NetFirewallRule
if ( $rule ) { if ( $rule ) {
write-host -NoNewLine " exist : " Write-Host -NoNewLine " exist "
write-host -ForegroundColor yellow $rule.name Write-Host -ForegroundColor yellow $rule.name
} }
else { else {
Try { Try {
New-NetFirewallRule -Name "$name" -DisplayName "$name" -Direction Outbound -Protocol any -Enabled True -Profile Any -RemoteAddress $params.ip -Action Block | Out-Null New-NetFirewallRule -Name "$name" -DisplayName "$name" -Direction Outbound -Protocol any -Enabled True -Profile Any -RemoteAddress $params.ip -Action Block | Out-Null
Write-Host -ForegroundColor Green "Done"
} }
Catch { Catch {
Write-Host -ForegroundColor Red "error" Write-Host -ForegroundColor Red "Error"
return Write-Debug $Error[0].Exception.Message
} }
Write-Host -ForegroundColor Green "done"
} }
} }
else { else {
@ -134,40 +134,41 @@ function FwBlockOutputIP {
function FwBlockProgram { function FwBlockProgram {
param ( param (
[cmdletbinding( [cmdletbinding(
DefaultParameterSetName='params' DefaultParameterSetName='params'
)] )]
[Parameter( [Parameter(
ValueFromPipeline=$False, ValueFromPipeline=$False,
ParameterSetName="params", ParameterSetName="params",
Position = 0 Position = 0
)] )]
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | Foreach { FwBlockProgram @{path=$_} } Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | Foreach { FwBlockProgram @{path=$_} }
} }
elseif ( $params.ContainsKey('path') ) { elseif ( $params.ContainsKey('path') ) {
$path = Invoke-Expression """$($params.path)""" $path = Invoke-Expression """$($params.path)"""
if ( -not $params.ContainsKey('name') -or $params.name -eq "" ) { if ( -not $params.ContainsKey('name') -or $params.name -eq "" ) {
$name = $FW_RULE_NAME_PREFIX + "_PROG_" + $params.path $name = $FW_RULE_NAME_PREFIX + "_PROG_" + $params.path
} }
$name = $FW_RULE_NAME_PREFIX + "_PROG_" + $params.name $name = $FW_RULE_NAME_PREFIX + "_PROG_" + $params.name
Write-Host -NoNewline "`tAdd FW program rule $name ($($path)) : " Write-Host -NoNewline "`tAdd FW program rule $name ($($path)) : "
if ( Get-NetFirewallRule -Name $name -ErrorAction SilentlyContinue) { if ( Get-NetFirewallRule -Name $name -ErrorAction SilentlyContinue) {
Write-Host -ForegroundColor Yellow "already exist" Write-Host -ForegroundColor Yellow "already exist"
return return
} }
if ( -not (Test-Path $path) ) { if ( -not (Test-Path $path) ) {
Write-Host -Foregroundcolor Red "Error (path not found)" Write-Host -Foregroundcolor Red "Error (path not found)"
return return
} }
try { try {
New-NetFirewallRule -Name "$name" -DisplayName "$name (program : $($params.path))" -Program "$path" -Direction Outbound -Protocol any -Enabled True -Profile Any -RemoteAddress any -Action Block | Out-Null New-NetFirewallRule -Name "$name" -DisplayName "$name (program : $($params.path))" -Program "$path" -Direction Outbound -Protocol any -Enabled True -Profile Any -RemoteAddress any -Action Block | Out-Null
Write-Host -ForegroundColor Green "done" Write-Host -ForegroundColor Green "done"
} }
catch { catch {
Write-Host -ForegroundColor Red "error" Write-Host -ForegroundColor Red "error"
Write-Debug $Error[0].Exception.Message
} }
} }
else { else {
@ -177,37 +178,37 @@ function FwBlockProgram {
function RemoveScheduledTask () { function RemoveScheduledTask () {
param ( param (
[cmdletbinding( [cmdletbinding(
DefaultParameterSetName='params' DefaultParameterSetName='params'
)] )]
[Parameter( [Parameter(
ValueFromPipeline=$False, ValueFromPipeline=$False,
ParameterSetName="params", ParameterSetName="params",
Position = 0 Position = 0
)] )]
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | foreach { RemoveScheduledTask @{name=$_} } Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | foreach { RemoveScheduledTask @{name=$_} }
} }
elseif ( $params.ContainsKey('name') ) { elseif ( $params.ContainsKey('name') ) {
$command = "Get-ScheduledTask -ErrorAction Stop -TaskName `"$($params.name)`"" $command = "Get-ScheduledTask -ErrorAction Stop -TaskName `"$($params.name)`""
if ($params.ContainsKey('path') -and $params.path -ne '') { if ($params.ContainsKey('path') -and $params.path -ne '') {
$command += " -TaskPath `"$($params.path)`"" $command += " -TaskPath `"$($params.path)`""
} }
else { $params.path="" } else { $params.path="" }
try { try {
$task = Invoke-Expression $command $task = Invoke-Expression $command
Write-Host -NoNewline "`tRemove task $($params.name) : " Write-Host -NoNewline "`tRemove task $($params.name) : "
$task | Unregister-ScheduledTask -ErrorAction SilentlyContinue -Confirm:$false $task | Unregister-ScheduledTask -ErrorAction SilentlyContinue -Confirm:$false
Write-Host -ForegroundColor Green "done" Write-Host -ForegroundColor Green "done"
} }
catch [Microsoft.PowerShell.Cmdletization.Cim.CimJobException]{ catch [Microsoft.PowerShell.Cmdletization.Cim.CimJobException]{
Write-Host -ForegroundColor Yellow "`tScheduled Task $($params.path)$($params.name) not found" Write-Host -ForegroundColor Yellow "`tScheduled Task $($params.path)$($params.name) not found"
} }
catch { catch {
Write-Host -NoNewLine -ForegroundColor Red "`tError in RemoveSheduledTask`n`t" Write-Host -NoNewLine -ForegroundColor Red "`tError in RemoveSheduledTask"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
} }
} }
else { else {
@ -217,12 +218,12 @@ function RemoveScheduledTask () {
function AddRegKey { function AddRegKey {
param( param(
[Parameter(Mandatory=$true)] [Parameter(Mandatory=$true)]
[object]$params [object]$params
) )
if ( -not $params.ContainsKey('path') -or -not $params.ContainsKey('key') ) { if ( -not $params.ContainsKey('path') -or -not $params.ContainsKey('key') ) {
Write-Host -ForegroundColor Red -NoNewline "Error in AddRegKey : no path, key or value`n" Write-Host -ForegroundColor Red -NoNewline "Error in AddRegKey : no path, key or value`n"
return return
} }
if ( -not $params.ContainsKey('value') ) { if ( -not $params.ContainsKey('value') ) {
$params.value = $null $params.value = $null
@ -258,8 +259,8 @@ function AddRegKey {
New-Item -Path $params.path -Force | Out-Null New-Item -Path $params.path -Force | Out-Null
} }
catch { catch {
Write-Host -NoNewLine -ForegroundColor Red "Error`n`t" Write-Host -NoNewLine -ForegroundColor Red "Error"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
return return
} }
} }
@ -287,16 +288,16 @@ function AddRegKey {
Write-Host -ForegroundColor Red "Error (access denied)" Write-Host -ForegroundColor Red "Error (access denied)"
} }
catch { catch {
Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t" Write-Host -NoNewLine -ForegroundColor Red "Error"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
} }
} }
function DelRegKey { function DelRegKey {
param( param(
[Parameter(Mandatory=$true)] [Parameter(Mandatory=$true)]
[object]$params [object]$params
) )
#When keypath start with HKCU, we need to apply it ro all users #When keypath start with HKCU, we need to apply it ro all users
if ( ($params.path).StartsWith("HKCU") ) { if ( ($params.path).StartsWith("HKCU") ) {
$script:users | Foreach { $script:users | Foreach {
@ -315,7 +316,7 @@ function DelRegKey {
} }
Write-Host -NoNewline "`tDelete registery key $($params.key) : " Write-Host -NoNewline "`tDelete registery key $($params.key) : "
if ( ! (Test-Path $params.path) ){ if ( ! (Test-Path $params.path) ){
Write-Host -ForegroundColor Red " Error (path not found)" Write-Host -ForegroundColor Red "Error (path not found)"
return return
} }
try { try {
@ -333,44 +334,45 @@ function DelRegKey {
Write-Host -ForegroundColor Red "Error (access denied)" Write-Host -ForegroundColor Red "Error (access denied)"
} }
catch { catch {
Write-Host -ForegroundColor Red -NoNewLine "Error`n`t" Write-Host -ForegroundColor Red -NoNewLine "Error"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
} }
} }
function DisableFeature { function DisableFeature {
param ( param (
[cmdletbinding( [cmdletbinding(
DefaultParameterSetName='params' DefaultParameterSetName='params'
)] )]
[Parameter( [Parameter(
ValueFromPipeline=$False, ValueFromPipeline=$False,
ParameterSetName="params", ParameterSetName="params",
Position = 0 Position = 0
)] )]
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | foreach { DisableFeature @{name=$_} } Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | foreach { DisableFeature @{name=$_} }
} }
elseif ( $params.ContainsKey('name') ) { elseif ( $params.ContainsKey('name') ) {
$feature = $(dism /online /Get-FeatureInfo /FeatureName:$($params.name) /English) $feature = $(dism /online /Get-FeatureInfo /FeatureName:$($params.name) /English)
$name = $feature | Select-String "Feature Name" | %{($_ -split " : ")[1]} $name = $feature | Select-String "Feature Name" | %{($_ -split " : ")[1]}
if (-not $name){ if (-not $name){
Write-Host -ForegroundColor Yellow "`tFeature $params.name not found" Write-Host -ForegroundColor Yellow "`tFeature $params.name not found"
return return
} }
Write-Host -NoNewline "`tDisable Feature $name : " Write-Host -NoNewline "`tDisable Feature $name : "
if ( $($feature | Select-String "state") -match "Disable" ){ if ( $($feature | Select-String "state") -match "Disable" ){
Write-Host -ForegroundColor Yellow "already disable" Write-Host -ForegroundColor Yellow "already disable"
return return
} }
try { try {
Dism /online /Disable-Feature /FeatureName:$name /NoRestart | Out-Null Dism /online /Disable-Feature /FeatureName:$name /NoRestart | Out-Null
Write-Host -ForegroundColor Green "done" Write-Host -ForegroundColor Green "done"
} }
catch { catch {
Write-Host -ForegroundColor Red "error" Write-Host -ForegroundColor Red "Error"
Write-Debug $Error[0].Exception.Message
} }
} }
else { else {
@ -380,16 +382,16 @@ function DisableFeature {
function UninstallModernApp { function UninstallModernApp {
param( param(
[cmdletbinding( [cmdletbinding(
DefaultParameterSetName='params' DefaultParameterSetName='params'
)] )]
[Parameter( [Parameter(
ValueFromPipeline=$False, ValueFromPipeline=$False,
ParameterSetName="params", ParameterSetName="params",
Position = 0 Position = 0
)] )]
[object]$params [object]$params
) )
if ( $params.ContainsKey('removeProvisioned') -and $params.removeProvisioned -eq $true ) { if ( $params.ContainsKey('removeProvisioned') -and $params.removeProvisioned -eq $true ) {
UninstallModernProvisionedApp $params UninstallModernProvisionedApp $params
} }
@ -400,18 +402,18 @@ function UninstallModernApp {
UninstallModernApp @{name=$_} UninstallModernApp @{name=$_}
} }
$uninstall_list | Where-Object { $_ -notin $pkgs } | Foreach { $uninstall_list | Where-Object { $_ -notin $pkgs } | Foreach {
Write-Host -ForegroundColor Yellow "`tModern App $_ not installed" Write-Debug "`tModern App $_ not installed"
} }
} }
elseif ( $params.ContainsKey('name') ) { elseif ( $params.ContainsKey('name') ) {
Write-Host -NoNewLine "`tUninstall $($params.name) : " Write-Host -NoNewLine "`tUninstall $($params.name) : "
try { try {
$(Get-AppxPackage -AllUsers | Where-Object { $_.name -like "*$($params.name)*" } | Remove-AppxPackage -AllUsers) $(Get-AppxPackage -AllUsers | Where-Object { $_.name -like "*$($params.name)*" } | Remove-AppxPackage -AllUsers)
Write-Host -ForegroundColor Green "done" Write-Host -ForegroundColor Green "done"
} }
catch { catch {
Write-Host -NoNewLine -ForegroundColor Red "Error `n`t" Write-Host -NoNewLine -ForegroundColor Red "Error"
write-Host -ForegroundColor DarkRed $_ Write-Debug $Error[0].Exception.Message
} }
} }
else { else {
@ -421,37 +423,35 @@ function UninstallModernApp {
function UninstallModernProvisionedApp { function UninstallModernProvisionedApp {
param( param(
[cmdletbinding( [cmdletbinding(
DefaultParameterSetName='params' DefaultParameterSetName='params'
)] )]
[Parameter( [Parameter(
ValueFromPipeline=$False, ValueFromPipeline=$False,
ParameterSetName="params", ParameterSetName="params",
Position = 0 Position = 0
)] )]
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
$pkgs = $(Get-AppxProvisionedPackage -Online).DisplayName $pkgs = $(Get-AppxProvisionedPackage -Online).DisplayName
$list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" } $list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" }
$pkgs | Where-Object { $_ -in $list } | Foreach { $pkgs | Where-Object { $_ -in $list } | Foreach {
UninstallModernProvisionedApp @{name=$_} UninstallModernProvisionedApp @{name=$_}
} }
$list | Where-Object { $_ -notin $pkgs } | Foreach { $list | Where-Object { $_ -notin $pkgs } | Foreach {
Write-Host -ForegroundColor Yellow "`tProvisioned App $_ not found" Write-Debug "`tProvisioned App $_ not found"
} }
} }
elseif ( $params.ContainsKey('name') ){ elseif ( $params.ContainsKey('name') ){
Write-Host -NoNewLine "`tUninstall Provisioned $($params.name) :" Write-Host -NoNewLine "`tUninstall Provisioned $($params.name) :"
try { try {
$(Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -eq $($params.name) }) | Remove-AppxProvisionedPackage -Online | Out-Null $(Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -eq $($params.name) }) | Remove-AppxProvisionedPackage -Online | Out-Null
Write-Host -ForegroundColor Green "done" Write-Host -ForegroundColor Green "done"
} }
catch { catch {
Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t" Write-Host -NoNewLine -ForegroundColor Red "`tError"
write-Host -ForegroundColor DarkRed $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
return
} }
} }
else { else {
@ -461,47 +461,46 @@ function UninstallModernProvisionedApp {
function DisableService { function DisableService {
param ( param (
[cmdletbinding( [cmdletbinding(
DefaultParameterSetName='params' DefaultParameterSetName='params'
)] )]
[Parameter( [Parameter(
ValueFromPipeline=$False, ValueFromPipeline=$False,
ParameterSetName="params", ParameterSetName="params",
Position = 0 Position = 0
)] )]
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
$services = $(Get-Service).name $services = $(Get-Service).name
$list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" } $list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" }
$services | Where-Object { $_ -in $list } | Foreach { $services | Where-Object { $_ -in $list } | Foreach {
DisableService @{name=$_} DisableService @{name=$_}
} }
$list | Where-Object { $_ -notin $services } | Foreach { $list | Where-Object { $_ -notin $services } | Foreach {
Write-Host -ForegroundColor Yellow "`t Service $_ not found" Write-Debug "`t Service $_ not found"
} }
} }
elseif ( $params.ContainsKey('name') ) { elseif ( $params.ContainsKey('name') ) {
$service = Get-Service -Name $params.name $service = Get-Service -Name $params.name
if ( -not $service ){ if ( -not $service ){
Write-Host -ForegroundColor "`t Service $($params.name) not found" Write-Host -ForegroundColor "`t Service $($params.name) not found"
return return
} }
Write-Host -NoNewline "`tDisable service $($params.name) : " Write-Host -NoNewline "`tDisable service $($params.name) : "
if ( $service.StartType -eq "Disable") { if ( $service.StartType -eq "Disable") {
Write-Host -ForegroundColor Yellow "already disabled" Write-Host -ForegroundColor Yellow "already disabled"
return return
} }
try { try {
Stop-Service -InputObject $service Stop-Service -InputObject $service
$service | Set-Service -StartupType disabled -ErrorAction Stop $service | Set-Service -StartupType disabled -ErrorAction Stop
Write-Host -ForegroundColor Green "done" Write-Host -ForegroundColor Green "done"
} }
catch { catch {
Write-Host -NoNewLine -ForegroundColor Red "Error`n`t" Write-Host -ForegroundColor Red "Error"
write-Host -ForegroundColor DarkRed $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
} }
finally { finally {
if ( $params.ContainsKey('userService') -and $params.userService -eq $true ) { if ( $params.ContainsKey('userService') -and $params.userService -eq $true ) {
@ -522,21 +521,21 @@ function DisableService {
function KillProcess { function KillProcess {
param( param(
[cmdletbinding( [cmdletbinding(
DefaultParameterSetName='params' DefaultParameterSetName='params'
)] )]
[Parameter( [Parameter(
ValueFromPipeline=$False, ValueFromPipeline=$False,
ParameterSetName="params", ParameterSetName="params",
Position = 0 Position = 0
)] )]
[object]$params [object]$params
) )
Write-Host -NoNewLine "`tKilling $($params.name) : " Write-Host -NoNewLine "`tKilling $($params.name) : "
try { try {
Stop-Process $(Get-Process $params.name -ErrorAction SilentlyContinue ) Stop-Process $(Get-Process $params.name -ErrorAction SilentlyContinue )
Write-Host -ForegroundColor Green "Done" Write-Host -ForegroundColor Green "Done"
} }
catch { catch {
Write-host -ForegroundColor Yellow "Not started" Write-host -ForegroundColor Yellow "Not started"
@ -545,21 +544,21 @@ function KillProcess {
function DelFile { function DelFile {
param ( param (
[cmdletbinding( [cmdletbinding(
DefaultParameterSetName='params' DefaultParameterSetName='params'
)] )]
[Parameter( [Parameter(
ValueFromPipeline=$False, ValueFromPipeline=$False,
ParameterSetName="params", ParameterSetName="params",
Position = 0 Position = 0
)] )]
[object]$params [object]$params
) )
$path = Invoke-Expression """$($params.path)""" $path = Invoke-Expression """$($params.path)"""
Write-Host -NoNewline "`tDelete $path : " Write-Host -NoNewline "`tDelete $path : "
if ( -not (Test-Path $path) ){ if ( -not (Test-Path $path) ){
Write-Host -ForegroundColor Yellow "not found" Write-Host -ForegroundColor Yellow "not found"
return return
} }
$command = "Remove-Item -ErrorAction SilentlyContinue -Force -Path `"$path`"" $command = "Remove-Item -ErrorAction SilentlyContinue -Force -Path `"$path`""
if ( $params.ContainsKey('recurse') -and $params.recurse -eq $true ) { if ( $params.ContainsKey('recurse') -and $params.recurse -eq $true ) {
@ -567,26 +566,26 @@ function DelFile {
} }
try { try {
Invoke-Expression $command Invoke-Expression $command
Write-Host -ForegroundColor Green "done" Write-Host -ForegroundColor Green "done"
} }
catch { catch {
Write-Host -NoNewLine -ForegroundColor Red "`Error`n`t" Write-Host -ForegroundColor Red "`tError"
write-Host -ForegroundColor DarkRed $Error[0].Exception.Message write-Debug $Error[0].Exception.Message
} }
} }
function ExecCommand { function ExecCommand {
param ( param (
[cmdletbinding( [cmdletbinding(
DefaultParameterSetName='params' DefaultParameterSetName='params'
)] )]
[Parameter( [Parameter(
ValueFromPipeline=$False, ValueFromPipeline=$False,
ParameterSetName="params", ParameterSetName="params",
Position = 0 Position = 0
)] )]
[object]$params [object]$params
) )
$path = $params.path.Replace("##mod_path##", $script:current_module_path) $path = $params.path.Replace("##mod_path##", $script:current_module_path)
$args = $params.arguments.Replace("##mod_path##", $script:current_module_path) $args = $params.arguments.Replace("##mod_path##", $script:current_module_path)
Write-Host "`n`tExecute $path : " Write-Host "`n`tExecute $path : "
@ -601,25 +600,25 @@ function ExecCommand {
} }
catch { catch {
Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t`t" Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
} }
} }
function ProcessModuleFile { function ProcessModuleFile {
param ( param (
[Parameter( [Parameter(
Mandatory=$true, Mandatory=$true,
ValueFromPipeline=$True, ValueFromPipeline=$True,
ParameterSetName="path" ParameterSetName="path"
)] )]
[string]$path [string]$path
) )
try { try {
$mod = Get-Content $(Get-ChildItem $path).FullName -Raw | ConvertFrom-Json $mod = Get-Content $(Get-ChildItem $path).FullName -Raw | ConvertFrom-Json
} }
catch { catch {
Write-Host -ForegroundColor Red "Error While Loading JSON : $path `n`n" Write-Host -ForegroundColor Red "Error While Loading JSON : $path `n`n"
#Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
return return
} }
Write-Host -ForegroundColor White "`nProcess Module $($mod.name) `n" Write-Host -ForegroundColor White "`nProcess Module $($mod.name) `n"
@ -628,20 +627,20 @@ function ProcessModuleFile {
$action_file = "" $action_file = ""
$current_action = @{} $current_action = @{}
$script:current_module_path = $(Get-ChildItem $path).DirectoryName + "\" + $(Get-ChildItem $path).BaseName + '\' $script:current_module_path = $(Get-ChildItem $path).DirectoryName + "\" + $(Get-ChildItem $path).BaseName + '\'
foreach( $p in $_.psobject.properties.name ){ foreach( $p in $_.psobject.properties.name ){
$current_action[$p] = $_.$p $current_action[$p] = $_.$p
} }
if ( -not $current_action.ContainsKey('action') ) { if ( -not $current_action.ContainsKey('action') ) {
Write-Host -ForegroundColor Red "`tError : action not found" Write-Host -ForegroundColor Red "`tError : action not found"
return return
} }
# If action content a file element, need to test if file exist # If action content a file element, need to test if file exist
if ( $current_action.ContainsKey('file')) { if ( $current_action.ContainsKey('file')) {
$action_file = $script:current_module_path + $current_action.file $action_file = $script:current_module_path + $current_action.file
if ( -not (Test-Path $action_file) ) { if ( -not (Test-Path $action_file) ) {
Write-Host -ForegroundColor Red "`tError in $($mod.name) : file $action_file not found`n" Write-Host -ForegroundColor Red "`tError in $($mod.name) : file $action_file not found`n"
return return
} }
$current_action.file = $action_file $current_action.file = $action_file
} }
# Invoke function # Invoke function
@ -671,15 +670,15 @@ try {
} }
catch { catch {
Write-Host -NoNewline -ForegroundColor Red "Error while mounting Registery`n`t" Write-Host -NoNewline -ForegroundColor Red "Error while mounting Registery`n`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
return return
} }
#We need access to users registry hive for applying mofidication to existing users #We need access to users registry hive for applying modifications to existing users
$profile_list = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" $profile_list = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\"
Get-LocalUser | Where-Object { $_.Enabled -eq $true } | foreach { Get-LocalUser | Where-Object { $_.Enabled -eq $true } | foreach {
$current_user_path = Get-ItemPropertyValue -Path "$profile_list$($_.SID.Value)\" -Name "ProfileImagePath" $current_user_path = Get-ItemPropertyValue -Path "$profile_list$($_.SID.Value)\" -Name "ProfileImagePath"
$script:users += @{name = $_.name;'sid' = $_.SID.Value; 'was_mounted' = $false; 'directory' = $current_user_path} $script:users += @{name = $_.name;'sid' = $_.SID.Value; 'was_mounted' = $false; 'directory' = $current_user_path}
} }
Write-Host "Mount users registry hives :" Write-Host "Mount users registry hives :"
@ -692,7 +691,7 @@ $script:users | foreach {
} }
catch { catch {
Write-Host -ForegroundColor Red "Error`n`t" Write-Host -ForegroundColor Red "Error`n`t"
Write-host $Error[0].Exception.Message Write-Debug $Error[0].Exception.Message
} }
} }
else { else {