Skip to content

Commit

Permalink
Merge pull request #11 from Akaizoku/develop
Browse files Browse the repository at this point in the history
1.2.6
  • Loading branch information
Akaizoku authored Oct 7, 2024
2 parents 08496a4 + f72cefa commit 051ec75
Show file tree
Hide file tree
Showing 17 changed files with 769 additions and 271 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ All notable changes to the [PSTK](https://github.com/Akaizoku/PSTK) project will
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.6](https://github.com/Akaizoku/PSTK/releases/1.2.6) - 2024-10-07

Quality of life

### Added

The following functions have been added:

- Ping-Host: Check connectivity to a specified host
- Show-EnvironmentVariables: Fetch environment variables
- Get-PowerShellError: Returns the latest error
- New-ProcessObject: Create standardised process object to monitor custom processes
- Update-ProcessObject: Update standardised process object to monitor custom processes

### Changed

The following functions have been updated:

- Get-Properties: Now provides an option to pull metadata such as section and description
- Read-Properties: Now provides an option to pull metadata such as section and description
- Write-Log: Added notice message option

### Fixed

- Compare-Version: Fixed version comparison issues by using built-in version compare function when applciable

## [1.2.5](https://github.com/Akaizoku/PSTK/releases/1.2.5) - 2021-11-21

Maintenance automation and incremental update
Expand Down
43 changes: 22 additions & 21 deletions PSTK.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Florian Carrier
#
# Generated on: 21/11/2021
# Generated on: 24/09/2024
#

@{
Expand All @@ -12,7 +12,7 @@
RootModule = 'PSTK.psm1'

# Version number of this module.
ModuleVersion = '1.2.5'
ModuleVersion = '1.2.6'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand All @@ -24,28 +24,28 @@ GUID = '065d3e48-45c0-4b6b-9052-b92fe22b4e51'
Author = 'Florian Carrier'

# Company or vendor of this module
CompanyName = ''
CompanyName = 'Unknown'

# Copyright statement for this module
Copyright = '(c) 2019-2021 Florian Carrier. All rights reserved.'
Copyright = '(c) 2019-2024 Florian Carrier. All rights reserved.'

# Description of the functionality provided by this module
Description = 'Collection of useful functions and procedures for PowerShell scripting'

# Minimum version of the Windows PowerShell engine required by this module
# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '3.0'

# Name of the Windows PowerShell host required by this module
# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# CLRVersion = ''
# ClrVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
Expand Down Expand Up @@ -75,18 +75,19 @@ FunctionsToExport = 'Compare-Hashtable', 'Compare-Properties', 'Compare-Version'
'ConvertTo-RegularExpression', 'Copy-Object', 'Copy-OrderedHashtable',
'Expand-CompressedFile', 'Find-Key', 'Format-String',
'Get-CallerPreference', 'Get-EnvironmentVariable', 'Get-HTTPStatus',
'Get-KeyValue', 'Get-Object', 'Get-Path', 'Get-Properties', 'Get-URI',
'Import-CSVProperties', 'Import-Function', 'Import-Properties',
'Invoke-OracleCmd', 'New-DynamicParameter', 'New-RandomPassword',
'New-SelfContainedPackage', 'Out-Hashtable',
'Protect-WindowsCmdValue', 'Remove-EnvironmentVariable',
'Remove-Object', 'Rename-NumberedFile', 'Resolve-Array',
'Resolve-Boolean', 'Resolve-Tags', 'Resolve-URI', 'Select-XMLNode',
'Set-EnvironmentVariable', 'Set-RelativePath', 'Set-Tags',
'Start-Script', 'Stop-AllTranscripts', 'Stop-Script',
'Sync-EnvironmentVariable', 'Test-EnvironmentVariable',
'Test-HTTPStatus', 'Test-Object', 'Test-OracleConnection',
'Test-Service', 'Test-SQLConnection', 'Update-File', 'Wait-WebResource',
'Get-KeyValue', 'Get-Object', 'Get-Path', 'Get-PowerShellError',
'Get-Properties', 'Get-URI', 'Import-CSVProperties', 'Import-Function',
'Import-Properties', 'Invoke-OracleCmd', 'New-DynamicParameter',
'New-ProcessObject', 'New-RandomPassword', 'New-SelfContainedPackage',
'Out-Hashtable', 'Ping-Host', 'Protect-WindowsCmdValue',
'Remove-EnvironmentVariable', 'Remove-Object', 'Rename-NumberedFile',
'Resolve-Array', 'Resolve-Boolean', 'Resolve-Tags', 'Resolve-URI',
'Select-XMLNode', 'Set-EnvironmentVariable', 'Set-RelativePath',
'Set-Tags', 'Show-EnvironmentVariables', 'Start-Script',
'Stop-AllTranscripts', 'Stop-Script', 'Sync-EnvironmentVariable',
'Test-EnvironmentVariable', 'Test-HTTPStatus', 'Test-Object',
'Test-OracleConnection', 'Test-Service', 'Test-SQLConnection',
'Update-File', 'Update-ProcessObject', 'Wait-WebResource',
'Write-Checksum', 'Write-ErrorLog', 'Write-InsertOrUpdate', 'Write-Log'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down Expand Up @@ -125,7 +126,7 @@ PrivateData = @{
# IconUri = ''

# ReleaseNotes of this module
ReleaseNotes = 'https://github.com/Akaizoku/PSAYX/blob/main/CHANGELOG.md'
ReleaseNotes = 'https://github.com/Akaizoku/PSTK/blob/main/CHANGELOG.md'

# Prerelease string of this module
# Prerelease = ''
Expand Down
263 changes: 149 additions & 114 deletions Private/Read-Properties.ps1
Original file line number Diff line number Diff line change
@@ -1,122 +1,157 @@
# ------------------------------------------------------------------------------
# Properties parsing function
# ------------------------------------------------------------------------------
function Read-Properties {
<#
.SYNOPSIS
Parse properties file
<#
.SYNOPSIS
Parse properties file
.DESCRIPTION
Parse properties file to generate configuration variables
.DESCRIPTION
Parse properties file to generate configuration variables
.PARAMETER Path
The patch parameter corresponds to the path to the property file to read.
.PARAMETER Path
[String] The patch parameter corresponds to the path to the property file to read.
.PARAMETER Section
[Switch] The Section parameter indicates if properties should be grouped depending on
existing sections in the file.
.PARAMETER Section
[Switch] The Section parameter indicates if properties should be grouped depending on existing sections in the file.
.OUTPUTS
[System.Collections.Specialized.OrderedDictionary] Read-Properties returns an
ordered hash table containing the content of the property file.
.PARAMETER Metadata
[Switch] The metadata parameter indicates that the value (data) as well as the description and section (metadata) should be returned. This does not apply is the section switch is enabled.
.EXAMPLE
Read-Properties -Path ".\conf\default.ini" -Section
.OUTPUTS
[System.Collections.Specialized.OrderedDictionary] Read-Properties returns an ordered hash table containing the content of the property file.
In this example, Read-Properties will parse the default.ini file contained
in the .\conf directory and generate an ordered hashtable containing the
key-values pairs.
#>
[CmdletBinding ()]
Param (
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "Path to the property file"
)]
[ValidateNotNullOrEmpty ()]
[String]
$Path,
[Parameter (
Position = 3,
Mandatory = $false,
HelpMessage = "Define if section headers should be used to group properties or be ignored"
)]
[Switch]
$Section
)
Begin {
# Get global preference variables
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
# Instantiate variables
$Properties = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
$Sections = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
$Header = $null
$errors = 0
}
Process {
# Check that the file exists
if (Test-Path -Path $Path) {
$ListOfProperties = Get-Content -Path $Path
$LineNumber = 0
# Read the property file line by line
foreach ($Property in $ListOfProperties) {
$LineNumber += 1
# If properties have to be grouped by section
if ($Section) {
# If end of file and section is open
if ($LineNumber -eq $ListOfProperties.Count -And $Header) {
if ($Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property -ne "") {
$Property = Read-Property -Property $Property
if ($Property.Count -gt 0) {
$Sections.Add($Property.Key, $Property.Value)
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
}
}
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
$Properties.Add($Header, $Clone)
} elseif ($Property[0] -eq "[") {
# If previous section exists add it to the property list
if ($Header) {
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
$Properties.Add($Header, $Clone)
}
# Create new property group
$Header = $Property.Substring(1, $Property.Length - 2)
$Sections.Clear()
} elseif ($Header -And $Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property -ne "") {
$Property = Read-Property -Property $Property
if ($Property.Count -gt 0) {
$Sections.Add($Property.Key, $Property.Value)
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
}
}
} else {
# Ignore comments, sections, and blank lines
if ($Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property[0] -ne "[" -And $Property -ne "") {
$Property = Read-Property -Property $Property
if ($Property.Count -gt 0) {
try {
$Properties.Add($Property.Key, $Property.Value)
} catch {
Write-Log -Type "WARN" -Object "Two distinct definitions of the property $($Property.Key) have been found in the configuration file"
$Errors += 1
}
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
}
}
}
}
} else {
# Alert that configuration file does not exist at specified location
Write-Log -Type "ERROR" -Message "Path not found $Path" -ExitCode 1
}
if ($Errors -gt 0) {
Write-Log -Type "ERROR" -Object "Unable to proceed. Resolve the issues in $Path" -ExitCode 1
}
return $Properties
}
.EXAMPLE
Read-Properties -Path ".\conf\default.ini" -Section
In this example, Read-Properties will parse the default.ini file contained in the .\conf directory and generate an ordered hashtable containing the key-values pairs.
.NOTES
File name: Read-Properties.ps1
Author: Florian Carrier
Creation date: 2018-11-27
Last modified: 2024-09-13
#>
[CmdletBinding ()]
Param (
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "Path to the property file"
)]
[ValidateNotNullOrEmpty ()]
[String]
$Path,
[Parameter (
Position = 3,
Mandatory = $false,
HelpMessage = "Define if section headers should be used to group properties or be ignored"
)]
[Switch]
$Section,
[Parameter (
HelpMessage = "Switch to retrieve metadata about properties"
)]
[Switch]
$Metadata
)
Begin {
# Get global preference variables
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
}
Process {
# Check that the file exists
if (Test-Path -Path $Path) {
# Load property file content
$Content = Get-Content -Path $Path
# Instantiate variables
$Properties = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
$Sections = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
$Errors = 0
$LineNumber = 0
$Header = $null
$PreviousLine = $null
# Read content line by line
foreach ($Line in $Content) {
$LineNumber += 1
# If properties have to be grouped by section
if ($Section) {
# If end of file and section is open
if ($LineNumber -eq $Content.Count -And $Header) {
if ($Line[0] -ne "#" -And $Line[0] -ne ";" -And $Line -ne "") {
$Property = Read-Property -Property $Line
if ($Property.Count -gt 0) {
$Sections.Add($Property.Key, $Property.Value)
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
}
}
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
$Properties.Add($Header, $Clone)
} elseif ($Line[0] -eq "[") {
# If previous section exists add it to the property list
if ($Header) {
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
$Properties.Add($Header, $Clone)
}
# Create new property group
$Header = $Line.Substring(1, $Line.Length - 2)
$Sections.Clear()
} elseif ($Header -And $Line[0] -ne "#" -And $Line[0] -ne ";" -And $Line -ne "") {
$Property = Read-Property -Property $Line
if ($Property.Count -gt 0) {
$Sections.Add($Property.Key, $Property.Value)
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
}
}
} else {
# Parse rows
if ($null -eq $Line -or $Line -eq "") {
# Ignore empty lines
} elseif ($Line[0] -eq "[") {
# Parse sections
$Header = $Line.Substring(1, $Line.Length - 2).Trim()
} elseif ($Line[0] -eq "#" -Or $Line[0] -eq ";" ) {
# Parse comments
$PreviousLine = $Line.Substring(1, $Line.Length - 1).Trim()
} else {
# Parse properties
$Property = Read-Property -Property $Line
if ($Property.Count -gt 0) {
if ($Metadata -eq $true) {
# Create custom object including metadata
$Value = [Ordered]@{
"Value" = $Property.Value
"Description" = $PreviousLine
"Section" = $Header
}
} else {
# Return raw value
$Value = $Property.Value
}
try {
# Assign property
$Properties.Add($Property.Key, $Value)
} catch {
Write-Log -Type "WARN" -Object "Two distinct definitions of the property $($Property.Key) have been found in the configuration file"
$Errors += 1
}
# Reset metadata
$PreviousLine = $null
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
}
}
}
}
} else {
# Alert that configuration file does not exist at specified location
Write-Log -Type "ERROR" -Message "Path not found $Path" -ExitCode 1
}
if ($Errors -gt 0) {
Write-Log -Type "ERROR" -Object "Unable to proceed. Resolve the issues in $Path" -ExitCode 1
}
}
End {
# Return list of properties
return $Properties
}
}
Loading

0 comments on commit 051ec75

Please sign in to comment.