Public/New-specPSModuleObject.ps1

function New-specPSModuleObject {
    <#
    .SYNOPSIS
        Creates a new instance of the psmodule class for managing PowerShell modules.
 
    .DESCRIPTION
        The `New-specPSModuleObject` function instantiates an object of the psmodule class
        based on the provided parameters. This allows users to manage PowerShell modules easily
        by specifying the module name, version, scope, and license acceptance. If only the
        module name is provided, a default instance is created. If the version, scope, and
        license acceptance are specified, a more specific instance is created.
 
    .PARAMETER ModuleName
        The name of the PowerShell module to be managed. This parameter is required.
 
    .PARAMETER Version
        The specific version of the PowerShell module. This parameter is optional. If not
        specified, the latest version of the module will be used.
 
    .PARAMETER Scope
        The scope in which the module will be installed. Valid options are CurrentUser
        (installs the module for the current user only) or AllUsers (installs the module
        for all users on the system). The default value is AllUsers.
 
    .PARAMETER AcceptLicense
        A boolean value indicating whether to accept the license agreement during installation.
        The default value is $true. If set to $false, the installation will not proceed
        if the module requires license acceptance.
 
    .EXAMPLES
 
        # Example 1: Create an instance of the psmodule class with only the module name.
        $module = New-specPSModuleObject -ModuleName "Spec.Base.Utilities"
        # This creates a default psmodule object for the specified module.
 
        # Example 2: Create an instance of the psmodule class with module name and version.
        $moduleWithVersion = New-specPSModuleObject -ModuleName "Spec.Base.Utilities" -Version "1.0.9"
        # This creates a psmodule object for the specified module with the given version.
 
        # Example 3: Create an instance with module name, version, scope, and license acceptance.
        $detailedModule = New-specPSModuleObject -ModuleName "Spec.Base.Utilities" -Version "1.0.9" -Scope "CurrentUser" -AcceptLicense $true
        # This creates a psmodule object configured with specific parameters for detailed management.
 
        # Example 4: Create an instance with module name and specify the installation scope only.
        $userScopeModule = New-specPSModuleObject -ModuleName "Spec.Base.Utilities" -Scope "CurrentUser"
        # This creates a psmodule object with the specified scope, defaulting to all other parameters.
 
    .NOTES
        Author: owen.heaume
        Version: 1.0 - initial release
    #>



    param (
        [Parameter(Mandatory = $true)]
        [string]$ModuleName,

        [string]$Version,

        [ValidateSet('CurrentUser', 'AllUsers')]
        [string]$Scope = 'AllUsers',

        [bool]$AcceptLicense = $true
    )

    # Instantiate the object based on which parameters are provided
    if (-not $PSBoundParameters.ContainsKey('Version')) {
        return [psmodule]::new($ModuleName)
    } elseif (-not $PSBoundParameters.ContainsKey('Scope') -and -not $PSBoundParameters.ContainsKey('AcceptLicense')) {
        return [psmodule]::new($ModuleName, $Version)
    } else {
        return [psmodule]::new($ModuleName, $Version, $Scope, $AcceptLicense)
    }
}