Compare commits

...

8 commits

View file

@ -3,7 +3,7 @@ param (
[string]$dir="modules.d",
[string]$module,
[switch]$debug = $false
)
)
#requires -RunAsAdministrator
Import-Module NetSecurity #Useful to manipulate firewall rules
@ -35,7 +35,7 @@ function BlockHost {
Write-Host "`n`tBlock host $($params.host) : "
try {
if ( $(IsHostAlreadyBlocked $HOST_FILE $params.host) ){
#If host is inhosts.conf, verify that ip is blocked in FW
#If host is inhosts.conf, verify that ip is blocked in FW
if ( $params.ContainsKey('firewall') -and $params.firewall -eq $true ) {
$tmp = Get-Content $HOST_FILE | Where { $_ -ne "$HOST_IP`t`t$($params.host)" }
Set-Content $HOST_FILE $tmp
@ -225,7 +225,7 @@ function AddRegKey {
return
}
if ( -not $params.ContainsKey('value') ) {
$params.value = ""
$params.value = $null
}
if ( -not $params.ContainsKey('type') -or $params.type -eq "" ){ $params.type="DWord" }
@ -251,13 +251,12 @@ function AddRegKey {
}
#Let's begin...
Write-Host -NoNewline "`t$($params.path.substring(0,30))...$($params.key) reg key to $($params.value) : "
Write-Host -NoNewline "`t$($params.path.substring(0,30))...$($params.key) reg key to '$($params.value)' : "
if ( -not (Test-Path $params.path) ){
Write-Host -NoNewline -ForegroundColor DarkGreen "creating path "
try {
New-Item -Path $params.path -Force | Out-Null
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "Error`n`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
@ -267,12 +266,13 @@ function AddRegKey {
# Test if the key already exist
try {
$current_value = Get-ItemPropertyValue -Path $params.path -Name $params.key
if ( $current_value -eq $params.value ) {
$current_value = (Get-ItemPropertyValue -Path $params.path -Name $params.key)
#current_value and params.value both empty need a special condition
if ( ( [string]::IsNullOrWhitespace($current_value) -and [string]::IsNullOrWhitespace($params.value) ) -or ($current_value -eq $params.value) ) {
Write-Host -ForegroundColor Yellow "Already done"
return
}
else { Write-Host -NoNewline -ForegroundColor DarkGreen "old value $current_value " }
else { Write-Host -NoNewline -ForegroundColor DarkGreen " old value $current_value - " }
}
catch {
Write-Host -NoNewline -ForegroundColor DarkGreen "new key "
@ -319,15 +319,14 @@ function DelRegKey {
return
}
try {
Get-ItemProperty -Path $params.path -Name $params.key
Get-ItemProperty -Path $params.path -Name $params.key -ErrorAction Stop | Out-Null
}
catch {
Write-Host -ForegroundColor Yellow "key already deleted"
Write-Host -ForegroundColor Yellow "key not exist (already deleted?)"
return
}
try {
#Remove-ItemProperty -Path $params.path -Name $params.key
Remove-ItemProperty -Path $params.path -Name $params.key
Write-host -ForegroundColor Green "done"
}
catch [System.Security.SecurityException]{
@ -391,6 +390,9 @@ function UninstallModernApp {
)]
[object]$params
)
if ( $params.ContainsKey('removeProvisioned') -and $params.removeProvisioned -eq $true ) {
UninstallModernProvisionedApp $params
}
if ( $params.ContainsKey('file') ) {
$pkgs = $(Get-AppxPackage -AllUsers).name
$uninstall_list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" }
@ -414,10 +416,6 @@ function UninstallModernApp {
}
else {
Write-Host -ForegroundColor Red "`tError : No name or file for action $($MyInvocation.MyCommand.Name)"
return
}
if ( $params.ContainsKey('removeProvisioned') -and $params.removeProvisioned -eq $true ) {
UninstallModernProvisionedApp $params
}
}
@ -591,18 +589,19 @@ function ExecCommand {
)
$path = $params.path.Replace("##mod_path##", $script:current_module_path)
$args = $params.arguments.Replace("##mod_path##", $script:current_module_path)
Write-Host -NoNewline "`tExecute : $path : "
Write-Host "`n`tExecute $path : "
$path = Invoke-Expression """$($path)"""
if ( -not (Test-Path $path) -and -not $path -eq "powershell" ) {
Write-Host -ForegroundColor Yellow "File not found"
if ( -not ((Test-Path $path) -or $path -eq "powershell") ) {
Write-Host -ForegroundColor Yellow "`t`tFile not found"
return
}
try {
Start-Process -NoNewWindow -wait -filepath $path -ArgumentList $args
Start-Process -NoNewWindow -wait -filepath $path -ArgumentList $args -ErrorAction Stop
Write-Host -ForegroundColor Green "`t`tExecution done"
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "Error`n`t"
write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
}
}
@ -673,7 +672,7 @@ try {
catch {
Write-Host -NoNewline -ForegroundColor Red "Error while mounting Registery`n`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
#return
return
}
#We need access to users registry hive for applying mofidication to existing users
@ -701,14 +700,15 @@ $script:users | foreach {
Write-Host -ForegroundColor Yellow "Already mounted"
}
}
Write-Host "Folder to process : $module"
if ( $debug ) {
$DebugPreference = "Continue"
}
if ( $module -and $( Test-Path $module ) ) {
Write-Host "File to process : $module"
$module | ProcessModuleFile
}
else {
Write-Host "Folder to process : $dir"
Get-ChildItem -Path $dir -Filter "*.conf" | foreach {
$_.FullName | ProcessModuleFile
}
@ -724,7 +724,7 @@ catch {
Write-Host -NoNewline -ForegroundColor Red "Error`n`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
}
0
[gc]::collect()
Write-Host "`nUnload Users hives : "
#Unmount Registery