CmxModule.psm1

<#
 
Installation
    - To install this module, copy the "CmxModule" folder to this directory:
    > [Environment]::GetFolderPath("MyDocuments") + "\WindowsPowerShell\Modules\CmxModule"
    - The module is installed now. You can use it from any powershell console.
 
Use this module
    To use the functions inside this module, load the module into the current
    powershell session:
    > Import-Module CmxModule -Force -DisableNameChecking
    Once loaded, you can now call the module functions. For example to get the version of the
    installed CMX module just call:
    > CmxGetVersion
    To check whether the module is imported into the current powershell session, just call:
    > (Get-Module | Where-Object {$_.Name -eq "CmxModule"}) -ne $null
    To remove the current module from the current powershell session call:
    > Remove-Module CmxModule
 
How to open a PowerShell console where the CMX module is loaded by default
    - Search for PowerShell in the start menu, and open its file location. This is usually:
    %appdata%\Microsoft\Windows\Start Menu\Programs\Windows PowerShell
    - Copy the shortcut "Windows PowerShell" to the same folder.
    - Open the shortcut properties and enter this code into the "Target" field:
    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoExit -Command "Write-Host 'Load CMX . . .';Import-Module CmxModule -Force -DisableNameChecking; Write-Host Done"
    - Save it. Add this shortcut to the task bar. Whenever you click it, a PowerShell console starts
    and loads the CMX module by default.
 
Function overview
    To see all command of this module, just type:
    > (Get-Command -Module CmxModule).Name
    To see all functions of this module, just type:
    > Get-Command *cmx*
 
Help
    - To get help on a certain command, e.g. CmxGetVersion, just run:
    > Get-Help CmxGetVersion
 
#>



# Includes
. ($PsScriptRoot + "\Scripts\System.ps1")
. ($PsScriptRoot + "\Scripts\String.ps1")
. ($PsScriptRoot + "\Scripts\FileSystem.ps1")
. ($PsScriptRoot + "\Scripts\TFS.ps1")
. ($PsScriptRoot + "\Scripts\VisualStudio.ps1")
. ($PsScriptRoot + "\Scripts\Build.ps1")
. ($PsScriptRoot + "\Scripts\Deploy.ps1")
. ($PsScriptRoot + "\Scripts\RemoteDesktop.ps1")
. ($PsScriptRoot + "\Scripts\DotnetCore.ps1")
. ($PsScriptRoot + "\Scripts\Scripting.ps1")
. ($PsScriptRoot + "\Scripts\Installation.ps1")

# Global User variables: Defined and changed by user.
[string] $global:WorkspaceName = "Step7_Safety_T"
[string] $global:LocalWorkspace = "D:\WS\Step7_Safety_T"
[string] $global:ServerWorkspace = "$/TIA Portal/TPE/dev/Step7_Safety_T"
[string] $global:BuildDefinition = "TPE.Step7_Safety_T.Rolling"
[string] $global:WindowsUsername = "DefaultUser"
[System.Security.SecureString] $global:WindowsPassword = "DummyPassword" | ConvertTo-SecureString -AsPlainText -Force
[string] $global:TfsUrl = "https://jupiter.tfs.siemens.net/tfs/tia"
[string] $global:BinariesFolder = "$LocalWorkspace\Binaries"

# Global System variables: Defined by system. Not visible to user.
[string] $global:ModuleName = "CmxModule"
[string] $global:VsVersion = "[17,18)"
[string] $global:VsVersion14 = "[14,15)" # VisualStudio 2015
[string] $global:VsVersion15 = "[15,16)" # VisualStudio 2017
[string] $global:VsVersion16 = "[16,17)" # VisualStudio 2019
[string] $global:VsVersion17 = "[17,18)" # VisualStudio 2022
[string] $global:VsWherePath = "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
[string] $global:CmxRootFolder = $PsScriptRoot

# Read or write the configuration file

function CmxGetDataPath()
{
    $localAppData = $env:LOCALAPPDATA
    $dataPath = "$localAppData\CmxModule"
    return $dataPath
}

function CmxGetConfigPath()
{
    $dataPath = CmxGetDataPath
    $configPath = "$dataPath\config.json"
    return $configPath
}

function CmxSaveGlobalsToFile($configPath)
{
    $data = [PSCustomObject]@{
        WorkspaceName = $global:WorkspaceName
        LocalWorkspace = $global:LocalWorkspace
        ServerWorkspace = $global:ServerWorkspace
        BuildDefinition = $global:BuildDefinition
        WindowsUsername = $global:WindowsUsername
        WindowsPassword = $global:WindowsPassword | ConvertFrom-SecureString
        TfsUrl = $global:TfsUrl
    }
    $data | ConvertTo-Json | Out-File $configPath
}

function CmxLoadGlobalsFromFile($configPath)
{
    $data = Get-Content $configPath | ConvertFrom-Json
    $global:WorkspaceName = $data.WorkspaceName
    $global:LocalWorkspace = $data.LocalWorkspace
    $global:ServerWorkspace = $data.ServerWorkspace
    $global:BuildDefinition = $data.BuildDefinition
    $global:TfsUrl = $data.TfsUrl
    $global:WindowsUsername = $data.WindowsUsername
    $global:WindowsPassword = $data.WindowsPassword | ConvertTo-SecureString
    $global:BinariesFolder = "$LocalWorkspace\Binaries"
}

$dataPath = CmxGetDataPath
$configPath = CmxGetConfigPath
if(-not (Test-Path -Path $dataPath -PathType Container))
{
    New-Item -Path $dataPath -ItemType Directory
}
if(-not (Test-Path -Path $configPath -PathType Leaf))
{
    # Write file
    CmxSaveGlobalsToFile $configPath
}
else 
{
    # Read file
    CmxLoadGlobalsFromFile $configPath
}


function CmxWriteGlobals()
{
    <#
    .SYNOPSIS
    Writes the CMX user global variables to the output.
    #>

    
    #Write-Output "VsVersion = $VsVersion"
    #Write-Output "VsVersion14 = $VsVersion14"
    #Write-Output "VsVersion15 = $VsVersion15"
    #Write-Output "VsVersion16 = $VsVersion16"
    #Write-Output "VsWherePath = $VsWherePath"
    Write-Output "TfsUrl = $TfsUrl"
    Write-Output "WorkspaceName = $global:WorkspaceName"
    Write-Output "LocalWorkspace = $global:LocalWorkspace"
    Write-Output "BuildDefinition = $global:BuildDefinition"
    Write-Output "ServerWorkspace = $global:ServerWorkspace"
    Write-Output "WindowsUsername = $global:WindowsUsername"
    Write-Output "WindowsPassword = $global:WindowsPassword"
    Write-Output "BinariesFolder = $global:BinariesFolder"
}

function CmxGetVersion()
{
    <#
    .SYNOPSIS
    Gets the version of the CMX module.
    .EXAMPLE
    $version = Get-CmxVersion
    #>

    
    return (Get-Module -Name $moduleName).Version.ToString()
    # return [System.Version]::new($CmxVersion)
}

function CmxGetRoot()
{
    <#
    .SYNOPSIS
    Gets the root folder of the CMX module e.g. "D:\UserData\z0012stm\Documents\WindowsPowerShell\Modules\CmxModule".
    #>


    return $PsScriptRoot
}

function CmxSaveWindowsCredential()
{
    # ask user for name and pwd and save it to WindowsCredentials.txt
    $global:WindowsUsername = Read-Host "Enter Username"
    $global:WindowsPassword = Read-Host "Enter Password" -AsSecureString
    $configPath = CmxGetConfigPath
    CmxSaveGlobalsToFile $configPath
}
function CmxGetWindowsCredential()
{
    # pull the credentials from the file
    $credential = $null
    if([string]::IsNullOrEmpty($global:WindowsUsername))
    {
        return $credential
    }
    [PSCredential]$credential = New-Object System.Management.Automation.PsCredential($global:WindowsUsername, $global:WindowsPassword)
    return $credential
}

# This call is not available per default.
# To make it available you have to call:
# using module CmxModule
# Then you may use the class.
# Hint: The class must be defined in the "psm1" file.
# Classes in "ps1" files are not loaded when you call:
# using module CmxModule
# The you must call:
# > [CmxInfo]::GetStaticInfo()
# or:
# $obj = New-Object CmxInfo
# $obj.GetInfo()
class CmxInfo
{
    static [string] GetStaticInfo()
    {
        return "Powershell Command Extension Module"
    }

    [string] GetInfo()
    {
        return [CmxInfo]::GetStaticInfo()
    }    
}

function CmxTestFunc1
{
    [CmdletBinding()]
    param()

    # Is shown if $DebugPreference = Continue
    # or this function has argument -Debug
    Write-Debug "A Write-Debug message"

    Write-Host "A Write-Host message 1"
    Write-Host "A Write-Host colored message" -ForegroundColor Blue -BackgroundColor White

    # Is shown if $InformationPreference = Continue
    # or this function has argument -InformationAction Continue
    Write-Information "A Write-Information message"

    # Writes to the ERROR STREAM.
    Write-Error "A Write-Error message" -Category InvalidArgument

    # Is shown if $WarningPreference = Continue or
    # this function has argument -WarningAction Continue
    Write-Warning "A Write-Warning message"

    # Is shown if $VerbosePreference = Continue or
    # this function has argument -Verbose
    Write-Verbose "A Write-Verbose message"

    # Writes to the SUCCESS STREAM.
    # If this function is assigned to a variable or piping elsewhere
    # then this message is not shown in the host.
    Write-Output "A Write-Output message"

    Write-Host "A Write-Host message 2"
}

# Caution: Do never comment out something like this. It kicks the real function out!
# function Get-Date
# {
# [CmdletBinding()]
# Param()

# Write-Host "Ain't no date today"
# return "Sorry"
# }