Skip to content

Commit 051ec75

Browse files
authored
Merge pull request #11 from Akaizoku/develop
1.2.6
2 parents 08496a4 + f72cefa commit 051ec75

17 files changed

+769
-271
lines changed

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ All notable changes to the [PSTK](https://github.com/Akaizoku/PSTK) project will
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.2.6](https://github.com/Akaizoku/PSTK/releases/1.2.6) - 2024-10-07
9+
10+
Quality of life
11+
12+
### Added
13+
14+
The following functions have been added:
15+
16+
- Ping-Host: Check connectivity to a specified host
17+
- Show-EnvironmentVariables: Fetch environment variables
18+
- Get-PowerShellError: Returns the latest error
19+
- New-ProcessObject: Create standardised process object to monitor custom processes
20+
- Update-ProcessObject: Update standardised process object to monitor custom processes
21+
22+
### Changed
23+
24+
The following functions have been updated:
25+
26+
- Get-Properties: Now provides an option to pull metadata such as section and description
27+
- Read-Properties: Now provides an option to pull metadata such as section and description
28+
- Write-Log: Added notice message option
29+
30+
### Fixed
31+
32+
- Compare-Version: Fixed version comparison issues by using built-in version compare function when applciable
33+
834
## [1.2.5](https://github.com/Akaizoku/PSTK/releases/1.2.5) - 2021-11-21
935

1036
Maintenance automation and incremental update

PSTK.psd1

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: Florian Carrier
55
#
6-
# Generated on: 21/11/2021
6+
# Generated on: 24/09/2024
77
#
88

99
@{
@@ -12,7 +12,7 @@
1212
RootModule = 'PSTK.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.2.5'
15+
ModuleVersion = '1.2.6'
1616

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

2626
# Company or vendor of this module
27-
CompanyName = ''
27+
CompanyName = 'Unknown'
2828

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

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

35-
# Minimum version of the Windows PowerShell engine required by this module
35+
# Minimum version of the PowerShell engine required by this module
3636
PowerShellVersion = '3.0'
3737

38-
# Name of the Windows PowerShell host required by this module
38+
# Name of the PowerShell host required by this module
3939
# PowerShellHostName = ''
4040

41-
# Minimum version of the Windows PowerShell host required by this module
41+
# Minimum version of the PowerShell host required by this module
4242
# PowerShellHostVersion = ''
4343

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

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

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

9293
# 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.
@@ -125,7 +126,7 @@ PrivateData = @{
125126
# IconUri = ''
126127

127128
# ReleaseNotes of this module
128-
ReleaseNotes = 'https://github.com/Akaizoku/PSAYX/blob/main/CHANGELOG.md'
129+
ReleaseNotes = 'https://github.com/Akaizoku/PSTK/blob/main/CHANGELOG.md'
129130

130131
# Prerelease string of this module
131132
# Prerelease = ''

Private/Read-Properties.ps1

Lines changed: 149 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,157 @@
1-
# ------------------------------------------------------------------------------
2-
# Properties parsing function
3-
# ------------------------------------------------------------------------------
41
function Read-Properties {
5-
<#
6-
.SYNOPSIS
7-
Parse properties file
2+
<#
3+
.SYNOPSIS
4+
Parse properties file
85
9-
.DESCRIPTION
10-
Parse properties file to generate configuration variables
6+
.DESCRIPTION
7+
Parse properties file to generate configuration variables
118
12-
.PARAMETER Path
13-
The patch parameter corresponds to the path to the property file to read.
9+
.PARAMETER Path
10+
[String] The patch parameter corresponds to the path to the property file to read.
1411
15-
.PARAMETER Section
16-
[Switch] The Section parameter indicates if properties should be grouped depending on
17-
existing sections in the file.
12+
.PARAMETER Section
13+
[Switch] The Section parameter indicates if properties should be grouped depending on existing sections in the file.
1814
19-
.OUTPUTS
20-
[System.Collections.Specialized.OrderedDictionary] Read-Properties returns an
21-
ordered hash table containing the content of the property file.
15+
.PARAMETER Metadata
16+
[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.
2217
23-
.EXAMPLE
24-
Read-Properties -Path ".\conf\default.ini" -Section
18+
.OUTPUTS
19+
[System.Collections.Specialized.OrderedDictionary] Read-Properties returns an ordered hash table containing the content of the property file.
2520
26-
In this example, Read-Properties will parse the default.ini file contained
27-
in the .\conf directory and generate an ordered hashtable containing the
28-
key-values pairs.
29-
#>
30-
[CmdletBinding ()]
31-
Param (
32-
[Parameter (
33-
Position = 1,
34-
Mandatory = $true,
35-
HelpMessage = "Path to the property file"
36-
)]
37-
[ValidateNotNullOrEmpty ()]
38-
[String]
39-
$Path,
40-
[Parameter (
41-
Position = 3,
42-
Mandatory = $false,
43-
HelpMessage = "Define if section headers should be used to group properties or be ignored"
44-
)]
45-
[Switch]
46-
$Section
47-
)
48-
Begin {
49-
# Get global preference variables
50-
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
51-
# Instantiate variables
52-
$Properties = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
53-
$Sections = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
54-
$Header = $null
55-
$errors = 0
56-
}
57-
Process {
58-
# Check that the file exists
59-
if (Test-Path -Path $Path) {
60-
$ListOfProperties = Get-Content -Path $Path
61-
$LineNumber = 0
62-
# Read the property file line by line
63-
foreach ($Property in $ListOfProperties) {
64-
$LineNumber += 1
65-
# If properties have to be grouped by section
66-
if ($Section) {
67-
# If end of file and section is open
68-
if ($LineNumber -eq $ListOfProperties.Count -And $Header) {
69-
if ($Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property -ne "") {
70-
$Property = Read-Property -Property $Property
71-
if ($Property.Count -gt 0) {
72-
$Sections.Add($Property.Key, $Property.Value)
73-
} else {
74-
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
75-
}
76-
}
77-
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
78-
$Properties.Add($Header, $Clone)
79-
} elseif ($Property[0] -eq "[") {
80-
# If previous section exists add it to the property list
81-
if ($Header) {
82-
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
83-
$Properties.Add($Header, $Clone)
84-
}
85-
# Create new property group
86-
$Header = $Property.Substring(1, $Property.Length - 2)
87-
$Sections.Clear()
88-
} elseif ($Header -And $Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property -ne "") {
89-
$Property = Read-Property -Property $Property
90-
if ($Property.Count -gt 0) {
91-
$Sections.Add($Property.Key, $Property.Value)
92-
} else {
93-
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
94-
}
95-
}
96-
} else {
97-
# Ignore comments, sections, and blank lines
98-
if ($Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property[0] -ne "[" -And $Property -ne "") {
99-
$Property = Read-Property -Property $Property
100-
if ($Property.Count -gt 0) {
101-
try {
102-
$Properties.Add($Property.Key, $Property.Value)
103-
} catch {
104-
Write-Log -Type "WARN" -Object "Two distinct definitions of the property $($Property.Key) have been found in the configuration file"
105-
$Errors += 1
106-
}
107-
} else {
108-
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
109-
}
110-
}
111-
}
112-
}
113-
} else {
114-
# Alert that configuration file does not exist at specified location
115-
Write-Log -Type "ERROR" -Message "Path not found $Path" -ExitCode 1
116-
}
117-
if ($Errors -gt 0) {
118-
Write-Log -Type "ERROR" -Object "Unable to proceed. Resolve the issues in $Path" -ExitCode 1
119-
}
120-
return $Properties
121-
}
21+
.EXAMPLE
22+
Read-Properties -Path ".\conf\default.ini" -Section
23+
24+
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.
25+
26+
.NOTES
27+
File name: Read-Properties.ps1
28+
Author: Florian Carrier
29+
Creation date: 2018-11-27
30+
Last modified: 2024-09-13
31+
#>
32+
[CmdletBinding ()]
33+
Param (
34+
[Parameter (
35+
Position = 1,
36+
Mandatory = $true,
37+
HelpMessage = "Path to the property file"
38+
)]
39+
[ValidateNotNullOrEmpty ()]
40+
[String]
41+
$Path,
42+
[Parameter (
43+
Position = 3,
44+
Mandatory = $false,
45+
HelpMessage = "Define if section headers should be used to group properties or be ignored"
46+
)]
47+
[Switch]
48+
$Section,
49+
[Parameter (
50+
HelpMessage = "Switch to retrieve metadata about properties"
51+
)]
52+
[Switch]
53+
$Metadata
54+
)
55+
Begin {
56+
# Get global preference variables
57+
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
58+
}
59+
Process {
60+
# Check that the file exists
61+
if (Test-Path -Path $Path) {
62+
# Load property file content
63+
$Content = Get-Content -Path $Path
64+
# Instantiate variables
65+
$Properties = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
66+
$Sections = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
67+
$Errors = 0
68+
$LineNumber = 0
69+
$Header = $null
70+
$PreviousLine = $null
71+
# Read content line by line
72+
foreach ($Line in $Content) {
73+
$LineNumber += 1
74+
# If properties have to be grouped by section
75+
if ($Section) {
76+
# If end of file and section is open
77+
if ($LineNumber -eq $Content.Count -And $Header) {
78+
if ($Line[0] -ne "#" -And $Line[0] -ne ";" -And $Line -ne "") {
79+
$Property = Read-Property -Property $Line
80+
if ($Property.Count -gt 0) {
81+
$Sections.Add($Property.Key, $Property.Value)
82+
} else {
83+
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
84+
}
85+
}
86+
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
87+
$Properties.Add($Header, $Clone)
88+
} elseif ($Line[0] -eq "[") {
89+
# If previous section exists add it to the property list
90+
if ($Header) {
91+
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
92+
$Properties.Add($Header, $Clone)
93+
}
94+
# Create new property group
95+
$Header = $Line.Substring(1, $Line.Length - 2)
96+
$Sections.Clear()
97+
} elseif ($Header -And $Line[0] -ne "#" -And $Line[0] -ne ";" -And $Line -ne "") {
98+
$Property = Read-Property -Property $Line
99+
if ($Property.Count -gt 0) {
100+
$Sections.Add($Property.Key, $Property.Value)
101+
} else {
102+
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
103+
}
104+
}
105+
} else {
106+
# Parse rows
107+
if ($null -eq $Line -or $Line -eq "") {
108+
# Ignore empty lines
109+
} elseif ($Line[0] -eq "[") {
110+
# Parse sections
111+
$Header = $Line.Substring(1, $Line.Length - 2).Trim()
112+
} elseif ($Line[0] -eq "#" -Or $Line[0] -eq ";" ) {
113+
# Parse comments
114+
$PreviousLine = $Line.Substring(1, $Line.Length - 1).Trim()
115+
} else {
116+
# Parse properties
117+
$Property = Read-Property -Property $Line
118+
if ($Property.Count -gt 0) {
119+
if ($Metadata -eq $true) {
120+
# Create custom object including metadata
121+
$Value = [Ordered]@{
122+
"Value" = $Property.Value
123+
"Description" = $PreviousLine
124+
"Section" = $Header
125+
}
126+
} else {
127+
# Return raw value
128+
$Value = $Property.Value
129+
}
130+
try {
131+
# Assign property
132+
$Properties.Add($Property.Key, $Value)
133+
} catch {
134+
Write-Log -Type "WARN" -Object "Two distinct definitions of the property $($Property.Key) have been found in the configuration file"
135+
$Errors += 1
136+
}
137+
# Reset metadata
138+
$PreviousLine = $null
139+
} else {
140+
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
141+
}
142+
}
143+
}
144+
}
145+
} else {
146+
# Alert that configuration file does not exist at specified location
147+
Write-Log -Type "ERROR" -Message "Path not found $Path" -ExitCode 1
148+
}
149+
if ($Errors -gt 0) {
150+
Write-Log -Type "ERROR" -Object "Unable to proceed. Resolve the issues in $Path" -ExitCode 1
151+
}
152+
}
153+
End {
154+
# Return list of properties
155+
return $Properties
156+
}
122157
}

0 commit comments

Comments
 (0)