## 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) ) { New-Item -Path $MSPRoot -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } If ( !(Test-Path $MSPScripts) ) { New-Item -Path $MSPScripts -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } If ( !(Test-Path $MSPTools) ) { New-Item -Path $MSPTools -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } If ( !(Test-Path $MSPLogs) ) { New-Item -Path $MSPLogs -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } ## Set a datestamp for any log files that might need to be created $LogFilePrefix = Get-Date -Format "yyyyMMddHHmmss" ## Function to log activity to the configured log directory Function Log { param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message, [Parameter(Mandatory=$false)][switch]$Error=$false, [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 ( $Name ) { $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 } Else { Write-Output $LogEntry } } ## Function to determine if the current user context is an Administrator Function IsAdmin { If ( ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) ) { Return $true } Else { Return $false } } ## Runs a script from the repo Function Run-PSScript { 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 ) { $URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1" Log "Retrieving : ${Name}.ps1" -Name $LogName Try { $Script = (New-Object Net.WebClient).DownloadString($URL) } Catch { $_.Exception.Message | Log -Error -Name $LogName } Try { $ScriptBlock = [Scriptblock]::Create($Script) } 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 -Name $LogName } } Else { Log "No script was specified" -Name $LogName } } ## Runs a script local to the endpoint Function Run-LocalInstallScript { param( [Parameter(Mandatory=$true)][string]$Script, [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 ( $AppName -ne "null" ) { Log "Checking for running instances of ${AppName}" -Name $LogName $RunningInstances = Get-Process | Where { $_.Name -like "${AppName}" } If (! ($RunningInstances) ) { 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" -Name $LogName } } Else { Log "Executing: ${Script} ${Arguments}" -Name $LogName Try { Start-Process "${Script}" -ArgumentList "{$Arguments}" -Wait } Catch { $_.Exception.Message | Log -Error -Name $LogName } } } Else { Log "The specified script does not exist: ${Script}" -Name $LogName } } ## Run a script from the live repo or from a local path Function Run-Script { 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, ## Parameters for all scripts [Parameter(Mandatory=$false,ParameterSetName='live-ps')] [Parameter(Mandatory=$false,ParameterSetName='local')] [string]$Arguments='null', [Parameter(Mandatory=$false,ParameterSetName='live-ps')] [Parameter(Mandatory=$false,ParameterSetName='local')] [string[]]$HaltIfRunning ) $LogName = $MyInvocation.MyCommand If ( $Arguments -eq "null" ) { $Arguments = $null } #If (! ($HaltIfRunning) ) { $HaltIfRunning = $null } 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' { If ( Test-Path $Path ) { $Halt = $false If ( $HaltIfRunning ) { 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 ) { $Halt = $true Log " ""${name}"" (running)" -Name $LogName } Else { Log " ""${name}""" -Name $LogName } } } If ( $Halt -eq $false ) { Log "Executing: ${Script} ${Arguments}" -Name $LogName Try { Start-Process "${Script}" -ArgumentList "{$Arguments}" -Wait } Catch { $_.Exception.Message | Log -Name $LogName } } Else { Log "Dependent apps are currently in use and are preventing the specified script from running" -Name $LogName } } Else { Log "The specified script does not exist: ${Path}" -Name $LogName } } } } ## Function to return a Powershell version object from a string Function Get-Version { param([Parameter(Mandatory=$true)][string]$Version) [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('.') } ## Function to compare current Windows version with a supplied version ## The value supplied must be a string and must include at least 1 decimal Function IsWindowsVersion { param( [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 ) [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 } } } } ## Function to compare current Windows build with a supplied version ## The value supplied must be an int Function IsWindowsBuild { param( [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 ) [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 } } } }