PwSh.Fw.OS.psm1
$Script:NS = (get-item $PSCommandPath).basename $Script:os = $null # function Get-PwShFwModuleInfos { # [CmdletBinding()][OutputType([String])]Param ( # # [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][string]$string # ) # Begin { # Write-PwShFwOSEnterFunction # } # Process { # Write-Output "PSCommandPath = $PSCommandPath" # Write-Output "PSScriptRoot = $PSScriptRoot" # } # End { # Write-PwShFwOSLeaveFunction # } # } function New-OSObject { Begin { } Process { $os = New-Object -TypeName PSObject -Property @{ platform = "" kernel = "" mainstream = "" kernelVersion = "0.0.0.0" family = "" distrib = "" edition = "" installType = "" version = "" displayVersion = "" releaseId = "" productName = "" codeName = "" longCodeName = "" name = "" arch = "" Online = $true # root folder of disk containing OS # '/' for online Unix/Linux systems # 'c:\' for online Windows systems Root = "" # root folder of OS on disk (from the OS point of view) # for online OS, $Root and $SystemRoot may be the same, especially unix and derivatives # for offline OS, $Root is the location of OS from the running OS, $SystemRoot is the location from the offline OS # '/' on Unix/Linux systems # 'c:\Windows' on Windows systems (can be C:\winnt on older systems) SystemRoot = "" files = @{} folders = @{} packages = [ordered]@{} } return $os } End { } } <# .SYNOPSIS Get an operating system object. .DESCRIPTION Returns an Operating System object. An Operating System object is an object containing full identification of an Operating System. That is its version, build number, codename, and so on. .EXAMPLE Get-OperatingSystem -Online .EXAMPLE Get-OperatingSystem -Root '/mnt/sda3' .EXAMPLE Get-OperatingSystem -Root F:\ .NOTES General notes #> function Get-OperatingSystem { [CmdletBinding(DefaultParameterSetName="ROOT")]Param ( # The path to the root of an OS. [Parameter(ParameterSetName = "ROOT", Mandatory = $true, ValueFromPipeLine = $true)][string]$Root, # True to specify to fetch information from the running OS [Parameter(ParameterSetName = "ONLINE", Mandatory = $true, ValueFromPipeLine = $true)][switch]$Online, # Force reload/redetect things [switch]$Force ) Begin { Write-PwShFwOSEnterFunction $os = New-OSObject } Process { switch ($PSCmdlet.ParameterSetName) { 'ONLINE' { $Root = Get-OSRoot -Online if ($Script:os -and (-not $Force)) { return $Script:os } } 'ROOT' { $Root = Get-OSRoot -Path $Root } } if ([string]::IsNullOrEmpty($Root)) { return $false } $os.Root = $Root $os.online = Test-OSIsOnline -Root $Root $os.platform = Get-OSPlatform -Root $Root $os.kernel = Get-OSKernel -Root $Root # 2024.11.12 - deprecated # $os.mainstream = Get-OSMainstream -Kernel $os.kernel $os.mainstream = $os.platform # From this point we need to load som librairies of OS to be discovered # $null = Load-Libraries -Platform $os.platform -Kernel $os.kernel # fill in attributes $attributes = @('family', 'distrib', 'kernelVersion', 'edition', 'version', 'releaseId', 'displayVersion', 'productName', 'codeName', 'longCodeName', 'name', 'arch', 'installType', 'files', 'folders', 'systemRoot') foreach ($attribute in $attributes) { $os.$attribute = Get-OSAttribute -OS $os -Attribute $attribute } # From this point we need to load some dictionaries for currently running OS to go further if ($IsLinux) { $null = Load-Dictionaries -Platform "Linux" -Kernel "Linux" } elseif ($IsMacOS) { $null = Load-Dictionaries -Platform "MacOS" -Kernel "Darwin" } elseif ($IsWindows) { $null = Load-Dictionaries -Platform "Windows" -Kernel "WinNT" } switch ($PSCmdlet.ParameterSetName) { 'ONLINE' { $Script:os = $os } } return $os } End { Write-PwShFwOSLeaveFunction } } <# .SYNOPSIS Get an operating system object. .DESCRIPTION Returns an Operating System object. An Operating System object is an object containing full identification of an Operating System. That is its version, build number, codename, and so on. .NOTES General notes #> function Get-OperatingSystemObject { [CmdletBinding(DefaultParameterSetName="ROOT")]Param ( [Parameter(ParameterSetName = "ROOT", Mandatory = $true, ValueFromPipeLine = $true)][string]$Root, [Parameter(ParameterSetName = "ONLINE", Mandatory = $true, ValueFromPipeLine = $true)][switch]$Online, [switch]$ShowMissing ) Begin { # eenter($MyInvocation.MyCommand) switch ($PSCmdlet.ParameterSetName) { 'ONLINE' { if ($env:SystemDrive) { $Root = $env:SystemDrive } else { $Root = '/' } } } # Write-PwShFwOSDevel "PWSHFW_PATH = " + $Global:PWSHFW_PATH $os = [ordered]@{} # ewarn "Get-OperatingSystemObject() use custom classes and custom object type." # ewarn "It may not work under certain circumstances." # ewarn "Windows 10 Pro and Scheduled Tasks are known to not work properly." # ewarn "Please migrate your script to use Get-OperatingSystem instead." } Process { return $null } End { # eleave($MyInvocation.MyCommand) } } <# .SYNOPSIS Get informations about currently running Operating System .DESCRIPTION Try to get concise yet useful and formatted informations about running OS. The way we get informations must run in constrained language mode. .EXAMPLE $os = Get-OnlineOpratingSystem .NOTES General notes #> function Get-OnlineOperatingSystem { # [CmdletBinding()]Param ( # ) Begin { Write-PwShFwOSEnterFunction # $os = @{} $os = New-Object -TypeName PSObject -Property @{ platform = "unknown" mainstream = "unknown" kernel = "unknown" kernelVersion = "0.0.0.0" family = "unknown" distrib = "unknown" edition = "" installType = "unknown" version = "" releaseId = "" productName = "unknown" codeName = "unknown" # longCodeName = "unknown" name = "unknown" arch = "x86" Online = $true Root = "/" SystemRoot = "/" # $files = @{} # $folders = @{} packages = [ordered]@{} } } Process { # handle 3 mainstream operating systems Windows - macOS - linux if ($IsLinux) { $os.platform = "Unix" $os.mainstream = "Linux" $os.kernel = "linux" $null = Load-Module -fullyqualifiedName "$PSScriptRoot/Includes/OS.Unix/OS.Unix.psd1" -Force $null = Load-Module -fullyqualifiedName "$PSScriptRoot/Includes/OS.Linux/OS.Linux.psd1" -Force $os.kernelVersion = (Lib.Linux\Get-OSKernelVersion -Online) $os.family = (Lib.Linux\Get-OSFamily -Online) $family = (get-culture).TextInfo.ToTitleCase($os.family) -replace '[\s\p{P}]' $null = Load-Module -fullyqualifiedName "$PSScriptRoot/Includes/OS.$family/OS.$family.psd1" -Force -Policy Optional $os.distrib = (Lib.Linux\Get-OSDistrib -Online) $distrib = (get-culture).TextInfo.ToTitleCase($os.distrib) -replace '[\s\p{P}]' $null = Load-Module -fullyqualifiedName "$PSScriptRoot/Includes/OS.$distrib/OS.$distrib.psd1" -Force -Policy Optional $os.edition = (Lib.Linux\Get-OSEdition -Online) $os.installType = (Get-OSInstalltype -Online) $os.version = (Lib.Linux\Get-OSVersion -Online) $os.releaseId = (Lib.Linux\Get-OSReleaseID -Online) $os.productName = (Lib.Linux\Get-OSProductName -Online) $os.codeName = (Lib.Linux\Get-OSCodeName -Online) $os.arch = (Get-OSArch -Online) $os.online = $true $os.root = '/' $os.systemRoot = '/' $os | Add-Member -NotePropertyName files -NotePropertyValue (Lib.Linux\Get-OSFiles -Root $os.root) $os | Add-Member -NotePropertyName folders -NotePropertyValue (Lib.Linux\Get-OSFolders -Root $os.root) $os.packages = (Get-OSPackages -Online) } if ($isMacos) { $null = Load-Module -fullyqualifiedName "$PSScriptRoot\Includes\Lib.Unix\OS.Unix.psd1" -Force $null = Load-Module -fullyqualifiedName "$PSScriptRoot\Includes\Lib.Macos\OS.Macos.psd1" -Force $os.platform = "Unix" $os.mainstream = "macOS" $os.kernel = "Darwin" $os.kernelVersion = (Lib.Macos\Get-OSKernelVersion -Online) $os.distrib = "macOS" $osIdentity = Lib.Macos\Get-OSIdentity -Online $os.family = "macOS" # $os.edition = $os.installType = "Desktop" $os.version = $osIdentity.ProductVersion $os.releaseId = Lib.Macos\Get-OSReleaseId -version $os.version $os.codeName = Lib.Macos\Get-OSCodeName -ReleaseId $os.releaseId $os.productName = $osIdentity.ProductName + " " + $os.version + " (" + $os.codename + ")" $os.arch = Lib.Macos\Get-OSArch -Online $os.online = $true $os.root = '/' $os.systemRoot = '/' $os | Add-Member -NotePropertyName files -NotePropertyValue (Lib.Macos\Get-OSFiles -Root $os.root) $os | Add-Member -NotePropertyName folders -NotePropertyValue (Lib.Macos\Get-OSFolders -Root $os.root) $os.packages = (Get-OSPackages -Online) } if ($isWindows) { $null = Load-Module -fullyqualifiedName "$PSScriptRoot\Includes\Lib.Windows\OS.Windows.psd1" -Force # $os.platform = "Windows" # $os.mainstream = "Windows" # $os.kernel = "WinNT" # $os.buildNumber = Lib.Windows\Get-OSKernelVersion -Online # $os.distrib = Lib.Windows\Get-OSDistrib -Online # $os.edition = Lib.Windows\Get-OSEdition -Online # $os.installType = Lib.Windows\Get-OSInstalltype -Online # $os.version = Lib.Windows\Get-OSVersion -Online # $os.releaseId = Lib.Windows\Get-OSReleaseID -Online # $os.productName = Lib.Windows\Get-OSProductName -Online # $os.codeName = Lib.Windows\Get-OSCodeName -Online # $os.arch = Lib.Windows\Get-OSArch -Online # $os.online = $true # $os.root = $env:SystemDrive # $os.systemRoot = $env:SystemRoot # $os.files = Lib.Windows\Get-OSFiles -Root $os.root # $os.folders = Lib.Windows\Get-OSFolders -Root $os.root $os.platform = "Windows" $os.mainstream = "Windows" $os.kernel = "WinNT" $os.kernelVersion = (Lib.Windows\Get-OSKernelVersion -Online) $os.version = (Lib.Windows\Get-OSVersion -Online) $os.installType = (Lib.Windows\Get-OSInstalltype -Online) $os.releaseId = (Lib.Windows\Get-OSReleaseID -Online) $osIdentity = $os | Get-OSIdentity # for Windows before Windows 10 if ([System.String]::IsNullOrEmpty($os.releaseId)) { $os.releaseId = $osIdentity.releaseId} # for Windows Server after Windows 2012r2 if ($os.installType -eq 'Server') { if (-not([string]::IsNullOrEmpty($osIdentity.releaseId))) { $os.releaseId = $osIdentity.releaseId } } $os.family = $osIdentity.family $os.distrib = $osIdentity.distrib $os.codeName = $osIdentity.codename $os.edition = (Lib.Windows\Get-OSEdition -Online) $os.productName = (Lib.Windows\Get-OSProductName -Online) $os.arch = (Lib.Windows\Get-OSArch -Online) $os.online = $true $os.root = ($env:SystemDrive) $os.systemRoot = ($env:SystemRoot) $os | Add-Member -NotePropertyName files -NotePropertyValue (Lib.Windows\Get-OSFiles -Root $os.root) $os | Add-Member -NotePropertyName folders -NotePropertyValue (Lib.Windows\Get-OSFolders -Root $os.root) } $os.name = ($os.distrib + ' ' + $os.releaseId) # return $os | Sort-HashTable return $os } End { Write-PwShFwOSLeaveFunction } } <# .SYNOPSIS Load online operating system's dictionaries. .DESCRIPTION Dictionaries contain OS specific commands to do abstract OS layer from user's scripts. This function load them according to a predefined hierarchy. .PARAMETER Mainstream Mainstream OS label (merely Windows / Macos / Linux) .PARAMETER Platform Platform os OS .PARAMETER Family Family of OS .PARAMETER Distrib Distribution name of OS .PARAMETER ReleaseId Release number of OS .PARAMETER Path Path from which to load dictionaries. Allow custom script to have its own dictionaries. Defaults to PwSh.Fw.OS module's dictionaries path .PARAMETER Force Force to (re-)load module .EXAMPLE Get-OperatingSystem -Online | Load-Dictionaries .EXAMPLE Load-Dictionaries -Platform Windows -Family Desktop -Distrib "Windows 10" .EXAMPLE Load-Dictionaries -Platform Linux -Family Debian -Distrib Ubuntu .EXAMPLE Load-Dictionaries -Platform MacOS .NOTES General notes #> function Load-Dictionaries { [CmdletBinding()] [OutputType([void])] [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "", Justification="Load-Dictionaries is a more intuitive verb for this function.")] Param ( [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$Mainstream, [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$Platform, [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$Kernel, [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$Family, [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$Distrib, [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$ReleaseId, [string]$Path = "$PSScriptRoot/Dictionaries", # Prefix to prepend to search for dictionaries [string]$Prefix, [Parameter()][switch]$Force ) Begin { Write-PwShFwOSEnterFunction # Write-PwShFwOSDevel "Platform = $Platform" # Write-PwShFwOSDevel "Family = $Family" # Write-PwShFwOSDevel "Distrib = $Distrib" # Write-PwShFwOSDevel "ReleaseId = $ReleaseId" } Process { # Write-PwShFwOSDevel "Platform = $Platform" # Write-PwShFwOSDevel "Kernel = $Kernel" # Write-PwShFwOSDevel "Family = $Family" # Write-PwShFwOSDevel "Distrib = $Distrib" # Write-PwShFwOSDevel "ReleaseId = $ReleaseId" # from @url https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions # [\s\p{P}] Match all white space and punctuation. # $m = (get-culture).TextInfo.ToTitleCase($Mainstream) -replace '[\s\p{P}]' $p = (get-culture).TextInfo.ToTitleCase($platform) -replace '[\s\p{P}]' $k = (get-culture).TextInfo.ToTitleCase($Kernel) -replace '[\s\p{P}]' $f = (get-culture).TextInfo.ToTitleCase($Family) -replace '[\s\p{P}]' $d = (get-culture).TextInfo.ToTitleCase($Distrib) -replace '[\s\p{P}]' $r = (get-culture).TextInfo.ToTitleCase($ReleaseId) -replace '[\s\p{P}]' $loadOrder = @( "OS", "$p", "$p.$k", "$p.$k.$f", "$p.$k.$f.$d", "$p.$k.$f.$d.$r" ) ## Load PwSh Dictionaries # with fullyQualifiedName we are able to load Modules from devel folder foreach ($suffix in $loadOrder) { if ($Prefix) { $dict = "$Prefix.Dict.$suffix" } else { $dict = "Dict.$suffix" } Write-PwShFwOSDebug "Loading $dict" # switch (Get-PwShFwOSEnv) { # 'PROD' { # try { $null = Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } # 'DBG' { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } # 'DEV' { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } # } Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Continue } # ## Load PwSh Dictionaries # # with fullyQualifiedName we are able to load Modules from devel folder # # and I forgot that Dictionaries folder is not included in PsModulePath at install time # if ($Prefix) { $dict = "$Prefix.Dict.OS" } else { $dict = "Dict.OS" } # if ((Get-PwShFwOSEnv) -eq 'PROD') { # try { $null = Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } else { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } # # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force # # if (![string]::IsNullOrEmpty($Mainstream)) { # # if ($Prefix) { $dict = "$Prefix.Dict.$m" } else { $dict = "Dict.$m" } # # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force # # } # if (![string]::IsNullOrEmpty($Platform)) { # if ($Prefix) { $dict = "$Prefix.Dict.$p" } else { $dict = "Dict.$p" } # if ((Get-PwShFwOSEnv) -eq 'PROD') { # try { $null = Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } else { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } # # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force # } # if (![string]::IsNullOrEmpty($Kernel)) { # if ($Prefix) { $dict = "$Prefix.Dict.$p.$k" } else { $dict = "Dict.$p.$k" } # if ((Get-PwShFwOSEnv) -eq 'PROD') { # try { $null = Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } else { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } # # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force # } # if (![string]::IsNullOrEmpty($Family)) { # if ($Prefix) { $dict = "$Prefix.Dict.$p.$k.$f" } else { $dict = "Dict.$p.$k.$f" } # if ((Get-PwShFwOSEnv) -eq 'PROD') { # try { $null = Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } else { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } # # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force # } # if (![string]::IsNullOrEmpty($Distrib)) { # if ($Prefix) { $dict = "$Prefix.Dict.$p.$k.$f.$d" } else { $dict = "Dict.$p.$k.$f.$d" } # if ((Get-PwShFwOSEnv) -eq 'PROD') { # try { $null = Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } else { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } # # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force # } # if (![string]::IsNullOrEmpty($ReleaseId)) { # if ($Prefix) { $dict = "$Prefix.Dict.$p.$k.$f.$d.$r" } else { $dict = "Dict.$p.$k.$f.$d.$r" } # if ((Get-PwShFwOSEnv) -eq 'PROD') { # try { $null = Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } else { # try { Import-Module -FullyQualifiedName "$Path/$dict/$dict.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } # } # # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$dict/$dict.psd1" -Force:$Force # } } End { Write-PwShFwOSLeaveFunction } } <# .SYNOPSIS Load offline operating system's Libraries. .DESCRIPTION Libraries contain specific knowledge to related online or offline OS. This function load them according to a predefined hierarchy. .PARAMETER Platform Platform os OS .PARAMETER Family Family of OS .PARAMETER Distrib Distribution name of OS .PARAMETER ReleaseId Release number of OS .PARAMETER Force Force to (re-)load module .EXAMPLE Load-Libraries -Platform Windows -Family Desktop -Distrib "Windows 10" .EXAMPLE Load-Libraries -Platform Linux -Family Debian -Distrib Ubuntu .EXAMPLE Load-Libraries -Platform MacOS .NOTES General notes #> function Load-Libraries { [CmdletBinding()] [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "", Justification="Load-Libraries is a more intuitive verb for this function.")] Param ( [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$Platform, [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$Kernel, [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$Family, [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$Distrib, [AllowNull()][AllowEmptyString()][Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$ReleaseId, [string]$Path = "$PSScriptRoot/Libraries", # Prefix to prepend to search for libraries [string]$Prefix, [Parameter()][switch]$Force ) Begin { Write-PwShFwOSEnterFunction # Write-PwShFwOSDevel "Platform = $Platform" # Write-PwShFwOSDevel "Family = $Family" # Write-PwShFwOSDevel "Distrib = $Distrib" # Write-PwShFwOSDevel "ReleaseId = $ReleaseId" } Process { # Write-PwShFwOSDevel "Platform = $Platform" # Write-PwShFwOSDevel "Kernel = $Kernel" # Write-PwShFwOSDevel "Family = $Family" # Write-PwShFwOSDevel "Distrib = $Distrib" # Write-PwShFwOSDevel "ReleaseId = $ReleaseId" # from @url https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions # [\s\p{P}] Match all white space and punctuation. $p = (get-culture).TextInfo.ToTitleCase($platform) -replace '[\s\p{P}]' $k = (get-culture).TextInfo.ToTitleCase($Kernel) -replace '[\s\p{P}]' $f = (get-culture).TextInfo.ToTitleCase($Family) -replace '[\s\p{P}]' $d = (get-culture).TextInfo.ToTitleCase($Distrib) -replace '[\s\p{P}]' $r = (get-culture).TextInfo.ToTitleCase($ReleaseId) -replace '[\s\p{P}]' ## Load PwSh Libraries # with fullyQualifiedName we are able to load Modules from devel folder # and I forgot that Libraries folder is not included in PsModulePath at install time if ($Prefix) { $lib = "$Prefix.Lib.OS" } else { $lib = "Lib.OS" } if ((Get-PwShFwOSEnv) -eq 'PROD') { try { $null = Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } else { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -Quiet:(-not $Global:TRACE) if (![string]::IsNullOrEmpty($Platform)) { if ($Prefix) { $lib = "$Prefix.Lib.$p" } else { $lib = "Lib.$p" } if ((Get-PwShFwOSEnv) -eq 'PROD') { try { $null = Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } else { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -Quiet:(-not $Global:TRACE) } if (![string]::IsNullOrEmpty($Kernel)) { if ($Prefix) { $lib = "$Prefix.Lib.$p.$k" } else { $lib = "Lib.$p.$k" } if ((Get-PwShFwOSEnv) -eq 'PROD') { try { $null = Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } else { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -Quiet:(-not $Global:TRACE) } if (![string]::IsNullOrEmpty($Family)) { if ($Prefix) { $lib = "$Prefix.Lib.$p.$k.$f" } else { $lib = "Lib.$p.$k.$f" } if ((Get-PwShFwOSEnv) -eq 'PROD') { try { $null = Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } else { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -Quiet:(-not $Global:TRACE) } if (![string]::IsNullOrEmpty($Distrib)) { if ($Prefix) { $lib = "$Prefix.Lib.$p.$k.$f.$d" } else { $lib = "Lib.$p.$k.$f.$d" } if ((Get-PwShFwOSEnv) -eq 'PROD') { try { $null = Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } else { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -Quiet:(-not $Global:TRACE) } if (![string]::IsNullOrEmpty($ReleaseId)) { if ($Prefix) { $lib = "$Prefix.Lib.$p.$k.$f.$d.$r" } else { $lib = "Lib.$p.$k.$f.$d.$r" } if ((Get-PwShFwOSEnv) -eq 'PROD') { try { $null = Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } elseif ((Get-PwShFwOSEnv) -eq 'DBG') { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } else { try { Import-Module -FullyQualifiedName "$Path/$lib/$lib.psm1" -Force:$Force -ErrorAction Stop } catch { Write-PwShFwOSDevel $_.Exception.Message } } # $null = Load-Module -Policy Optional -FullyQualifiedName "$Path/$lib/$lib.psd1" -Force:$Force -Quiet:(-not $Global:TRACE) } # ## Load User's script Libraries # if (![string]::IsNullOrEmpty($Platform)) { # $lib = "Lib.$p" # $null = Load-Module -Policy Optional -FullyQualifiedName $($Global:DIRNAME + "/Libraries/$lib/$lib.psd1") -Force:$Force -Quiet # } # if (![string]::IsNullOrEmpty($Kernel)) { # $lib = "Lib.$k" # $null = Load-Module -Policy Optional -FullyQualifiedName $($Global:DIRNAME + "/Libraries/$lib/$lib.psd1") -Force:$Force -Quiet # } # if (![string]::IsNullOrEmpty($Distrib)) { # $lib = "Lib.$d" # $null = Load-Module -Policy Optional -FullyQualifiedName $($Global:DIRNAME + "/Libraries/$lib/$lib.psd1") -Force:$Force -Quiet # } } End { Write-PwShFwOSLeaveFunction } } <# .SYNOPSIS Retrive the attribute of an Operating System. .DESCRIPTION Get the attribute of an Operating System using what have been found so far. It uses OS Libraries to discover correct values .PARAMETER OS a partial OS object .PARAMETER Attribute Name of attribute of OS object to fetch .EXAMPLE Get-OSAttribute -OS $os -Attribute "version" .NOTES General notes #> function Get-OSAttribute { [CmdletBinding()][OutputType([Object])]Param ( [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][Object]$OS, [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][String]$Attribute ) Begin { Write-PwShFwOSEnterFunction } Process { # search function from the more specific to the most generic Write-PwShFwOSDevel "Going to fetch '$Attribute' attribute" # $searchOrderAttributes = @('releaseId', 'distrib', 'family', 'kernel', 'platform') $os | Load-Libraries $Attribute = (get-culture).TextInfo.ToTitleCase($Attribute) -replace '[\s\p{P}]' # from @url https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions # [\s\p{P}] Match all white space and punctuation. $p = (get-culture).TextInfo.ToTitleCase($os.platform) -replace '[\s\p{P}]' $k = (get-culture).TextInfo.ToTitleCase($os.kernel) -replace '[\s\p{P}]' $f = (get-culture).TextInfo.ToTitleCase($os.family) -replace '[\s\p{P}]' $d = (get-culture).TextInfo.ToTitleCase($os.distrib) -replace '[\s\p{P}]' $r = (get-culture).TextInfo.ToTitleCase($os.releaseId) -replace '[\s\p{P}]' $searchOrder = @( "$p.$k.$f.$d.$r", "$p.$k.$f.$d", "$p.$k.$f", "$p.$k", "$p" ) foreach ($ns in $searchOrder) { $Value = $null # ns: name space. It is the namespace to search. # if $s is kernel, we want to search in $os.$s (Lib.WinNT), but we need to work on value of $os.$s to be usable # for example, $s is distrib with a value of "Windows10", we need to search in "Lib.Windows10" and not "Lib.Windows 10" # $ns = (get-culture).TextInfo.ToTitleCase($os.$s) -replace '[\s\p{P}]' try { # edebug "try to load Lib.$ns\Get-OS$Attribute function" $null = Get-Command -Name "Lib.$ns\Get-OS$Attribute" -Module "Lib.$ns" -ErrorAction SilentlyContinue if ($?) { Write-PwShFwOSDebug "found Lib.$ns\Get-OS$Attribute" if ($os.Online) { $Value = invoke-expression "Lib.$ns\Get-OS$Attribute -Online" } else { $Value = invoke-expression "Lib.$ns\Get-OS$Attribute -Root $($os.Root)" } if ($Value) { break } } else { if ($Global:TRACE) { Write-PwShFwOSDevel "Lib.$ns\Get-OS$Attribute is not defined." } $Value = $null } } catch { Write-Error $_ if ($Global:TRACE) { eerror "An error occured while playing with 'Lib.$ns\Get-OS$Attribute -Online:$($os.online) -Root $($os.Root)'" } $Value = $null if ((Get-PwShFwOSEnv) -eq "DEV") { Write-Outdent } } } if ($null -eq $Value) { Write-PwShFwOSDebug "Unable to find a function implementing Get-OS$Attribute" } return $Value } End { Write-PwShFwOSLeaveFunction } } function Get-OSPackage { [CmdletBinding(DefaultParameterSetName='byPackageName')]Param ( [Parameter(Mandatory = $true, ValueFromPipeLine = $true)]$InputObject, [Parameter(Mandatory = $true, ValueFromPipeLine = $false, ParameterSetName = 'byPackageName')][string]$Name, [Parameter(Mandatory = $true, ValueFromPipeLine = $false, ParameterSetName = 'byFileName')][string]$Filename ) Begin { Write-PwShFwOSEnterFunction } Process { switch ($PSCmdlet.ParameterSetName) { 'byPackageName' { # $oPackage = $InputObject.packages.'$name' # break } 'byFileName' { $name = Get-OSPackagenameFromFilename -Online:$InputObject.Online -Root $InputObject.Root -Filename $Filename } } if (![string]::IsNullOrEmpty($name)) { return $InputObject.packages."$name" } else { return $null } } End { Write-PwShFwOSLeaveFunction } } <# .SYNOPSIS Convert a string to a [system.version] object .DESCRIPTION Convert a string to a [system.version] object. It takes care of fancy char separator, and keep only 4 first digits If it still can't convert to a [system.version] object, return input as a string unmodified .EXAMPLE "1.2.3-01" | ConvertTo-VersionNumber return '1.2.3.1' version number .EXAMPLE ConvertTo-VersionNumber -string "1.2.3-pre5" return '1.2.3.5' .EXAMPLE ConvertTo-VersionNumber -string "1.2.3-alpha5" ConvertTo-VersionNumber -string "1.2.3-beta5" Both return "1.2.3.5" version number. It is an actual limitation. .NOTES General notes #> function ConvertTo-VersionNumber { [CmdletBinding()] [OutputType([system.version], [string])] Param ( # a version as a string [Alias("string")][AllowEmptyString()] [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][string]$VersionString ) Begin { Write-PwShFwOSEnterFunction } Process { Write-PwShFwOSDevel "Before VersionString = $VersionString" if ([string]::IsNullOrEmpty($VersionString)) { return [system.version]"0.0.0.0" } $data = $VersionString + ".0.0.0.0" # remove leading "[0-9]:" commonly found in debian packages $data = $data -replace "^\d*:", "" # convert all separators to dot $data = $data -replace "[-_/+~]", "." # # replace 'build' keyword by a dot # $data = $data -replace "build", "." # remove everything after '~' char # $data = $data -replace "~.*", "" # # remove any char that is not a digit nor a dot (remove "alpha", "beta", "ubuntu" strings) # $data = $data -replace "[^\d.]", "" # remove any word by a dot (replace "build", "alpha", "beta", "ubuntu" strings) $data = $data -replace "[^\d.]+", "." # remove dot-dot like in "10.1..3.4" $data = $data -replace "\.\.*", "." # keep only first 4 digits $data = $data.Split(".")[0..3] -Join "." Write-PwShFwOSDevel "After VersionString = $data" try { return [system.version]$data } catch { return [string]$VersionString } } End { Write-PwShFwOSLeaveFunction } } |