Files
management-scripts/PSModule/PSModule.psm1
T
2020-08-09 17:52:30 -04:00

150 lines
4.6 KiB
PowerShell

#Requires -Version 2.0
## Set the MSP name
$MSPName = "Emberkom"
## Set the name of the environment variable used for locating this module
$MSPEnvVar = 'EKPSM'
## 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}"
## Setup the MSP management directory
If ( Test-Path ${Env:ProgramData} )
{ $RootDir = "${Env:ProgramData}\${MSPName}" } Else { $RootDir = "${Env:SystemDrive}\${MSPName}" }
$RecordKeepingDir = "${RootDir}\RecordKeeping"
$ScriptsDir = "${RootDir}\Scripts"
$ToolsDir = "${RootDir}\Tools"
$LogsDir = "${RootDir}\Logs"
## Function to create a directory and return an error if it fails
Function MakeDir
{
param([string]$Path)
Write-Output "Creating ${Path}"
Try { New-Item -Path $Path -ItemType Directory -Force | Out-Null }
Catch { Write-Output $_.Exception.Message }
}
## Make sure all the management directories exist
If ( !(Test-Path $RootDir) ) { MakeDir $RootDir }
If ( !(Test-Path $RecordKeepingDir) ) { MakeDir $RecordKeepingDir }
If ( !(Test-Path $ScriptsDir) ) { MakeDir $ScriptsDir }
If ( !(Test-Path $ToolsDir) ) { MakeDir $ToolsDir }
If ( !(Test-Path $LogsDir) ) { MakeDir $LogsDir }
## Set the path to this module
$ModulePath = "${ScriptDir}\${ModuleName}"
## Set the path to the module in an environment variable
[System.Environment]::SetEnvironmentVariable("${MSPEnvVar}", "${ModulePath}",[System.EnvironmentVariableTarget]::Machine)
## Update PATH to include the environment variable, if it's not already present
#$ListOfPaths = [System.Environment]::GetEnvironmentVariable('PATH')
#$SplitListOfPaths = $ListOfPaths.Split(';')
#$ModulePathPresent = $false
#$EnvVarString = '%{0}%' -f $MSPEnvVar
#ForEach ( $path in $SplitListOfPaths ) { If ( $path -eq $EnvVarString ) { $ModulePathPresent = $true } }
#If ( !($ModulePathPresent) )
#{
# ## Set the string to update PATH with
# $NewPathString = '{0};{1}' -f $EnvVarString,$ListOfPaths
# [System.Environment]::SetEnvironmentVariable('PATH', "${NewPathString}",[System.EnvironmentVariableTarget]::Machine)
#}
## 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, $ModulePath) }
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
Finaly { $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 $LogDir
Function global:Log
{
Begin {}
Process {}
End {}
}
## Import all modules
$ListOfModules = @(
"PSModule-Cleanup",
"PSModule-Create",
"PSModule-Fix",
"PSModule-Get",
"PSModule-Install",
"PSModule-Remove",
"PSModule-Set",
"PSModule-Start",
"PSModule-Uninstall",
"PSModule-Upload"
)
ForEach ($module in $ListOfModules) { Load-Module $module }
## Make sure this module exports only the functions that should be exported
Export-ModuleMember -Function Log -Variable RecordKeepingDir, ScriptsDir, ToolsDir, LogsDir