From 267e7a913d8e204545f2ac79673a7e122c75a30c Mon Sep 17 00:00:00 2001 From: dyoder Date: Sat, 19 Sep 2020 11:14:01 -0400 Subject: [PATCH] functional improvements --- Start-LinuxUpdate.sh | 38 +++++++++++++------------- Tools.ps1 | 65 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 71 insertions(+), 32 deletions(-) diff --git a/Start-LinuxUpdate.sh b/Start-LinuxUpdate.sh index 4729f36..714d533 100644 --- a/Start-LinuxUpdate.sh +++ b/Start-LinuxUpdate.sh @@ -2,40 +2,40 @@ function process_debian() { - ## Update repos - apt-get update + ## Update repos + apt-get update - ## Upgrade packages - apt-get upgrade -y + ## Upgrade packages + apt-get upgrade -y - ## Remove unused packages - apt autoremove + ## Remove unused packages + apt autoremove - ## Remove old cached packages - apt autoclean + ## Remove old cached packages + apt autoclean } function process_rhel() { - ## Clean cache - yum clean all + ## Clean cache + yum clean all - ## Update all packages, skipping problematic dependencies - yum update --skip-broken + ## Update all packages, skipping problematic dependencies + yum update --skip-broken } function process_fedora() { - ## Update Fedora release - dnf upgrade --refresh + ## Update Fedora release + dnf upgrade --refresh } if [ -n "$(command -v apt-get)" ]; then - process_debian + process_debian elif [ -n "$(command -v yum)" ]; then - process_rhel + process_rhel elif [ -n "$(command -v dnf)" ]; then - process_fedora + process_fedora else - echo "Package manager not found!" -fi \ No newline at end of file + echo "Package manager not found!" +fi diff --git a/Tools.ps1 b/Tools.ps1 index 2d6e425..eae7e63 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -13,19 +13,24 @@ $LogFilePrefix = Get-Date -Format "yyyyMMddHHmmss" ## Make sure all the management directories exist If ( !(Test-Path $MSPRoot) ) -{ New-Item -Path $MSPRoot -ItemType Directory -Force | Out-Null } +{ New-Item -Path $MSPRoot -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } If ( !(Test-Path $MSPScripts) ) -{ New-Item -Path $MSPScripts -ItemType Directory -Force | Out-Null } +{ New-Item -Path $MSPScripts -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } If ( !(Test-Path $MSPTools) ) -{ New-Item -Path $MSPTools -ItemType Directory -Force | Out-Null } +{ New-Item -Path $MSPTools -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } If ( !(Test-Path $MSPLogs) ) -{ New-Item -Path $MSPLogs -ItemType Directory -Force | Out-Null } +{ New-Item -Path $MSPLogs -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } ## Function to log activity to the configured log directory Function Log { -param([string]$Message,[string]$LogName) -$LogEntry = "{0} - {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $Message +param( + [Parameter(Mandatory=$true)][string]$Message, + [Parameter(Mandatory=$false)][switch]$Error, + [Parameter(Mandatory=$false)][string]$LogName +) +If ( $Error ) { $LogEntry = "{0} - Error: {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $Message } +Else { $LogEntry = "{0} - {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $Message } If ($LogName ) { $LogFile = "${MSPLogs}\${LogFilePrefix}-${LogName}.log" @@ -50,15 +55,49 @@ If ( $Arguments -eq "null" ) { $Arguments = $null } If ( $Name -and $Type ) { $URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1" - Write-Output "Retrieving : ${Name}.ps1" + Log "Retrieving : ${Name}.ps1" Try { $Script = (New-Object Net.WebClient).DownloadString($URL) } - Catch { Write-Output $_.Exception.Message } + Catch { Log -Message $_.Exception.Message -Error } Try { $ScriptBlock = [Scriptblock]::Create($Script) } - Catch { Write-Output $_.Exception.Message } - Write-Output "Executing: ${Name}.ps1 ${Arguments}" + Catch { Log -Message $_.Exception.Message -Error } + Log "Executing: ${Name}.ps1 ${Arguments}" Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments } - Catch { Write-Output $_.Exception.Message } -} Else { Write-Output "No script or type was specified" } + Catch { Log -Message $_.Exception.Message -Error } +} Else { Log "No script or type was specified" } +} + +## Runs a script local to the endpoint +Function Run-LocalInstallScript +{ +param( + [Parameter(Mandatory=$true)][string]$Script, + [Parameter(Mandatory=$true)][string]$Arguments, + [Parameter(Mandatory=$true)][string]$AppName +) +If ( $AppName -eq "null" ) { $AppName = $false } +If ( $Arguments -eq "null" ) { $Arguments = $null } +If ( Test-Path "${Script}" ) +{ + If ( $AppName ) + { + Log "Checking for running instances of ${AppName}" + $RunningInstances = Get-Process | Where { $_.Name -like "${AppName}" } + If (! ($RunningInstances) ) + { + Log "Executing: ${Script} ${Arguments}" + Try { Start-Process "${Script}" -ArgumentList "{$Arguments}" -Wait } + Catch { Log -Message $_.Exception.Message -Error } + } + Else { Log "${AppName} is currently in use and will not be installed now" } + } + Else + { + Log "Executing: ${Script} ${Arguments}" + Try { Start-Process "${Script}" -ArgumentList "{$Arguments}" -Wait } + Catch { Log -Message $_.Exception.Message -Error } + } +} +Else { Log "The specified script does not exist: ${Script}" } } ## Function to return a Powershell version object from a string @@ -115,4 +154,4 @@ switch ($PSCmdlet.ParameterSetName) 'le' { If ( $WindowsBuild -le $le ) { Return $true } Else { Return $false } } 'eq' { If ( $WindowsBuild -eq $eq ) { Return $true } Else { Return $false } } } -} +} \ No newline at end of file