Test-PowershellModule.ps1

<#
    .SYNOPSIS
 
    This function tests to see if a powershell module necessary for script execution is present.
 
    .DESCRIPTION
 
    This function tests to see if a powershell module necessary for script execution is present.
 
    .PARAMETER powershellModuleName
 
    The module name to test for.
 
    .PARAMETER powershellVersionTest
 
    Determines if a version test should be performed.
 
    .EXAMPLE
 
    Test-PowershellModule -powershellModuleName NAME -powershellVersionTest TRUE
 
    #>

    Function Test-PowershellModule
    {
        [cmdletbinding()]

        Param
        (
            [Parameter(Mandatory = $true)]
            [string]$powershellModuleName,
            [Parameter(Mandatory = $true)]
            [boolean]$getInstalledVersion
        )

        #Initiate the test.
        
        out-logfile -string "Entering Test-PowerShellModule"

        #Write function parameter information and variables to a log file.

        Out-LogFile -string ("PowerShellModuleName = "+$powershellModuleName)
        out-logfile -string $getInstalledVersion

        if ($getInstalledVersion -eq $TRUE)
        {
            try
            {
                out-logfile -string ("Obtain commands for installed module: "+$powershellModuleName)
                $commands = get-command -module $powershellModuleName -errorAction Stop
                out-logfile -string ("Commands version obtained: "+$commands[0].version)
                $returnVersion = $commands[0].version
            }
            catch
            {
                out-logfile -string "Unable to obtain the installed command versions."
                out-logfile -string $_ -isError:$TRUE
            }
        }
        else
        {
            try
            {
                out-logfile -string ("Obtain commands for gallery module: "+$powershellModuleName)
                $commands = find-module -name $powershellModuleName -errorAction Stop
                out-logfile -string ("Commands version obtained: "+$commands.version)
                $returnVersion = $commands.version
            }
            catch
            {
                out-logfile -string "Unable to obtain the gallery command versions."
                out-logfile -string $_ -isError:$TRUE
            }
        }

        return $returnVersion
    }