functional improvements

This commit is contained in:
2020-09-19 11:14:01 -04:00
parent 05db1ce426
commit 267e7a913d
2 changed files with 71 additions and 32 deletions
+51 -12
View File
@@ -13,19 +13,24 @@ $LogFilePrefix = Get-Date -Format "yyyyMMddHHmmss"
## Make sure all the management directories exist ## Make sure all the management directories exist
If ( !(Test-Path $MSPRoot) ) 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) ) 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) ) 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) ) 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 to log activity to the configured log directory
Function Log Function Log
{ {
param([string]$Message,[string]$LogName) param(
$LogEntry = "{0} - {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $Message [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 ) If ($LogName )
{ {
$LogFile = "${MSPLogs}\${LogFilePrefix}-${LogName}.log" $LogFile = "${MSPLogs}\${LogFilePrefix}-${LogName}.log"
@@ -50,15 +55,49 @@ If ( $Arguments -eq "null" ) { $Arguments = $null }
If ( $Name -and $Type ) If ( $Name -and $Type )
{ {
$URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1" $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) } Try { $Script = (New-Object Net.WebClient).DownloadString($URL) }
Catch { Write-Output $_.Exception.Message } Catch { Log -Message $_.Exception.Message -Error }
Try { $ScriptBlock = [Scriptblock]::Create($Script) } Try { $ScriptBlock = [Scriptblock]::Create($Script) }
Catch { Write-Output $_.Exception.Message } Catch { Log -Message $_.Exception.Message -Error }
Write-Output "Executing: ${Name}.ps1 ${Arguments}" Log "Executing: ${Name}.ps1 ${Arguments}"
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments } Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
Catch { Write-Output $_.Exception.Message } Catch { Log -Message $_.Exception.Message -Error }
} Else { Write-Output "No script or type was specified" } } 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 ## Function to return a Powershell version object from a string