#Requires -Version 2.0 ## Set the name of this module $ModuleName = "PSModule" ## Define base URL for updating modules and resources $BaseURL = "https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/${ModuleName}" ## Set the path to this module $ModulePath = "${Env:MSPDir}\Scripts\${ModuleName}" ## Define the paths for where we'll place stuff on the endpoint $MSPRoot = $Env:MSPDir $MSPScripts = "${MSPRoot}\Scripts" $MSPTools = "${MSPRoot}\Tools" $MSPLogs = "${MSPRoot}\Logs" ## Function to simply import a module and return an error message if it fails Function Load-Module { param([string]$modulename) ## Get a new copy of the module Get-Module $modulename ## Form the local file path to the specified module $FilePath = "${ModulePath}\${modulename}.psm1" ## Import the module If ( Test-Path $FilePath ) { Try { Import-Module -Name $FilePath } Catch { Write-Output $_.Exception.Message } } Else { Write-Output "Module does not exist: ${FilePath}" } } ## Function to update or install a module Function Get-Module { param([string]$modulename) ## Form the URL to the specified module $ModuleURL = "${BaseURL}/${modulename}.psm1" ## Form the local file path to the specified module $FilePath = "${ModulePath}\${modulename}.psm1" ## Download the new file, overwriting the local copy If ( IsDownloadable $ModuleURL ) { Try { (New-Object Net.WebClient).DownloadFile($ModuleURL, $FilePath) } Catch { Write-Output $_.Exception.Message } } Else { Write-Output "Could not download ${ModuleURL}" } } ## Determines whether a file can be downloaded or not Function IsDownloadable { param([string]$url) ## Create a web request $HTTPRequest = [System.Net.WebRequest]::Create($url) Try { ## Get the response $HTTPResponse = $HTTPRequest.GetResponse() ## Save the status code $HTTPStatus = [int]$HTTPResponse.StatusCode } Catch [System.Net.WebException] { $HTTPResponse = $_.Exception.Response } ## Close the connection, because we're done with it Finally { $HTTPResponse.Close() } ## Test to make sure we received a status of 200 "OK" from the resource If ($HTTPStatus -eq 200) { Return $true } ## If the status code is anything other than 200, return $false Else { Return $false } } ## EXPORT FUNCTION ## Function to log activity to the configured log directory Function global:Log { Begin {} Process {} End {} } ## EXPORT FUNCTION ## Function to determine if the current user context is an Administrator Function global:IsAdmin { Begin {} Process { If ( ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) ) { Return $true } Else { Return $false } } End {} } ## EXPORT FUNCTION ## Function to retrieve and run a powershell script Function Run-PSScript { param( [string]$Type='management-scripts', [string]$Name, [string]$Arguments ) ## Form the URL to the specified script $URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1" If ( IsDownloadable $URL ) { $Script = (New-Object Net.WebClient).DownloadString($URL) $ScriptBlock = [Scriptblock]::Create($Script) Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments } Catch { Write-Output $_.Exception.Message } } Else { Write-Output "Could not retrieve ${URL}" } } ## Make sure all the management directories exist If ( !(Test-Path $MSPRoot) ) { Try { New-Item -Path $MSPRoot -ItemType Directory -Force | Out-Null } Catch { Write-Output $_.Exception.Message } } If ( !(Test-Path $MSPScripts) ) { Try { New-Item -Path $MSPScripts -ItemType Directory -Force | Out-Null } Catch { Write-Output $_.Exception.Message } } If ( !(Test-Path $MSPTools) ) { Try { New-Item -Path $MSPTools -ItemType Directory -Force | Out-Null } Catch { Write-Output $_.Exception.Message } } If ( !(Test-Path $MSPLogs) ) { Try { New-Item -Path $MSPLogs -ItemType Directory -Force | Out-Null } Catch { Write-Output $_.Exception.Message } } ## Import all modules $ListOfModules = @( "PSModule", "PSModule-SetWinEvtLog" ) ForEach ($module in $ListOfModules) { Load-Module $module } ## Make sure this module exports only the functions that should be exported Export-ModuleMember -Function Log,IsAdmin,Run-PSScript