2020-09-09 21:06:44 -04:00
## Define some basic variables
$MSPName = "Emberkom"
## Setup the MSP management directories
If ( Test-Path ${ Env : ProgramData } )
{ $MSPRoot = " ${Env:ProgramData} \ ${MSPName} " } Else { $MSPRoot = " ${Env:SystemDrive} \ ${MSPName} " }
$MSPScripts = " ${MSPRoot} \Scripts"
$MSPTools = " ${MSPRoot} \Tools"
$MSPLogs = " ${MSPRoot} \Logs"
## Make sure all the management directories exist
If ( !( Test-Path $MSPRoot ) )
2020-09-19 11:14:01 -04:00
{ New-Item -Path $MSPRoot -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null }
2020-09-09 21:06:44 -04:00
If ( !( Test-Path $MSPScripts ) )
2020-09-19 11:14:01 -04:00
{ New-Item -Path $MSPScripts -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null }
2020-09-09 21:06:44 -04:00
If ( !( Test-Path $MSPTools ) )
2020-09-19 11:14:01 -04:00
{ New-Item -Path $MSPTools -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null }
2020-09-09 21:06:44 -04:00
If ( !( Test-Path $MSPLogs ) )
2020-09-19 11:14:01 -04:00
{ New-Item -Path $MSPLogs -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null }
2020-09-09 21:06:44 -04:00
2020-09-19 15:29:31 -04:00
## Set a datestamp for any log files that might need to be created
$LogFilePrefix = Get-Date -Format "yyyyMMddHHmmss"
2020-09-07 13:43:37 -04:00
## Function to log activity to the configured log directory
2020-09-09 21:06:44 -04:00
Function Log
2020-09-07 13:43:37 -04:00
{
2020-09-19 11:14:01 -04:00
param (
2020-09-19 13:27:09 -04:00
[ Parameter ( Mandatory = $true , ValueFromPipeline = $true )][ string ] $Message ,
2020-09-19 16:40:07 -04:00
[ Parameter ( Mandatory = $false )][ switch ] $Error = $false ,
2020-09-21 09:47:00 -04:00
[ Parameter ( Mandatory = $false )][ string ] $Name
2020-09-19 11:14:01 -04:00
)
2020-09-19 16:40:07 -04:00
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
If ( $Error ) { $LogEntry = " ${Timestamp} - Error: ${Message} " }
Else { $LogEntry = " ${Timestamp} - ${Message} " }
2020-09-21 09:47:00 -04:00
If ( $Name )
2020-09-09 21:06:44 -04:00
{
2020-09-21 09:47:00 -04:00
$LogFile = " ${MSPLogs} \ ${LogFilePrefix} - ${Name} .log"
2020-09-09 21:06:44 -04:00
If (! ( Test-Path $LogFile ) ) { New-Item -Path $LogFile -ItemType File -Force | Out-Null }
Add-Content -Path $LogFile -Value $LogEntry
2020-09-21 09:47:00 -04:00
Write-Output $LogEntry
2020-09-09 21:06:44 -04:00
}
2020-09-21 09:47:00 -04:00
Else { Write-Output $LogEntry }
2020-09-07 13:43:37 -04:00
}
## Function to determine if the current user context is an Administrator
2020-09-09 21:06:44 -04:00
Function IsAdmin
2020-09-07 13:43:37 -04:00
{
If ( ([ Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity ]:: GetCurrent ()). IsInRole ([ Security.Principal.WindowsBuiltInRole ]:: Administrator ) )
{ Return $true } Else { Return $false }
}
## Runs a script from the repo
2020-09-09 21:06:44 -04:00
Function Run-PSScript
2020-09-07 13:43:37 -04:00
{
2020-09-21 09:47:00 -04:00
param (
[ Parameter ( Mandatory = $false )][ string ] $Type = 'management-scripts' ,
[ Parameter ( Mandatory = $true )][ string ] $Name ,
[ Parameter ( Mandatory = $false )][ string ] $Arguments = "null"
)
2020-09-19 15:19:38 -04:00
$LogName = $MyInvocation . MyCommand
2020-09-07 13:43:37 -04:00
If ( $Arguments -eq "null" ) { $Arguments = $null }
2020-09-21 09:47:00 -04:00
If ( $Name )
2020-09-07 13:43:37 -04:00
{
$URL = "https://dev.emberkom.com/emberkom/ ${Type} /raw/branch/master/ ${Name} .ps1"
2020-09-21 09:47:00 -04:00
Log "Retrieving : ${Name} .ps1" -Name $LogName
2020-09-07 13:43:37 -04:00
Try { $Script = ( New-Object Net . WebClient ). DownloadString ( $URL ) }
2020-09-21 09:47:00 -04:00
Catch { $_ . Exception . Message | Log -Error -Name $LogName }
2020-09-07 13:43:37 -04:00
Try { $ScriptBlock = [ Scriptblock ]:: Create ( $Script ) }
2020-09-21 09:47:00 -04:00
Catch { $_ . Exception . Message | Log -Error -Name $LogName }
Log "Executing: ${Name} .ps1 ${Arguments} " -Name $LogName
2020-09-07 13:43:37 -04:00
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
2020-09-21 09:47:00 -04:00
Catch { $_ . Exception . Message | Log -Error -Name $LogName }
}
Else { Log "No script was specified" -Name $LogName }
2020-09-19 11:14:01 -04:00
}
## Runs a script local to the endpoint
Function Run-LocalInstallScript
{
param (
[ Parameter ( Mandatory = $true )][ string ] $Script ,
2020-09-21 09:47:00 -04:00
[ Parameter ( Mandatory = $false )][ string ] $Arguments = "null" ,
[ Parameter ( Mandatory = $false )][ string ] $AppName = "null"
2020-09-19 11:14:01 -04:00
)
2020-09-19 15:19:38 -04:00
$LogName = $MyInvocation . MyCommand
2020-09-19 11:14:01 -04:00
If ( $Arguments -eq "null" ) { $Arguments = $null }
2020-09-21 09:47:00 -04:00
If ( Test-Path $Script )
2020-09-19 11:14:01 -04:00
{
2020-09-19 13:27:09 -04:00
If ( $AppName -ne "null" )
2020-09-19 11:14:01 -04:00
{
2020-09-21 09:47:00 -04:00
Log "Checking for running instances of ${AppName} " -Name $LogName
2020-09-19 11:14:01 -04:00
$RunningInstances = Get-Process | Where { $_ . Name -like " ${AppName} " }
If (! ( $RunningInstances ) )
{
2020-09-21 09:47:00 -04:00
Log "Executing: ${Script} ${Arguments} " -Name $LogName
Try { Start-Process " ${Script} " -ArgumentList " ${Arguments} " -Wait }
Catch { $_ . Exception . Message | Log -Error -Name $LogName }
2020-09-19 11:14:01 -04:00
}
2020-09-21 09:47:00 -04:00
Else { Log "Dependent apps are currently in use and preventing the specified script from running" -Name $LogName }
2020-09-19 11:14:01 -04:00
}
Else
{
2020-09-21 09:47:00 -04:00
Log "Executing: ${Script} ${Arguments} " -Name $LogName
2020-09-19 11:14:01 -04:00
Try { Start-Process " ${Script} " -ArgumentList "{ $Arguments }" -Wait }
2020-09-21 09:47:00 -04:00
Catch { $_ . Exception . Message | Log -Error -Name $LogName }
2020-09-19 11:14:01 -04:00
}
}
2020-09-21 09:47:00 -04:00
Else { Log "The specified script does not exist: ${Script} " -Name $LogName }
2020-09-07 13:43:37 -04:00
}
2020-09-23 15:24:26 -04:00
## Run a script from the live repo or from a local path
2020-09-23 15:28:43 -04:00
Function Run-Script
2020-09-23 15:24:26 -04:00
{
param (
## Parameters for live-ps scripts
[ Parameter ( Mandatory = $false , ParameterSetName = 'live-ps' )]
[ string ] $Type = 'management-scripts' ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'live-ps' )]
[ string ] $LivePSScript ,
## Parameters for local scripts
[ Parameter ( Mandatory = $true , ParameterSetName = 'local' )]
[ string ] $Path ,
2020-09-23 20:43:29 -04:00
#### $NoWait will prevent waiting for the specified script to finish running
[ Parameter ( Mandatory = $false , ParameterSetName = 'local' )]
[ switch ] $NoWait = $false ,
2020-09-23 15:24:26 -04:00
## Parameters for all scripts
[ Parameter ( Mandatory = $false , ParameterSetName = 'live-ps' )]
[ Parameter ( Mandatory = $false , ParameterSetName = 'local' )]
[ string ] $Arguments = 'null' ,
2020-09-23 20:43:29 -04:00
#### $HaltIfRunning is a collection of app names which, if running, will prevent the script from running
[ Parameter ( Mandatory = $false , ParameterSetName = 'live-ps' )]
[ Parameter ( Mandatory = $false , ParameterSetName = 'local' )]
[ string[] ] $HaltIfRunning ,
#### $ForceClose will forcefully close all apps specified in $HaltIfRunning
2020-09-23 15:24:26 -04:00
[ Parameter ( Mandatory = $false , ParameterSetName = 'live-ps' )]
[ Parameter ( Mandatory = $false , ParameterSetName = 'local' )]
2020-09-23 20:43:29 -04:00
[ switch ] $ForceClose = $false
2020-09-23 15:24:26 -04:00
)
$LogName = $MyInvocation . MyCommand
If ( $Arguments -eq "null" ) { $Arguments = $null }
2020-09-23 20:43:29 -04:00
If ( $Path ) { If (! ( Test-Path $Path ) ) { Log "The specified script does not exist: ${Path} " -Name $LogName ; Exit } }
If ( ( $HaltIfRunning ) -and ( $ForceClose ) -and (! ( IsAdmin ) ) ) { Log "Cannot close dependent apps because this task does not have administrative privileges" -Name $LogName ; Exit }
If ( $HaltIfRunning )
{
$Halt = $false
Log "Checking for running instances of the following dependent apps:" -Name $LogName
ForEach ( $name in $HaltIfRunning )
{
$RunningInstances = Get-Process | Where { $_ . Name -like " ${name} " }
If ( $RunningInstances )
{
If ( $ForceClose -eq $true ) { $name | Stop-Process -Force ; Log " "" ${name} "" (force closed)" -Name $LogName }
Else { $Halt = $true ; Log " "" ${name} "" (running)" -Name $LogName }
}
Else { Log " "" ${name} "" (not running)" -Name $LogName }
}
If ( $Halt -eq $true ) { Log "Dependent apps are currently in use and are preventing the specified script from running" -Name $LogName ; Exit }
}
2020-09-23 15:24:26 -04:00
switch ( $PSCmdlet . ParameterSetName )
{
'live-ps'
{
$URL = "https://dev.emberkom.com/emberkom/ ${Type} /raw/branch/master/ ${LivePSScript} .ps1"
Log "Retrieving : ${LivePSScript} .ps1" -Name $LogName
Try { $Script = ( New-Object Net . WebClient ). DownloadString ( $URL ) }
Catch { $_ . Exception . Message | Log -Name $LogName }
Try { $ScriptBlock = [ Scriptblock ]:: Create ( $Script ) }
Catch { $_ . Exception . Message | Log -Name $LogName }
Log "Executing: ${LivePSScript} .ps1 ${Arguments} "
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
Catch { $_ . Exception . Message | Log -Name $LogName }
}
'local'
{
2020-09-23 20:43:29 -04:00
Log "Executing: ${Path} ${Arguments} " -Name $LogName
Try
2020-09-23 15:24:26 -04:00
{
2020-09-23 20:43:29 -04:00
If ( $NoWait ) { Start-Process " ${Path} " -ArgumentList "{ $Arguments }" }
Else
2020-09-23 15:24:26 -04:00
{
2020-09-23 20:43:29 -04:00
Log " Waiting for script to finish..." -Name $LogName
Start-Process " ${Path} " -ArgumentList "{ $Arguments }" -Wait
Log " Finished" -Name $LogName
2020-09-23 15:24:26 -04:00
}
}
2020-09-23 20:43:29 -04:00
Catch { $_ . Exception . Message | Log -Name $LogName }
2020-09-23 15:24:26 -04:00
}
}
}
2020-09-07 15:26:19 -04:00
## Function to return a Powershell version object from a string
2020-09-07 13:43:37 -04:00
Function Get-Version
{
2020-09-21 09:47:00 -04:00
param ([ Parameter ( Mandatory = $true )][ string ] $Version )
2020-09-07 13:43:37 -04:00
[ int ] $Sets = 4
$VersionArray = $Version . Split ( '.' )
If ( $VersionArray . Count -le $Sets ) { $Sets = $VersionArray . Count }
For ( $i = 0 ; $i -le ( $Sets - 1 ); $i ++)
{ $Return = '{0}.{1}' -f $Return , $VersionArray [ $i ] }
Return [ version ] $Return . Trim ( '.' )
2020-09-07 20:18:06 -04:00
}
## Function to compare current Windows version with a supplied version
2020-09-09 16:59:40 -04:00
## The value supplied must be a string and must include at least 1 decimal
2020-09-07 20:18:06 -04:00
Function IsWindowsVersion
{
param (
2020-09-09 16:46:21 -04:00
[ Parameter ( Mandatory = $true , ParameterSetName = 'gt' )][ string ] $gt ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'ge' )][ string ] $ge ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'lt' )][ string ] $lt ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'le' )][ string ] $le ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'eq' )][ string ] $eq
2020-09-07 20:18:06 -04:00
)
[ version ] $WindowsVersion = [ System.Environment ]:: OSVersion . Version
switch ( $PSCmdlet . ParameterSetName )
{
'gt' { If ( $WindowsVersion -gt ( Get-Version $gt ) ) { Return $true } Else { Return $false } }
'ge' { If ( $WindowsVersion -ge ( Get-Version $ge ) ) { Return $true } Else { Return $false } }
'lt' { If ( $WindowsVersion -lt ( Get-Version $lt ) ) { Return $true } Else { Return $false } }
'le' { If ( $WindowsVersion -le ( Get-Version $le ) ) { Return $true } Else { Return $false } }
'eq' { If ( $WindowsVersion -eq ( Get-Version $eq ) ) { Return $true } Else { Return $false } }
}
2020-09-09 16:46:21 -04:00
}
2020-09-09 16:59:40 -04:00
## Function to compare current Windows build with a supplied version
## The value supplied must be an int
2020-09-09 16:46:21 -04:00
Function IsWindowsBuild
{
param (
2020-09-09 16:59:40 -04:00
[ Parameter ( Mandatory = $true , ParameterSetName = 'gt' )][ int ] $gt ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'ge' )][ int ] $ge ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'lt' )][ int ] $lt ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'le' )][ int ] $le ,
[ Parameter ( Mandatory = $true , ParameterSetName = 'eq' )][ int ] $eq
2020-09-09 16:46:21 -04:00
)
[ int ] $WindowsBuild = [ System.Environment ]:: OSVersion . Version . Build
switch ( $PSCmdlet . ParameterSetName )
{
'gt' { If ( $WindowsBuild -gt $gt ) { Return $true } Else { Return $false } }
'ge' { If ( $WindowsBuild -ge $ge ) { Return $true } Else { Return $false } }
'lt' { If ( $WindowsBuild -lt $lt ) { Return $true } Else { Return $false } }
'le' { If ( $WindowsBuild -le $le ) { Return $true } Else { Return $false } }
'eq' { If ( $WindowsBuild -eq $eq ) { Return $true } Else { Return $false } }
}
2020-09-19 11:14:01 -04:00
}