add logging to a file for script execution functions

This commit is contained in:
2020-09-19 15:19:38 -04:00
parent bc05958fd0
commit 59801ddf92
+15 -13
View File
@@ -51,19 +51,20 @@ If ( ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]:
Function Run-PSScript
{
param([string]$Type='management-scripts',[string]$Name,[string]$Arguments)
$LogName = $MyInvocation.MyCommand
If ( $Arguments -eq "null" ) { $Arguments = $null }
If ( $Name -and $Type )
{
$URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1"
Log "Retrieving : ${Name}.ps1"
Log "Retrieving : ${Name}.ps1" -LogName $LogName
Try { $Script = (New-Object Net.WebClient).DownloadString($URL) }
Catch { $_.Exception.Message | Log -Error }
Catch { $_.Exception.Message | Log -Error -LogName $LogName }
Try { $ScriptBlock = [Scriptblock]::Create($Script) }
Catch { $_.Exception.Message | Log -Error }
Log "Executing: ${Name}.ps1 ${Arguments}"
Catch { $_.Exception.Message | Log -Error -LogName $LogName }
Log "Executing: ${Name}.ps1 ${Arguments}" -LogName $LogName
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
Catch { $_.Exception.Message | Log -Error }
} Else { Log "No script or type was specified" }
Catch { $_.Exception.Message | Log -Error -LogName $LogName }
} Else { Log "No script or type was specified" -LogName $LogName }
}
## Runs a script local to the endpoint
@@ -74,29 +75,30 @@ param(
[Parameter(Mandatory=$true)][string]$Arguments,
[Parameter(Mandatory=$true)][string]$AppName
)
$LogName = $MyInvocation.MyCommand
If ( $Arguments -eq "null" ) { $Arguments = $null }
If ( Test-Path "${Script}" )
{
If ( $AppName -ne "null" )
{
Log "Checking for running instances of ${AppName}"
Log "Checking for running instances of ${AppName}" -LogName $LogName
$RunningInstances = Get-Process | Where { $_.Name -like "${AppName}" }
If (! ($RunningInstances) )
{
Log "Executing: ${Script} ${Arguments}"
Log "Executing: ${Script} ${Arguments}" -LogName $LogName
Try { Start-Process "${Script}" -ArgumentList "{$Arguments}" -Wait }
Catch { $_.Exception.Message | Log -Error }
Catch { $_.Exception.Message | Log -Error -LogName $LogName }
}
Else { Log "Dependent apps are currently in use and preventing the specified script from running" }
Else { Log "Dependent apps are currently in use and preventing the specified script from running" -LogName $LogName }
}
Else
{
Log "Executing: ${Script} ${Arguments}"
Log "Executing: ${Script} ${Arguments}" -LogName $LogName
Try { Start-Process "${Script}" -ArgumentList "{$Arguments}" -Wait }
Catch { $_.Exception.Message | Log -Error }
Catch { $_.Exception.Message | Log -Error -LogName $LogName }
}
}
Else { Log "The specified script does not exist: ${Script}" }
Else { Log "The specified script does not exist: ${Script}" -LogName $LogName }
}
## Function to return a Powershell version object from a string