improvements
This commit is contained in:
@@ -27,19 +27,20 @@ Function Log
|
|||||||
param(
|
param(
|
||||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message,
|
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message,
|
||||||
[Parameter(Mandatory=$false)][switch]$Error=$false,
|
[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"
|
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||||
If ( $Error ) { $LogEntry = "${Timestamp} - Error: ${Message}" }
|
If ( $Error ) { $LogEntry = "${Timestamp} - Error: ${Message}" }
|
||||||
Else { $LogEntry = "${Timestamp} - ${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 }
|
If (! (Test-Path $LogFile) ) { New-Item -Path $LogFile -ItemType File -Force | Out-Null }
|
||||||
Add-Content -Path $LogFile -Value $LogEntry
|
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
|
## Function to determine if the current user context is an Administrator
|
||||||
Function IsAdmin
|
Function IsAdmin
|
||||||
@@ -51,21 +52,26 @@ If ( ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]:
|
|||||||
## Runs a script from the repo
|
## Runs a script from the repo
|
||||||
Function Run-PSScript
|
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
|
$LogName = $MyInvocation.MyCommand
|
||||||
If ( $Arguments -eq "null" ) { $Arguments = $null }
|
If ( $Arguments -eq "null" ) { $Arguments = $null }
|
||||||
If ( $Name -and $Type )
|
If ( $Name )
|
||||||
{
|
{
|
||||||
$URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1"
|
$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) }
|
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) }
|
Try { $ScriptBlock = [Scriptblock]::Create($Script) }
|
||||||
Catch { $_.Exception.Message | Log -Error -LogName $LogName }
|
Catch { $_.Exception.Message | Log -Error -Name $LogName }
|
||||||
Log "Executing: ${Name}.ps1 ${Arguments}" -LogName $LogName
|
Log "Executing: ${Name}.ps1 ${Arguments}" -Name $LogName
|
||||||
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
|
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
|
||||||
Catch { $_.Exception.Message | Log -Error -LogName $LogName }
|
Catch { $_.Exception.Message | Log -Error -Name $LogName }
|
||||||
} Else { Log "No script or type was specified" -LogName $LogName }
|
}
|
||||||
|
Else { Log "No script was specified" -Name $LogName }
|
||||||
}
|
}
|
||||||
|
|
||||||
## Runs a script local to the endpoint
|
## Runs a script local to the endpoint
|
||||||
@@ -73,39 +79,39 @@ Function Run-LocalInstallScript
|
|||||||
{
|
{
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory=$true)][string]$Script,
|
[Parameter(Mandatory=$true)][string]$Script,
|
||||||
[Parameter(Mandatory=$true)][string]$Arguments,
|
[Parameter(Mandatory=$false)][string]$Arguments="null",
|
||||||
[Parameter(Mandatory=$true)][string]$AppName
|
[Parameter(Mandatory=$false)][string]$AppName="null"
|
||||||
)
|
)
|
||||||
$LogName = $MyInvocation.MyCommand
|
$LogName = $MyInvocation.MyCommand
|
||||||
If ( $Arguments -eq "null" ) { $Arguments = $null }
|
If ( $Arguments -eq "null" ) { $Arguments = $null }
|
||||||
If ( Test-Path "${Script}" )
|
If ( Test-Path $Script )
|
||||||
{
|
{
|
||||||
If ( $AppName -ne "null" )
|
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}" }
|
$RunningInstances = Get-Process | Where { $_.Name -like "${AppName}" }
|
||||||
If (! ($RunningInstances) )
|
If (! ($RunningInstances) )
|
||||||
{
|
{
|
||||||
Log "Executing: ${Script} ${Arguments}" -LogName $LogName
|
Log "Executing: ${Script} ${Arguments}" -Name $LogName
|
||||||
Try { Start-Process "${Script}" -ArgumentList "{$Arguments}" -Wait }
|
Try { Start-Process "${Script}" -ArgumentList "${Arguments}" -Wait }
|
||||||
Catch { $_.Exception.Message | Log -Error -LogName $LogName }
|
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
|
Else
|
||||||
{
|
{
|
||||||
Log "Executing: ${Script} ${Arguments}" -LogName $LogName
|
Log "Executing: ${Script} ${Arguments}" -Name $LogName
|
||||||
Try { Start-Process "${Script}" -ArgumentList "{$Arguments}" -Wait }
|
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 to return a Powershell version object from a string
|
||||||
Function Get-Version
|
Function Get-Version
|
||||||
{
|
{
|
||||||
param([string]$Version)
|
param([Parameter(Mandatory=$true)][string]$Version)
|
||||||
[int]$Sets = 4
|
[int]$Sets = 4
|
||||||
$VersionArray = $Version.Split('.')
|
$VersionArray = $Version.Split('.')
|
||||||
If ( $VersionArray.Count -le $Sets ) { $Sets = $VersionArray.Count }
|
If ( $VersionArray.Count -le $Sets ) { $Sets = $VersionArray.Count }
|
||||||
|
|||||||
Reference in New Issue
Block a user