Skip to content

Commit

Permalink
Module Checking
Browse files Browse the repository at this point in the history
Finished work on Init-UcmRequirements

Cmdlet will check for presence of required modules and warn user if the module is missing, too old, untested, or even known bad.

Added Initialize-UcmRequirements to Module import code
  • Loading branch information
Atreidae committed May 6, 2024
1 parent 84c4616 commit 1651b02
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 101 deletions.
1 change: 1 addition & 0 deletions UcmPSTools.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@{
RootModule = 'UcmPSTools.psm1'
ModuleVersion = '<ModuleVersion>'
#<ModuleVersion> is replaced during the build with the current version
GUID = '2c5dafca-888a-44ff-b04e-938e0912d69a'
Author = 'James Arber'
CompanyName = 'UCMadScientist'
Expand Down
6 changes: 5 additions & 1 deletion UcmPSTools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ foreach ($import in @($Public)) {
}
}

## Check module requirements
Initialize-UcmRequirements

## Export all of the public functions making them available to the user
foreach ($file in $Public) {
Export-ModuleMember -Function $file.BaseName
}
}

100 changes: 0 additions & 100 deletions private/Initialize-UcmRequirements.ps1

This file was deleted.

154 changes: 154 additions & 0 deletions public/Initialize-UcmRequirements.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#PerformScriptSigning
Function Initialize-UcmRequirements
{
<#
.SYNOPSIS
Cmdlet to check the requirements for UcmPsTools are available. Displays a warning if any modules are missing
.EXAMPLE
PS> New-UcmEXHOConnection
.INPUTS
This function does not accept any input
.OUTPUT
This Cmdet returns a PSCustomObject with multiple Keys to indicate status
$Return.Status
$Return.Message
Return.Status can return one of three values
"OK" : All requirements present
"Warn" : Some requirements missing, see $return.message for more info
"Unknown" : Cmdlet reached the end of the function without returning anything, this shouldnt happen, if it does please log an issue on Github
Return.Message returns descriptive text showing the connected tenant, mainly for logging or reporting
.NOTES
Version: 1.0
Date: 04/05/2024
.VERSION HISTORY
1.0: Initial Public Release
.REQUIRED FUNCTIONS/MODULES
Modules
UcmPSTools (Install-Module UcmPSTools) Includes Cmdlets below
Cmdlets
Write-UcmLog: https://github.com/Atreidae/UcmPSTools/blob/main/public/Write-UcmLog.ps1
.REQUIRED PERMISSIONS
None
.LINK
http://www.UcMadScientist.com
https://github.com/Atreidae/UcmPsTools
#>
Param #No parameters
(

)

#Set a default logging location of the users profile folder
$Script:LogFileLocation = "$ENV:UserProfile\UcmPsTools.log"

#region FunctionSetup, Set Default Variables for HTML Reporting and Write Log
$function = 'Initialize-UcmRequirements'
[hashtable]$Return = @{}
$return.Function = $function
$return.Status = "Unknown"
$return.Message = "Function did not return a status message"

# Log why we were called
Write-UcmLog -Message "$($MyInvocation.InvocationName) called with $($MyInvocation.Line)" -Severity 1 -Component $function
Write-UcmLog -Message "Parameters" -Severity 1 -Component $function -LogOnly
Write-UcmLog -Message "$($PsBoundParameters.Keys)" -Severity 1 -Component $function -LogOnly
Write-UcmLog -Message "Parameters Values" -Severity 1 -Component $function -LogOnly
Write-UcmLog -Message "$($PsBoundParameters.Values)" -Severity 1 -Component $function -LogOnly
Write-UcmLog -Message "Optional Arguments" -Severity 1 -Component $function -LogOnly
Write-UcmLog -Message "$Args" -Severity 1 -Component $function -LogOnly

#endregion FunctionSetup

#region FunctionWork
Write-UcmLog -Message "Loading UcmPsTools and checking requirements..." -Severity 2 -Component $function

#Supported PowerShell Modules
$Modules = @{}
$Modules["MicrosoftTeams"] = @{NotBefore =[version]"5.3.0"; NotAfter=[version]"7.0.0"; Blacklist="1.2.3"}
$Modules["Microsoft.Graph"] = @{NotBefore =[version]"2.0.0"; NotAfter=[version]"2.18.0"; Blacklist="None" }
$Modules["AzureAD"] = @{NotBefore =[version]"2.0.2.180"; NotAfter=[version]"2.0.2.182"; Blacklist="None" }
$Modules["MsOnline"] = @{NotBefore =[version]"1.1.183.80"; NotAfter=[version]"1.1.183.81"; Blacklist="None" }



#azureAD
#Msonline

#Put something in return so we can += it if needed
$return.Message = ""
#actually do the checking

Foreach ($key in $modules.keys) {
$Module = (get-module $key -ListAvailable)

#first, check to see if the module is installed
if ($null -eq $Module)
{
Write-UcmLog -Message "Module $key was not found. Cmdlets requiring this module will not function" -Severity 3 -Component $function
Write-UcmLog -Message "Please install this module by running 'Install-Module $key' from a PowerShell Window" -Severity 2 -Component $function
Write-UcmLog -Message "Continuing without these modules may cause unexpected behaviour. You have been warned" -Severity 2 -Component $function

$return.Status = "Warn"
$return.Message += "Module $key Not found."
}

#Check if the modules are too old.
if ($Module.Version -lt $Modules[$key].NotBefore)
{
Write-UcmLog -Message "Module $key is too old. Cmdlets requiring this module may not function correctly!" -Severity 3 -Component $function
Write-UcmLog -Message "Please update this module by running 'Update-Module $key' from a PowerShell Window" -Severity 2 -Component $function
Write-UcmLog -Message "Continuing without these modules may cause unexpected behaviour. You have been warned" -Severity 2 -Component $function

$return.Status = "Warn"
$return.Message += "Module $key is too old."
}
#check if the modules are too new.
if ($Module.Version -gt $Modules[$key].NotAfter)
{
Write-UcmLog -Message "Module $key is too new! UcmPsTools has not been tested with this version. Cmdlets requiring this module may not function correctly!" -Severity 3 -Component $function
Write-UcmLog -Message "Please update this module by running 'Update-Module $key' from a PowerShell Window" -Severity 2 -Component $function
Write-UcmLog -Message "Continuing without these modules may cause unexpected behaviour. You have been warned" -Severity 2 -Component $function

$return.Status = "Warn"
$return.Message += "Module $key is too new."
}

#check to see if the module has been blacklisted
if ($Modules[$key].Blacklist -like "*$($module.Version)*")
{
Write-UcmLog -Message "Module $key version $($module.version) is blacklisted. There is a known issue with this version!" -Severity 3 -Component $function
Write-UcmLog -Message "Please update this module by running 'Update-Module $key' from a PowerShell Window" -Severity 2 -Component $function
Write-UcmLog -Message "User attempted to load UcmPsTools with a blacklisted module installed. UcmPsTools will not be loaded" -Severity 4 -Component $function

$return.Status = "Error"
$return.Message += "Module $key version $($module.version) is blacklisted."
Throw "Module $key version $($module.version) is blacklisted. There is a known issue with this version!"
}

}
Write-UcmLog -Message "Requirement checks completed" -Severity 2 -Component $function
return

#region FunctionCleanup
if ($return.Status -eq "Unknown")
{
Write-UcmLog -Message "Function did not return a status message, this should not happen. Please log an issue on Github" -Severity 4 -Component $function
$return.Status = "Unknown"
}

Write-UcmLog -Message "Function completed with status $($return.Status)" -Severity 2 -Component $function
return $return
#endregion FunctionCleanup

}

0 comments on commit 1651b02

Please sign in to comment.