TcXaeMgmt.psm1

#
# Script module for module 'TcXaeMgmt'
#
Set-StrictMode -Version Latest

# Summary: TcXaeMgmt is supported on Windows PowerShell 5.1 or later and PowerShell 6.0+ / 7.0+

$isCore = ($PSVersionTable.Keys -contains "PSEdition") -and ($PSVersionTable.PSEdition -ne 'Desktop')
Write-Verbose "IsCore: $isCore"
$psVersion = $PSVersionTable.PSVersion
Write-Verbose "PSVersion: $psVersion"

if ($psVersion -ge [Version]'7.2')
{
    $script:Framework = 'netstandard2.0'
    $script:FrameworkToRemove = 'net461','netcoreapp3.1','net50'
    #$script:Framework = 'net6.0'
    #$script:FrameworkToRemove = 'net461','netstandard2.0','net50','netcoreapp3.1'

}
if ($psVersion -ge [Version]'7.0')
{
    $script:Framework = 'netstandard2.0'
    $script:FrameworkToRemove = 'net461','netcoreapp3.1','net50'

    #$script:Framework = 'netcoreapp3.1'
    #$script:FrameworkToRemove = 'net461','netstandard2.0','net50'
} 
elseif ($psVersion -ge [Version]'6.0') 
{
    $script:Framework = 'netstandard2.0'
    $script:FrameworkToRemove = 'net461','netcoreapp3.1','net50'
}
else {
    
    $script:Framework = 'netstandard2.0'
    $script:FrameworkToRemove = 'net461','netcoreapp3.1','net50'
    #$script:Framework = 'net461'
    #$script:FrameworkToRemove = 'netstandard2.0','netcoreapp3.1','net50'
}

# Set up some helper variables to make it easier to work with the module
$script:PSModule = $ExecutionContext.SessionState.Module
$script:PSModuleRoot = $script:PSModule.ModuleBase
$script:PSMainAssembly = 'TwinCAT.Management.dll'

## CONSTRUCT A PATH TO THE CORRECT ASSEMBLY
$pathToAssembly = [io.path]::combine($PSScriptRoot, $script:Framework, $script:PSMainAssembly)
$pathToFramework = Join-Path -Path $script:PSModuleRoot -ChildPath $script:Framework
$pathToAssembly = Join-Path -Path $pathToFramework -ChildPath $script:PSMainAssembly

## Remove framework binaries that are not needed
#$FrameworkToRemovePath = Join-Path -Path $script:PSModuleRoot -ChildPath $script:FrameworkToRemove
#if (Test-Path $FrameworkToRemovePath)
#{
# Remove-Item $FrameworkToRemovePath -Force -Recurse
#}

# NOW LOAD THE APPROPRIATE ASSEMBLY
Write-Verbose "Loading assembly '$pathToAssembly'"
$importedModule = Import-Module -Name $pathToAssembly -PassThru