From 0e69340331adddc60021387a3a9dd24467a52889 Mon Sep 17 00:00:00 2001 From: dyoder Date: Mon, 21 Sep 2020 09:47:00 -0400 Subject: [PATCH] improvements --- Tools.ps1 | 54 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/Tools.ps1 b/Tools.ps1 index d3f0cf7..86f08d6 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -27,18 +27,19 @@ Function Log param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message, [Parameter(Mandatory=$false)][switch]$Error=$false, - [Parameter(Mandatory=$false)][string]$LogName + [Parameter(Mandatory=$false)][string]$Name ) $Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" If ( $Error ) { $LogEntry = "${Timestamp} - Error: ${Message}" } Else { $LogEntry = "${Timestamp} - ${Message}" } -If ( $LogName ) +If ( $Name ) { - $LogFile = "${MSPLogs}\${LogFilePrefix}-${LogName}.log" + $LogFile = "${MSPLogs}\${LogFilePrefix}-${Name}.log" If (! (Test-Path $LogFile) ) { New-Item -Path $LogFile -ItemType File -Force | Out-Null } Add-Content -Path $LogFile -Value $LogEntry + Write-Output $LogEntry } -Write-Output $LogEntry +Else { Write-Output $LogEntry } } ## Function to determine if the current user context is an Administrator @@ -51,21 +52,26 @@ If ( ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]: ## Runs a script from the repo Function Run-PSScript { -param([string]$Type='management-scripts',[string]$Name,[string]$Arguments) +param( + [Parameter(Mandatory=$false)][string]$Type='management-scripts', + [Parameter(Mandatory=$true)][string]$Name, + [Parameter(Mandatory=$false)][string]$Arguments="null" +) $LogName = $MyInvocation.MyCommand If ( $Arguments -eq "null" ) { $Arguments = $null } -If ( $Name -and $Type ) +If ( $Name ) { $URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1" - Log "Retrieving : ${Name}.ps1" -LogName $LogName + Log "Retrieving : ${Name}.ps1" -Name $LogName Try { $Script = (New-Object Net.WebClient).DownloadString($URL) } - Catch { $_.Exception.Message | Log -Error -LogName $LogName } + Catch { $_.Exception.Message | Log -Error -Name $LogName } Try { $ScriptBlock = [Scriptblock]::Create($Script) } - Catch { $_.Exception.Message | Log -Error -LogName $LogName } - Log "Executing: ${Name}.ps1 ${Arguments}" -LogName $LogName + Catch { $_.Exception.Message | Log -Error -Name $LogName } + Log "Executing: ${Name}.ps1 ${Arguments}" -Name $LogName Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments } - Catch { $_.Exception.Message | Log -Error -LogName $LogName } -} Else { Log "No script or type was specified" -LogName $LogName } + Catch { $_.Exception.Message | Log -Error -Name $LogName } +} +Else { Log "No script was specified" -Name $LogName } } ## Runs a script local to the endpoint @@ -73,39 +79,39 @@ Function Run-LocalInstallScript { param( [Parameter(Mandatory=$true)][string]$Script, - [Parameter(Mandatory=$true)][string]$Arguments, - [Parameter(Mandatory=$true)][string]$AppName + [Parameter(Mandatory=$false)][string]$Arguments="null", + [Parameter(Mandatory=$false)][string]$AppName="null" ) $LogName = $MyInvocation.MyCommand If ( $Arguments -eq "null" ) { $Arguments = $null } -If ( Test-Path "${Script}" ) +If ( Test-Path $Script ) { If ( $AppName -ne "null" ) { - Log "Checking for running instances of ${AppName}" -LogName $LogName + Log "Checking for running instances of ${AppName}" -Name $LogName $RunningInstances = Get-Process | Where { $_.Name -like "${AppName}" } If (! ($RunningInstances) ) { - Log "Executing: ${Script} ${Arguments}" -LogName $LogName - Try { Start-Process "${Script}" -ArgumentList "{$Arguments}" -Wait } - Catch { $_.Exception.Message | Log -Error -LogName $LogName } + Log "Executing: ${Script} ${Arguments}" -Name $LogName + Try { Start-Process "${Script}" -ArgumentList "${Arguments}" -Wait } + Catch { $_.Exception.Message | Log -Error -Name $LogName } } - Else { Log "Dependent apps are currently in use and preventing the specified script from running" -LogName $LogName } + Else { Log "Dependent apps are currently in use and preventing the specified script from running" -Name $LogName } } Else { - Log "Executing: ${Script} ${Arguments}" -LogName $LogName + Log "Executing: ${Script} ${Arguments}" -Name $LogName Try { Start-Process "${Script}" -ArgumentList "{$Arguments}" -Wait } - Catch { $_.Exception.Message | Log -Error -LogName $LogName } + Catch { $_.Exception.Message | Log -Error -Name $LogName } } } -Else { Log "The specified script does not exist: ${Script}" -LogName $LogName } +Else { Log "The specified script does not exist: ${Script}" -Name $LogName } } ## Function to return a Powershell version object from a string Function Get-Version { -param([string]$Version) +param([Parameter(Mandatory=$true)][string]$Version) [int]$Sets = 4 $VersionArray = $Version.Split('.') If ( $VersionArray.Count -le $Sets ) { $Sets = $VersionArray.Count }