├── src ├── Convert │ ├── _RootModule.ps1 │ ├── Public │ │ ├── ConvertTo-TitleCase.ps1 │ │ ├── ConvertFrom-Base64ToByteArray.ps1 │ │ ├── ConvertFrom-ByteArrayToMemoryStream.ps1 │ │ ├── ConvertFrom-HashTable.ps1 │ │ ├── Get-UnixTime.ps1 │ │ ├── ConvertTo-EscapedUrl.ps1 │ │ ├── ConvertFrom-EscapedUrl.ps1 │ │ ├── ConvertTo-UnixTime.ps1 │ │ ├── ConvertFrom-Base64ToMemoryStream.ps1 │ │ ├── ConvertTo-Hash.ps1 │ │ ├── ConvertFrom-UnixTime.ps1 │ │ ├── ConvertTo-Celsius.ps1 │ │ ├── ConvertTo-Fahrenheit.ps1 │ │ ├── ConvertFrom-MemoryStreamToSecureString.ps1 │ │ ├── ConvertFrom-StringToCompressedByteArray.ps1 │ │ ├── ConvertFrom-CompressedByteArrayToString.ps1 │ │ ├── ConvertFrom-Base64ToString.ps1 │ │ ├── ConvertFrom-Base64.ps1 │ │ ├── ConvertFrom-MemoryStreamToBase64.ps1 │ │ ├── ConvertFrom-ByteArrayToBase64.ps1 │ │ ├── ConvertFrom-MemoryStreamToByteArray.ps1 │ │ ├── ConvertFrom-StringToByteArray.ps1 │ │ ├── ConvertFrom-MemoryStreamToString.ps1 │ │ ├── ConvertTo-MemoryStream.ps1 │ │ ├── ConvertFrom-StringToMemoryStream.ps1 │ │ └── ConvertFrom-StringToBase64.ps1 │ ├── Convert.psm1 │ └── Private │ │ ├── ConvertPtrToString.ps1 │ │ └── GetRustError.ps1 └── Tests │ ├── Unit │ ├── ConvertFrom-ByteArrayToMemoryStream.Tests.ps1 │ ├── ConvertFrom-Base64ToByteArray.Tests.ps1 │ ├── ConvertTo-TitleCase.Tests.ps1 │ ├── ConvertFrom-MemoryStreamToSecureString.Tests.ps1 │ ├── ConvertFrom-HashTable.Tests.ps1 │ ├── Get-UnixTime.Tests.ps1 │ ├── ConvertFrom-Base64ToMemoryStream.Tests.ps1 │ ├── ConvertFrom-EscapedUrl.Tests.ps1 │ ├── ConvertTo-MemoryStream.Tests.ps1 │ ├── ConvertFrom-StringToMemoryStream.Tests.ps1 │ ├── ConvertTo-String.Tests.ps1 │ ├── ConvertFrom-MemoryStreamToString.Tests.ps1 │ ├── ConvertFrom-MemoryStreamToBase64.Tests.ps1 │ └── Global.Tests.ps1 │ └── Build │ └── Build.Tests.ps1 ├── docs ├── requirements.txt └── functions │ ├── ConvertTo-TitleCase.md │ ├── ConvertTo-EscapedUrl.md │ ├── ConvertFrom-EscapedUrl.md │ ├── ConvertFrom-Base64ToByteArray.md │ ├── ConvertFrom-ByteArrayToMemoryStream.md │ ├── ConvertFrom-HashTable.md │ ├── Get-UnixTime.md │ ├── ConvertFrom-Base64ToMemoryStream.md │ ├── ConvertFrom-ByteArrayToBase64.md │ ├── ConvertTo-Hash.md │ ├── ConvertFrom-Clixml.md │ ├── ConvertTo-UnixTime.md │ ├── ConvertFrom-CompressedByteArrayToString.md │ ├── ConvertFrom-StringToCompressedByteArray.md │ ├── ConvertTo-Celsius.md │ ├── ConvertFrom-UnixTime.md │ ├── ConvertFrom-MemoryStreamToSecureString.md │ ├── ConvertTo-Clixml.md │ ├── ConvertTo-Fahrenheit.md │ ├── ConvertFrom-Base64ToString.md │ ├── ConvertFrom-StringToByteArray.md │ ├── ConvertFrom-MemoryStreamToBase64.md │ ├── ConvertFrom-Base64.md │ ├── ConvertFrom-StringToBase64.md │ ├── ConvertFrom-MemoryStreamToByteArray.md │ ├── ConvertFrom-MemoryStreamToString.md │ └── ConvertTo-MemoryStream.md ├── .gitignore ├── lib ├── Cargo.toml └── src │ ├── lib.rs │ └── error.rs ├── install_nuget.ps1 ├── .build ├── Invoke-ScriptAnalysis.ps1 ├── Invoke-CodeFormatter.ps1 ├── Invoke-PesterTests.ps1 ├── Invoke-DocumentationGeneration.ps1 └── Invoke-PublishModule.ps1 ├── LICENSE ├── .github └── workflows │ ├── publish.yml │ └── docs.yml ├── install_modules.ps1 └── mkdocs.yml /src/Convert/_RootModule.ps1: -------------------------------------------------------------------------------- 1 | $script:EPOCH_TIME = (Get-Date -Date '1970-01-01T00:00:00Z').ToUniversalTime() 2 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | # https://github.com/readthedocs-examples/example-mkdocs-basic/blob/main/docs/requirements.txt 2 | # requirements.txt 3 | jinja2==3.1.6 4 | mkdocs>=1.4.2 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Archive 2 | Artifacts 3 | DeploymentArtifacts 4 | .DS_Store 5 | .vscode 6 | coverage.xml 7 | /test*.xml 8 | 9 | # Rust 10 | /lib/target 11 | /src/Convert/Private/bin 12 | 13 | # MkDocs 14 | site/ 15 | .cache/ -------------------------------------------------------------------------------- /lib/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "convert_core" 3 | version = "2.0.0" 4 | edition = "2024" 5 | 6 | [lib] 7 | name = "convert_core" 8 | crate-type = ["cdylib"] 9 | 10 | [dependencies] 11 | base64 = "0.22" 12 | sha2 = "0.10" 13 | sha1 = "0.10" 14 | md-5 = "0.10" 15 | hmac = "0.12" 16 | flate2 = "1.0" 17 | percent-encoding = "2.3" 18 | chrono = "0.4" 19 | 20 | [dev-dependencies] 21 | criterion = "0.5" 22 | proptest = "1.4" 23 | -------------------------------------------------------------------------------- /lib/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Convert Core Library 2 | //! 3 | //! High-performance conversion functions for the PowerShell Convert module. 4 | //! This library provides C ABI exports for Base64 encoding/decoding, cryptographic 5 | //! hashing, compression, URL encoding, and time/temperature conversions. 6 | 7 | // Module declarations 8 | mod base64; 9 | mod compression; 10 | mod encoding; 11 | mod error; 12 | mod hash; 13 | mod memory; 14 | mod temperature; 15 | mod time; 16 | mod url; 17 | 18 | // Re-export public functions from modules 19 | pub use base64::*; 20 | pub use compression::*; 21 | pub use encoding::*; 22 | pub use error::*; 23 | pub use hash::*; 24 | pub use memory::*; 25 | pub use temperature::*; 26 | pub use time::*; 27 | pub use url::*; 28 | -------------------------------------------------------------------------------- /install_nuget.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | This script is used to ensure the NuGet provider is installed. 4 | #> 5 | $global:VerbosePreference = 'SilentlyContinue' 6 | $ErrorActionPreference = 'Stop' 7 | $ProgressPreference = 'SilentlyContinue' 8 | $VerbosePreference = 'SilentlyContinue' 9 | 10 | # Fix for PowerShell Gallery and TLS1.2 11 | # https://devblogs.microsoft.com/powershell/powershell-gallery-tls-support/ 12 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 13 | $installPackageProvider = @{ 14 | Name = 'NuGet' 15 | MinimumVersion = '2.8.5.201' 16 | Scope = 'CurrentUser' 17 | Confirm = $false 18 | Force = $true 19 | ErrorAction = 'SilentlyContinue' 20 | } 21 | $null = Install-PackageProvider @installPackageProvider 22 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertTo-TitleCase.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Convert a string to title case. 4 | 5 | .DESCRIPTION 6 | Convert a string to title case. 7 | 8 | .PARAMETER String 9 | The string to convert. 10 | 11 | .EXAMPLE 12 | PS> ConvertTo-TitleCase -String 'my string' 13 | 14 | Returns the string `My String`. 15 | 16 | .OUTPUTS 17 | [string] 18 | 19 | .LINK 20 | https://austoonz.github.io/Convert/functions/ConvertTo-TitleCase/ 21 | #> 22 | function ConvertTo-TitleCase { 23 | [CmdletBinding()] 24 | param ( 25 | [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] 26 | [ValidateNotNullOrEmpty()] 27 | [string[]]$String 28 | ) 29 | 30 | process { 31 | foreach ($s in $String) { 32 | ([System.Globalization.CultureInfo]::CurrentCulture).TextInfo.ToTitleCase($s) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-Base64ToByteArray.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a Base 64 Encoded String to a Byte Array 4 | 5 | .DESCRIPTION 6 | Converts a Base 64 Encoded String to a Byte Array 7 | 8 | .PARAMETER String 9 | The Base 64 Encoded String to be converted 10 | 11 | .EXAMPLE 12 | PS C:\> ConvertFrom-Base64ToByteArray -String 'dGVzdA==' 13 | 116 14 | 101 15 | 115 16 | 116 17 | 18 | Converts the base64 string to its byte array representation. 19 | 20 | .LINK 21 | https://msdn.microsoft.com/en-us/library/system.convert.frombase64string%28v=vs.110%29.aspx 22 | #> 23 | function ConvertFrom-Base64ToByteArray { 24 | [CmdletBinding()] 25 | [Alias('ConvertFrom-Base64StringToByteArray')] 26 | param 27 | ( 28 | [Parameter(Mandatory = $true)] 29 | [ValidateNotNullOrEmpty()] 30 | [Alias('Base64String')] 31 | [String]$String 32 | ) 33 | [System.Convert]::FromBase64String($String) 34 | } 35 | -------------------------------------------------------------------------------- /.build/Invoke-ScriptAnalysis.ps1: -------------------------------------------------------------------------------- 1 | param( 2 | [Parameter(Mandatory)] 3 | [string]$SourcePath, 4 | 5 | [Parameter(Mandatory)] 6 | [string]$TestsPath 7 | ) 8 | 9 | Import-Module PSScriptAnalyzer -ErrorAction Stop 10 | 11 | $allIssues = @() 12 | 13 | Write-Host ' Analyzing module files...' -ForegroundColor Gray 14 | $moduleParams = @{ 15 | Path = $SourcePath 16 | ExcludeRule = @('PSAvoidGlobalVars') 17 | Severity = @('Error', 'Warning') 18 | Recurse = $true 19 | } 20 | 21 | $moduleResults = Invoke-ScriptAnalyzer @moduleParams 22 | if ($moduleResults) { 23 | $allIssues += $moduleResults 24 | } 25 | 26 | # Skip .Tests.ps1 files - they have different coding standards and don't need analysis 27 | 28 | if ($allIssues.Count -gt 0) { 29 | Write-Host '' 30 | $allIssues | Format-Table -AutoSize 31 | Write-Host "Found $($allIssues.Count) PSScriptAnalyzer issue(s)." -ForegroundColor Red 32 | exit 1 33 | } 34 | 35 | Write-Host 'PowerShell code analysis passed.' -ForegroundColor Green 36 | exit 0 37 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-ByteArrayToMemoryStream.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a Byte Array to a MemoryStream 4 | 5 | .DESCRIPTION 6 | Converts a Byte Array to a MemoryStream 7 | 8 | .PARAMETER ByteArray 9 | The Byte Array to be converted 10 | 11 | .LINK 12 | https://msdn.microsoft.com/en-us/library/system.io.memorystream(v=vs.110).aspx 13 | 14 | .NOTES 15 | Additional information: 16 | https://msdn.microsoft.com/en-us/library/63z365ty(v=vs.110).aspx 17 | 18 | .EXAMPLE 19 | ConvertFrom-ByteArrayToMemoryStream -ByteArray ([Byte[]] (,0xFF * 100)) 20 | 21 | This command uses the ConvertFrom-ByteArrayToMemoryStream cmdlet to convert a Byte Array into a Memory Stream. 22 | #> 23 | function ConvertFrom-ByteArrayToMemoryStream { 24 | param 25 | ( 26 | [Parameter(Mandatory = $true)] 27 | [ValidateNotNullOrEmpty()] 28 | [Alias('Bytes')] 29 | [System.Byte[]]$ByteArray 30 | ) 31 | [System.IO.MemoryStream]::new($ByteArray, 0, $ByteArray.Length) 32 | } 33 | -------------------------------------------------------------------------------- /src/Convert/Convert.psm1: -------------------------------------------------------------------------------- 1 | # This file is used for development and testing only 2 | # The build process (Invoke-Build) compiles all .ps1 files into a single .psm1 3 | 4 | $ErrorActionPreference = 'Stop' 5 | 6 | # Load RustInterop.ps1 which handles all the Rust library loading 7 | # This ensures we only maintain the Add-Type definition in one place 8 | $rustInteropPath = [System.IO.Path]::Combine($PSScriptRoot, 'Private', 'RustInterop.ps1') 9 | . $rustInteropPath 10 | 11 | # Dot-source all other .ps1 files (Public and Private functions) 12 | # Note: Skip RustInterop.ps1 since we already loaded it above 13 | try { 14 | $allFiles = [System.IO.Directory]::GetFiles($PSScriptRoot, '*.ps1', [System.IO.SearchOption]::AllDirectories) 15 | foreach ($file in $allFiles) { 16 | $fileName = [System.IO.Path]::GetFileName($file) 17 | if ($fileName -ne 'Convert.psm1' -and $fileName -ne 'RustInterop.ps1') { 18 | . $file 19 | } 20 | } 21 | } catch { 22 | Write-Warning -Message ('{0}: {1}' -f $Function, $_.Exception.Message) 23 | throw 24 | } 25 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-HashTable.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts HashTable objects to PSCustomObject objects. 4 | 5 | .DESCRIPTION 6 | Converts HashTable objects to PSCustomObject objects. 7 | 8 | .PARAMETER HashTable 9 | A list of HashTable objects to convert 10 | 11 | .EXAMPLE 12 | PS> ConvertFrom-HashTable -HashTable @{'foo'='bar'} 13 | 14 | Returns a PSCustomObject with the property 'foo' with value 'bar'. 15 | 16 | .EXAMPLE 17 | PS> @{'foo'='bar'} | ConvertFrom-HashTable 18 | 19 | Returns a PSCustomObject with the property 'foo' with value 'bar'. 20 | 21 | .OUTPUTS 22 | [PSCustomObject[]] 23 | 24 | .LINK 25 | https://austoonz.github.io/Convert/functions/ConvertFrom-HashTable/ 26 | #> 27 | function ConvertFrom-HashTable { 28 | [CmdletBinding()] 29 | param ( 30 | [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] 31 | [HashTable[]]$HashTable 32 | ) 33 | 34 | process { 35 | foreach ($h in $HashTable) { 36 | [PSCustomObject]$h 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Convert/Public/Get-UnixTime.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Gets the current date time represented in Unix time. 4 | 5 | .DESCRIPTION 6 | Gets the current date time represented in Unix time, which is the time in seconds that have elapsed since 00:00:00 UTC on 7 | 1 January, 1970. 8 | 9 | A switch is provided to return the time value represented in milliseconds. 10 | 11 | .OUTPUTS 12 | [long] 13 | 14 | .LINK 15 | https://austoonz.github.io/Convert/functions/Get-UnixTime/ 16 | 17 | .EXAMPLE 18 | Get-UnixTime 19 | 20 | 1674712340 21 | 22 | .EXAMPLE 23 | Get-UnixTime -AsMilliseconds 24 | 25 | 1674712353731 26 | #> 27 | function Get-UnixTime { 28 | [CmdletBinding()] 29 | param ( 30 | # If specified, returns the time in milliseconds that have elapsed since 00:00:00 UTC on 1 January, 1970. 31 | [switch]$AsMilliseconds 32 | ) 33 | 34 | if ($AsMilliseconds) { 35 | ConvertTo-UnixTime -DateTime ([datetime]::UtcNow) -AsMilliseconds 36 | } else { 37 | ConvertTo-UnixTime -DateTime ([datetime]::UtcNow) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertFrom-ByteArrayToMemoryStream.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe $function { 4 | It 'Returns a MemoryStream' { 5 | $byteArray = [Byte[]] (, 0xFF * 100) 6 | 7 | $assertion = ConvertFrom-ByteArrayToMemoryStream -ByteArray $byteArray 8 | $assertion.GetType().Name | Should -BeExactly 'MemoryStream' 9 | } 10 | It 'Does not throw an exception when input is an empty System.Byte' { 11 | { ConvertFrom-ByteArrayToMemoryStream -ByteArray (New-Object -TypeName System.Byte) } | Should -Not -Throw 12 | } 13 | 14 | It 'Does not throw an exception when input is empty' { 15 | { ConvertFrom-ByteArrayToMemoryStream -ByteArray '' } | Should -Not -Throw 16 | } 17 | 18 | It 'Throws an exception when input is of wrong type' { 19 | { ConvertFrom-ByteArrayToMemoryStream -ByteArray (New-Object -TypeName PSObject) } | Should -Throw 20 | } 21 | 22 | It 'Throws an exception when input is null' { 23 | { ConvertFrom-ByteArrayToMemoryStream -ByteArray $null } | Should -Throw 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Andrew Pearce 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertFrom-Base64ToByteArray.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe $function { 4 | It 'Returns bytes' { 5 | $text = 'This is a secret and should be hidden' 6 | $bytes = [System.Text.Encoding]::Unicode.GetBytes($text) 7 | $base64String = [Convert]::ToBase64String($bytes) 8 | 9 | $assertion = ConvertFrom-Base64ToByteArray -String $base64String 10 | $assertion | Should -BeOfType 'byte' 11 | } 12 | 13 | It 'Throws an exception when input is a string of incorrect length' { 14 | { ConvertFrom-Base64ToByteArray -String 'String' } | Should -Throw 15 | } 16 | 17 | It 'Throws an exception when input is of wrong type' { 18 | { ConvertFrom-Base64ToByteArray -String (New-Object -TypeName PSObject) } | Should -Throw 19 | } 20 | 21 | It 'Throws an exception when input is null' { 22 | { ConvertFrom-Base64ToByteArray -String $null } | Should -Throw 23 | } 24 | 25 | It 'Throws an exception when input is empty' { 26 | { ConvertFrom-Base64ToByteArray -String '' } | Should -Throw 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertTo-TitleCase.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe $function { 4 | It 'Converts to TitleCase correctly' { 5 | $string = 'this is a string' 6 | $expected = 'This Is A String' 7 | 8 | $assertion = ConvertTo-TitleCase -String $string 9 | $assertion | Should -BeExactly $expected 10 | } 11 | 12 | It 'Supports the PowerShell pipeline' { 13 | $strings = @('this is a string', 'another_string') 14 | $expected = @('This Is A String', 'Another_String') 15 | 16 | $assertion = $strings | ConvertTo-TitleCase 17 | $assertion | Should -BeExactly $expected 18 | } 19 | 20 | It 'Supports the PowerShell pipeline by value name' { 21 | $strings = @([PSCustomObject]@{ 22 | String = 'this is a string' 23 | }, [PSCustomObject]@{ 24 | String = 'another_string' 25 | }) 26 | $expected = @('This Is A String', 'Another_String') 27 | 28 | $assertion = $strings | ConvertTo-TitleCase 29 | $assertion | Should -BeExactly $expected 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertFrom-MemoryStreamToSecureString.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe $function { 4 | It 'Returns a SecureString' { 5 | $string = 'Hello world!' 6 | $bytes = [System.Text.Encoding]::UTF8.GetBytes($string) 7 | $memoryStream = [System.IO.MemoryStream]::new($bytes, 0, $bytes.Length) 8 | 9 | $assertion = ConvertFrom-MemoryStreamToSecureString -MemoryStream $memoryStream 10 | $assertion | Should -BeOfType 'SecureString' 11 | 12 | $credential = [PSCredential]::new('DummyValue', $assertion) 13 | $credential.GetNetworkCredential().Password | Should -BeExactly $string 14 | } 15 | 16 | It 'Throws an exception when input is of wrong type' { 17 | { ConvertFrom-MemoryStreamToSecureString -MemoryStream 'String' } | Should -Throw 18 | } 19 | 20 | It 'Throws an exception when input is null' { 21 | { ConvertFrom-MemoryStreamToSecureString -MemoryStream $null } | Should -Throw 22 | } 23 | 24 | It 'Does not throw an exception when input is an empty System.IO.MemoryStream' { 25 | { ConvertFrom-MemoryStreamToSecureString -MemoryStream (New-Object System.IO.MemoryStream) } | Should -Not -Throw 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertFrom-HashTable.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe $function { 4 | BeforeAll { 5 | $foo = 'foo' 6 | $bar = 'bar' 7 | $ht = @{$foo = $bar} 8 | $null = $foo, $bar, $ht 9 | 10 | function ValidateAssertion { 11 | param ($Assertion) 12 | $assertion | Should -BeOfType 'PSCustomObject' 13 | ($assertion | Get-Member -MemberType NoteProperty | Where-Object {$_.Name}).Name | Should -BeExactly $foo 14 | $assertion.$foo | Should -BeExactly $bar 15 | 16 | } 17 | } 18 | 19 | It 'Converts a HashTable to a PSCustomObject' { 20 | $assertion = ConvertFrom-HashTable -HashTable $ht 21 | ValidateAssertion $assertion 22 | } 23 | 24 | It 'Supports the PowerShell pipeline' { 25 | $assertion = $ht | ConvertFrom-HashTable 26 | ValidateAssertion $assertion 27 | 28 | $assertion = $ht,$ht | ConvertFrom-HashTable 29 | ValidateAssertion $assertion[0] 30 | ValidateAssertion $assertion[1] 31 | } 32 | 33 | It 'Supports the PowerShell pipeline by value name' { 34 | $pipelineObject = [PSCustomObject]@{ 35 | HashTable = $ht 36 | } 37 | 38 | $assertion = $pipelineObject | ConvertFrom-HashTable 39 | ValidateAssertion $assertion 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish to PowerShell Gallery 2 | 3 | on: 4 | workflow_run: 5 | workflows: ["CI"] 6 | types: [completed] 7 | branches: [main] 8 | workflow_dispatch: 9 | 10 | permissions: 11 | contents: read 12 | actions: read 13 | 14 | concurrency: 15 | group: publish-powershell-gallery 16 | cancel-in-progress: true 17 | 18 | env: 19 | MODULE_NAME: Convert 20 | 21 | jobs: 22 | publish: 23 | name: Publish module 24 | runs-on: ubuntu-latest 25 | if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }} 26 | timeout-minutes: 10 27 | 28 | steps: 29 | - name: Checkout 30 | uses: actions/checkout@v4 31 | with: 32 | sparse-checkout: .build/Invoke-PublishModule.ps1 33 | sparse-checkout-cone-mode: false 34 | 35 | - name: Download module artifact 36 | uses: actions/download-artifact@v4 37 | with: 38 | name: ${{ env.MODULE_NAME }}-Module-Universal 39 | path: Artifacts/ 40 | run-id: ${{ github.event.workflow_run.id }} 41 | github-token: ${{ secrets.GITHUB_TOKEN }} 42 | 43 | - name: Publish to PowerShell Gallery 44 | shell: pwsh 45 | env: 46 | PSGALLERY_API_KEY: ${{ secrets.PSGALLERY_API_KEY }} 47 | run: ./.build/Invoke-PublishModule.ps1 -ModuleName ${{ env.MODULE_NAME }} -ArtifactPath Artifacts 48 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertTo-EscapedUrl.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a URL to an escaped Url. 4 | 5 | .DESCRIPTION 6 | Converts a URL to an escaped Url. 7 | 8 | .PARAMETER Url 9 | The URL to escape. 10 | 11 | .EXAMPLE 12 | PS> ConvertTo-EscapedUrl -Url 'http://test.com?value=my value' 13 | 14 | Returns the string `http%3A%2F%2Ftest.com%3Fvalue%3Dmy%20value`. 15 | 16 | .OUTPUTS 17 | [string] 18 | 19 | .LINK 20 | https://austoonz.github.io/Convert/functions/ConvertTo-EscapedUrl/ 21 | #> 22 | function ConvertTo-EscapedUrl { 23 | [CmdletBinding()] 24 | param ( 25 | [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] 26 | [ValidateNotNullOrEmpty()] 27 | [string[]]$Url 28 | ) 29 | 30 | process { 31 | foreach ($u in $Url) { 32 | $ptr = [IntPtr]::Zero 33 | try { 34 | $ptr = [ConvertCoreInterop]::url_encode($u) 35 | if ($ptr -eq [IntPtr]::Zero) { 36 | $errorMessage = GetRustError 37 | Write-Error -Message $errorMessage 38 | continue 39 | } 40 | ConvertPtrToString -Ptr $ptr 41 | } finally { 42 | if ($ptr -ne [IntPtr]::Zero) { 43 | [ConvertCoreInterop]::free_string($ptr) 44 | } 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-EscapedUrl.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts an escaped URL back to a standard Url. 4 | 5 | .DESCRIPTION 6 | Converts an escaped URL back to a standard Url. 7 | 8 | .PARAMETER Url 9 | The escaped URL to convert. 10 | 11 | .EXAMPLE 12 | PS> ConvertFrom-EscapedUrl -Url 'http%3A%2F%2Ftest.com%3Fvalue%3Dmy%20value' 13 | 14 | Returns the string `http://test.com?value=my value`. 15 | 16 | .OUTPUTS 17 | [string] 18 | 19 | .LINK 20 | https://austoonz.github.io/Convert/functions/ConvertFrom-EscapedUrl/ 21 | #> 22 | function ConvertFrom-EscapedUrl { 23 | [CmdletBinding()] 24 | param ( 25 | [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] 26 | [ValidateNotNullOrEmpty()] 27 | [string[]]$Url 28 | ) 29 | 30 | process { 31 | foreach ($u in $Url) { 32 | $ptr = [IntPtr]::Zero 33 | try { 34 | $ptr = [ConvertCoreInterop]::url_decode($u) 35 | if ($ptr -eq [IntPtr]::Zero) { 36 | $errorMessage = GetRustError 37 | Write-Error -Message $errorMessage 38 | continue 39 | } 40 | ConvertPtrToString -Ptr $ptr 41 | } finally { 42 | if ($ptr -ne [IntPtr]::Zero) { 43 | [ConvertCoreInterop]::free_string($ptr) 44 | } 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /install_modules.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | This script is used to install the required PowerShell Modules for the build process. 4 | It has a dependency on the PowerShell Gallery. 5 | #> 6 | $global:VerbosePreference = 'SilentlyContinue' 7 | $ErrorActionPreference = 'Stop' 8 | $ProgressPreference = 'SilentlyContinue' 9 | $VerbosePreference = 'SilentlyContinue' 10 | 11 | # Fix for PowerShell Gallery and TLS1.2 12 | # https://devblogs.microsoft.com/powershell/powershell-gallery-tls-support/ 13 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 14 | 15 | # List of PowerShell Modules required for the build 16 | $modulesToInstall = @( 17 | @{ 18 | ModuleName = 'Pester' 19 | ModuleVersion = '5.7.1' 20 | } 21 | @{ 22 | ModuleName = 'platyPS' 23 | ModuleVersion = '0.14.2' 24 | } 25 | @{ 26 | ModuleName = 'PSScriptAnalyzer' 27 | ModuleVersion = '1.24.0' 28 | } 29 | ) 30 | 31 | $installModule = @{ 32 | Scope = 'CurrentUser' 33 | AllowClobber = $true 34 | Force = $true 35 | SkipPublisherCheck = $true 36 | Verbose = $false 37 | } 38 | 39 | foreach ($module in $modulesToInstall) { 40 | try { 41 | Import-Module -Name $module.ModuleName -RequiredVersion $module.ModuleVersion -Force -ErrorAction Stop 42 | } 43 | catch { 44 | Install-Module -Name $module.ModuleName -RequiredVersion $module.ModuleVersion @installModule 45 | Import-Module -Name $module.ModuleName -RequiredVersion $module.ModuleVersion -Force 46 | } 47 | } -------------------------------------------------------------------------------- /src/Tests/Unit/Get-UnixTime.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe -Name $function -Fixture { 4 | BeforeEach { 5 | $epochTime = Get-Date -Date '01-01-1970' 6 | 7 | # Use the variables so IDE does not complain 8 | $null = $epochTime 9 | } 10 | 11 | Context -Name 'Gets the current time represented as Unix time' -Fixture { 12 | It -Name 'Returns a Unix time in seconds' -Test { 13 | $now = [datetime]::UtcNow 14 | $currentUnixTime = [System.Math]::Round(($now - $epochTime).TotalSeconds) 15 | $assertion = Get-UnixTime 16 | 17 | $assertion | Should -BeOfType [long] 18 | 19 | # As we cannot guarantee these values will be the same, we'll ensure they're within 1 seconds of each other 20 | $assertion - $currentUnixTime | Should -BeLessThan 1 21 | 22 | (($epochTime + [System.TimeSpan]::FromSeconds($assertion)) - $now).TotalSeconds | Should -BeLessThan 1 23 | } 24 | 25 | It -Name 'Returns a Unix time in milliseconds' -Test { 26 | $now = [datetime]::UtcNow 27 | $currentUnixTime = [System.Math]::Round(($now - $epochTime).TotalMilliseconds) 28 | $assertion = Get-UnixTime -AsMilliseconds 29 | 30 | $assertion | Should -BeOfType [long] 31 | 32 | # As we cannot guarantee these values will be the same, we'll ensure they're within 1 seconds of each other 33 | $assertion - $currentUnixTime | Should -BeLessThan 1000 34 | (($epochTime + [System.TimeSpan]::FromMilliseconds($assertion)) - $now).TotalMilliseconds | Should -BeLessThan 1000 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertTo-UnixTime.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a date time to the date time represented in Unix time. 4 | 5 | .DESCRIPTION 6 | Converts a date time to the date time represented in Unix time, which is the time in seconds that have elapsed since 7 | 00:00:00 UTC on 1 January, 1970. 8 | 9 | A switch is provided to return the time value represented in milliseconds. 10 | 11 | .OUTPUTS 12 | [long] 13 | 14 | .LINK 15 | https://austoonz.github.io/Convert/functions/ConvertTo-UnixTime/ 16 | 17 | .EXAMPLE 18 | ConvertTo-UnixTime 19 | 20 | 1674712201 21 | 22 | .EXAMPLE 23 | Get-Date | ConvertTo-UnixTime 24 | 25 | 1674683490 26 | 27 | .EXAMPLE 28 | ConvertTo-UnixTime -DateTime (Get-Date).AddMonths(6) 29 | 30 | 1690321833 31 | 32 | .EXAMPLE 33 | ConvertTo-UnixTime -AsMilliseconds 34 | 35 | 1674712253812 36 | #> 37 | function ConvertTo-UnixTime { 38 | [CmdletBinding()] 39 | param ( 40 | # A DateTime object representing the time to convert. Defaults to `[datetime]::UtcNow`. 41 | [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] 42 | [DateTime]$DateTime = [datetime]::UtcNow, 43 | 44 | # If specified, returns the time in milliseconds that have elapsed since 00:00:00 UTC on 1 January, 1970. 45 | [switch]$AsMilliseconds 46 | ) 47 | 48 | process { 49 | [ConvertCoreInterop]::to_unix_time( 50 | $DateTime.Year, 51 | $DateTime.Month, 52 | $DateTime.Day, 53 | $DateTime.Hour, 54 | $DateTime.Minute, 55 | $DateTime.Second, 56 | $AsMilliseconds.IsPresent 57 | ) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-Base64ToMemoryStream.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a base64 encoded string to a MemoryStream. 4 | 5 | .DESCRIPTION 6 | Converts a base64 encoded string to a MemoryStream. 7 | 8 | .PARAMETER String 9 | A Base64 Encoded String 10 | 11 | .EXAMPLE 12 | ConvertFrom-Base64ToMemoryStream -String 'QSBzdHJpbmc=' 13 | 14 | .EXAMPLE 15 | ConvertFrom-Base64ToMemoryStream -String 'A string','Another string' 16 | 17 | .EXAMPLE 18 | 'QSBzdHJpbmc=' | ConvertFrom-Base64ToMemoryStream 19 | 20 | .EXAMPLE 21 | 'QSBzdHJpbmc=','QW5vdGhlciBzdHJpbmc=' | ConvertFrom-Base64ToMemoryStream 22 | 23 | .OUTPUTS 24 | [String[]] 25 | 26 | .LINK 27 | https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToMemoryStream/ 28 | #> 29 | function ConvertFrom-Base64ToMemoryStream { 30 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToMemoryStream/')] 31 | [OutputType('System.IO.MemoryStream')] 32 | param 33 | ( 34 | [Parameter( 35 | Mandatory = $true, 36 | ValueFromPipeline = $true, 37 | ValueFromPipelineByPropertyName = $true)] 38 | [ValidateNotNullOrEmpty()] 39 | [Alias('Base64String')] 40 | [String[]] 41 | $String 42 | ) 43 | 44 | begin { 45 | $userErrorActionPreference = $ErrorActionPreference 46 | } 47 | 48 | process { 49 | foreach ($s in $String) { 50 | try { 51 | $byteArray = ConvertFrom-Base64ToByteArray -String $s 52 | ConvertFrom-ByteArrayToMemoryStream -ByteArray $byteArray 53 | } catch { 54 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /docs/functions/ConvertTo-TitleCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertTo-TitleCase/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertTo-TitleCase 9 | 10 | ## SYNOPSIS 11 | Convert a string to title case. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertTo-TitleCase [[-String] ] [-ProgressAction ] [] 17 | ``` 18 | 19 | ## DESCRIPTION 20 | Convert a string to title case. 21 | 22 | ## EXAMPLES 23 | 24 | ### EXAMPLE 1 25 | ``` 26 | ConvertTo-TitleCase -String 'my string' 27 | ``` 28 | 29 | Returns the string \`My String\`. 30 | 31 | ## PARAMETERS 32 | 33 | ### -String 34 | The string to convert. 35 | 36 | ```yaml 37 | Type: String[] 38 | Parameter Sets: (All) 39 | Aliases: 40 | 41 | Required: False 42 | Position: 1 43 | Default value: None 44 | Accept pipeline input: True (ByPropertyName, ByValue) 45 | Accept wildcard characters: False 46 | ``` 47 | 48 | ### -ProgressAction 49 | {{ Fill ProgressAction Description }} 50 | 51 | ```yaml 52 | Type: ActionPreference 53 | Parameter Sets: (All) 54 | Aliases: proga 55 | 56 | Required: False 57 | Position: Named 58 | Default value: None 59 | Accept pipeline input: False 60 | Accept wildcard characters: False 61 | ``` 62 | 63 | ### CommonParameters 64 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 65 | 66 | ## INPUTS 67 | 68 | ## OUTPUTS 69 | 70 | ### [string] 71 | ## NOTES 72 | 73 | ## RELATED LINKS 74 | 75 | [https://austoonz.github.io/Convert/functions/ConvertTo-TitleCase/](https://austoonz.github.io/Convert/functions/ConvertTo-TitleCase/) 76 | 77 | -------------------------------------------------------------------------------- /src/Convert/Private/ConvertPtrToString.ps1: -------------------------------------------------------------------------------- 1 | function ConvertPtrToString { 2 | <# 3 | .SYNOPSIS 4 | Converts an IntPtr to a UTF-8 string in a PowerShell 5.1-compatible way. 5 | 6 | .DESCRIPTION 7 | Helper function to convert IntPtr to UTF-8 string that works with both 8 | PowerShell 5.1 (which lacks PtrToStringUTF8) and PowerShell 7+. 9 | 10 | Uses the Rust library's string_to_bytes_copy function to copy the string 11 | to a byte array, then decodes it as UTF-8. This avoids all PowerShell 12 | version compatibility issues. 13 | 14 | .PARAMETER Ptr 15 | The IntPtr pointing to a UTF-8 encoded null-terminated string. 16 | 17 | .OUTPUTS 18 | System.String 19 | Returns the string value from the pointer. 20 | 21 | .EXAMPLE 22 | $ptr = [ConvertCoreInterop]::string_to_base64($input, 'UTF8') 23 | $result = ConvertPtrToString -Ptr $ptr 24 | #> 25 | [CmdletBinding()] 26 | [OutputType([string])] 27 | param( 28 | [Parameter(Mandatory)] 29 | [IntPtr] 30 | $Ptr 31 | ) 32 | 33 | if ($Ptr -eq [IntPtr]::Zero) { 34 | return $null 35 | } 36 | 37 | $length = [UIntPtr]::Zero 38 | $bytesPtr = [ConvertCoreInterop]::string_to_bytes_copy($Ptr, [ref]$length) 39 | 40 | if ($bytesPtr -eq [IntPtr]::Zero) { 41 | return [string]::Empty 42 | } 43 | 44 | try { 45 | $byteCount = [int]$length.ToUInt64() 46 | if ($byteCount -eq 0) { 47 | return [string]::Empty 48 | } 49 | 50 | $bytes = [byte[]]::new($byteCount) 51 | [System.Runtime.InteropServices.Marshal]::Copy($bytesPtr, $bytes, 0, $byteCount) 52 | [System.Text.Encoding]::UTF8.GetString($bytes) 53 | } finally { 54 | [ConvertCoreInterop]::free_bytes($bytesPtr) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Convert/Private/GetRustError.ps1: -------------------------------------------------------------------------------- 1 | function GetRustError { 2 | <# 3 | .SYNOPSIS 4 | Retrieves the last error message from the Rust library. 5 | 6 | .DESCRIPTION 7 | Helper function to retrieve detailed error messages from the Rust convert_core library. 8 | Calls get_last_error() and marshals the returned string pointer to a PowerShell string. 9 | Automatically frees the error string memory after retrieval. 10 | 11 | .PARAMETER DefaultMessage 12 | Optional default message to return if no Rust error is available. 13 | If not provided, returns 'Unknown error'. 14 | 15 | .OUTPUTS 16 | System.String 17 | Returns the error message string from Rust, or the default message if no error is available. 18 | 19 | .EXAMPLE 20 | $ptr = [ConvertCoreInterop]::string_to_base64($input, $encoding) 21 | if ($ptr -eq [IntPtr]::Zero) { 22 | $errorMsg = GetRustError 23 | throw "Base64 encoding failed: $errorMsg" 24 | } 25 | 26 | .EXAMPLE 27 | $ptr = [ConvertCoreInterop]::string_to_base64($input, $encoding) 28 | if ($ptr -eq [IntPtr]::Zero) { 29 | $errorMsg = GetRustError -DefaultMessage "Encoding '$Encoding' is not supported" 30 | throw "Base64 encoding failed: $errorMsg" 31 | } 32 | #> 33 | [CmdletBinding()] 34 | [OutputType([string])] 35 | param( 36 | [Parameter(Mandatory = $false)] 37 | [string] 38 | $DefaultMessage = 'Unknown error' 39 | ) 40 | 41 | $errorPtr = [ConvertCoreInterop]::get_last_error() 42 | if ($errorPtr -ne [IntPtr]::Zero) { 43 | try { 44 | ConvertPtrToString -Ptr $errorPtr 45 | } finally { 46 | [ConvertCoreInterop]::free_string($errorPtr) 47 | } 48 | } else { 49 | $DefaultMessage 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /.build/Invoke-CodeFormatter.ps1: -------------------------------------------------------------------------------- 1 | param( 2 | [Parameter(Mandatory)] 3 | [string]$RepositoryRoot, 4 | 5 | [Parameter(Mandatory)] 6 | [string]$SourcePath 7 | ) 8 | 9 | Import-Module PSScriptAnalyzer -ErrorAction Stop 10 | 11 | $formatterSettings = @{ 12 | Rules = @{ 13 | PSPlaceOpenBrace = @{ 14 | Enable = $true 15 | OnSameLine = $true 16 | NewLineAfter = $true 17 | IgnoreOneLineBlock = $true 18 | } 19 | PSPlaceCloseBrace = @{ 20 | Enable = $true 21 | NoEmptyLineBefore = $true 22 | IgnoreOneLineBlock = $true 23 | NewLineAfter = $false 24 | } 25 | PSUseConsistentWhitespace = @{ 26 | Enable = $true 27 | CheckOpenBrace = $true 28 | CheckOpenParen = $true 29 | CheckOperator = $true 30 | CheckSeparator = $true 31 | } 32 | } 33 | } 34 | 35 | $files = [System.IO.Directory]::GetFiles($SourcePath, '*.ps1', [System.IO.SearchOption]::AllDirectories) 36 | $modifiedFiles = @() 37 | 38 | foreach ($file in $files) { 39 | $contentBefore = [System.IO.File]::ReadAllText($file) 40 | 41 | $result = Invoke-Formatter -ScriptDefinition $contentBefore -Settings $formatterSettings 42 | 43 | if ($result -ne $contentBefore) { 44 | $utf8WithBom = [System.Text.UTF8Encoding]::new($true) 45 | [System.IO.File]::WriteAllText($file, $result, $utf8WithBom) 46 | $relativePath = $file.Replace($RepositoryRoot, '').TrimStart('\', '/') 47 | $modifiedFiles += $relativePath 48 | Write-Host " Modified: $relativePath" -ForegroundColor Gray 49 | } 50 | } 51 | 52 | if ($modifiedFiles.Count -eq 0) { 53 | Write-Host 'All files already formatted correctly.' -ForegroundColor Green 54 | } else { 55 | Write-Host "Formatted $($modifiedFiles.Count) file(s)." -ForegroundColor Green 56 | } 57 | 58 | exit 0 59 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertFrom-Base64ToMemoryStream.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe -Name $function -Fixture { 4 | BeforeEach { 5 | $Base64 = 'VGhpc0lzTXlTdHJpbmc=' 6 | $Expected = 'ThisIsMyString' 7 | 8 | # Use the variables so IDe does not complain 9 | $null = $Base64, $Expected 10 | } 11 | 12 | Context -Name 'Happy Path' -Fixture { 13 | It -Name "Converts a string to a memory stream" -Test { 14 | $assertion = ConvertFrom-Base64ToMemoryStream -String $Base64 15 | $assertion | Should -BeOfType 'System.IO.MemoryStream' 16 | } 17 | 18 | It -Name 'Supports the Pipeline' -Test { 19 | $assertion = $Base64 | ConvertFrom-Base64ToMemoryStream 20 | $assertion | Should -BeOfType 'System.IO.MemoryStream' 21 | } 22 | 23 | It -Name 'Supports EAP SilentlyContinue' -Test { 24 | $assertion = ConvertFrom-Base64ToMemoryStream -String ([int]1) -ErrorAction SilentlyContinue 25 | $assertion | Should -BeNullOrEmpty 26 | } 27 | 28 | It -Name 'Supports EAP Stop' -Test { 29 | { ConvertFrom-Base64ToMemoryStream -String ([int]1) -ErrorAction Stop } | Should -Throw 30 | } 31 | 32 | It -Name 'Supports EAP Continue' -Test { 33 | $assertion = ConvertFrom-Base64ToMemoryStream -String ([int]1) -ErrorAction Continue 2>&1 34 | 35 | $exception = @( 36 | # PowerShell 37 | 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.' 38 | 39 | # Windows PowerShell 40 | 'Invalid length for a Base-64 char array or string.' 41 | ) 42 | $assertion.Exception.InnerException.Message | Should -BeIn $exception 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /docs/functions/ConvertTo-EscapedUrl.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertTo-EscapedUrl/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertTo-EscapedUrl 9 | 10 | ## SYNOPSIS 11 | Converts a URL to an escaped Url. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertTo-EscapedUrl [[-Url] ] [-ProgressAction ] [] 17 | ``` 18 | 19 | ## DESCRIPTION 20 | Converts a URL to an escaped Url. 21 | 22 | ## EXAMPLES 23 | 24 | ### EXAMPLE 1 25 | ``` 26 | ConvertTo-EscapedUrl -Url 'http://test.com?value=my value' 27 | ``` 28 | 29 | Returns the string \`http%3A%2F%2Ftest.com%3Fvalue%3Dmy%20value\`. 30 | 31 | ## PARAMETERS 32 | 33 | ### -Url 34 | The URL to escape. 35 | 36 | ```yaml 37 | Type: String[] 38 | Parameter Sets: (All) 39 | Aliases: 40 | 41 | Required: False 42 | Position: 1 43 | Default value: None 44 | Accept pipeline input: True (ByPropertyName, ByValue) 45 | Accept wildcard characters: False 46 | ``` 47 | 48 | ### -ProgressAction 49 | {{ Fill ProgressAction Description }} 50 | 51 | ```yaml 52 | Type: ActionPreference 53 | Parameter Sets: (All) 54 | Aliases: proga 55 | 56 | Required: False 57 | Position: Named 58 | Default value: None 59 | Accept pipeline input: False 60 | Accept wildcard characters: False 61 | ``` 62 | 63 | ### CommonParameters 64 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 65 | 66 | ## INPUTS 67 | 68 | ## OUTPUTS 69 | 70 | ### [string] 71 | ## NOTES 72 | 73 | ## RELATED LINKS 74 | 75 | [https://austoonz.github.io/Convert/functions/ConvertTo-EscapedUrl/](https://austoonz.github.io/Convert/functions/ConvertTo-EscapedUrl/) 76 | 77 | -------------------------------------------------------------------------------- /lib/src/error.rs: -------------------------------------------------------------------------------- 1 | //! Error reporting mechanism with thread-local storage 2 | 3 | use std::cell::RefCell; 4 | use std::ffi::CString; 5 | use std::os::raw::c_char; 6 | 7 | thread_local! { 8 | static LAST_ERROR: RefCell> = const { RefCell::new(None) }; 9 | } 10 | 11 | /// Set the last error message 12 | pub fn set_error(message: String) { 13 | LAST_ERROR.with(|e| { 14 | *e.borrow_mut() = Some(message); 15 | }); 16 | } 17 | 18 | /// Clear the last error message 19 | pub fn clear_error() { 20 | LAST_ERROR.with(|e| { 21 | *e.borrow_mut() = None; 22 | }); 23 | } 24 | 25 | /// Get the last error message as a C string 26 | /// Returns null if no error 27 | /// Caller must free the returned string with free_string 28 | /// 29 | /// # Safety 30 | /// This function is safe to call from any thread. Returns a newly allocated string 31 | /// that must be freed by the caller using `free_string`. 32 | #[unsafe(no_mangle)] 33 | pub unsafe extern "C" fn get_last_error() -> *mut c_char { 34 | LAST_ERROR.with(|e| match e.borrow().as_ref() { 35 | Some(err) => match CString::new(err.clone()) { 36 | Ok(c_str) => c_str.into_raw(), 37 | Err(_) => std::ptr::null_mut(), 38 | }, 39 | None => std::ptr::null_mut(), 40 | }) 41 | } 42 | 43 | #[cfg(test)] 44 | mod tests { 45 | use super::*; 46 | use std::ffi::CStr; 47 | 48 | #[test] 49 | fn test_error_handling() { 50 | clear_error(); 51 | let ptr = unsafe { get_last_error() }; 52 | assert!(ptr.is_null()); 53 | 54 | set_error("Test error".to_string()); 55 | let ptr = unsafe { get_last_error() }; 56 | assert!(!ptr.is_null()); 57 | 58 | unsafe { 59 | let c_str = CStr::from_ptr(ptr); 60 | assert_eq!(c_str.to_str().unwrap(), "Test error"); 61 | // Free the string 62 | let _ = CString::from_raw(ptr); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-EscapedUrl.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-EscapedUrl/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-EscapedUrl 9 | 10 | ## SYNOPSIS 11 | Converts an escaped URL back to a standard Url. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-EscapedUrl [[-Url] ] [-ProgressAction ] [] 17 | ``` 18 | 19 | ## DESCRIPTION 20 | Converts an escaped URL back to a standard Url. 21 | 22 | ## EXAMPLES 23 | 24 | ### EXAMPLE 1 25 | ``` 26 | ConvertFrom-EscapedUrl -Url 'http%3A%2F%2Ftest.com%3Fvalue%3Dmy%20value' 27 | ``` 28 | 29 | Returns the string \`http://test.com?value=my value\`. 30 | 31 | ## PARAMETERS 32 | 33 | ### -Url 34 | The escaped URL to convert. 35 | 36 | ```yaml 37 | Type: String[] 38 | Parameter Sets: (All) 39 | Aliases: 40 | 41 | Required: False 42 | Position: 1 43 | Default value: None 44 | Accept pipeline input: True (ByPropertyName, ByValue) 45 | Accept wildcard characters: False 46 | ``` 47 | 48 | ### -ProgressAction 49 | {{ Fill ProgressAction Description }} 50 | 51 | ```yaml 52 | Type: ActionPreference 53 | Parameter Sets: (All) 54 | Aliases: proga 55 | 56 | Required: False 57 | Position: Named 58 | Default value: None 59 | Accept pipeline input: False 60 | Accept wildcard characters: False 61 | ``` 62 | 63 | ### CommonParameters 64 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 65 | 66 | ## INPUTS 67 | 68 | ## OUTPUTS 69 | 70 | ### [string] 71 | ## NOTES 72 | 73 | ## RELATED LINKS 74 | 75 | [https://austoonz.github.io/Convert/functions/ConvertFrom-EscapedUrl/](https://austoonz.github.io/Convert/functions/ConvertFrom-EscapedUrl/) 76 | 77 | -------------------------------------------------------------------------------- /src/Tests/Build/Build.Tests.ps1: -------------------------------------------------------------------------------- 1 | Describe -Name 'Module Manifest' -Fixture { 2 | BeforeAll { 3 | $script:ModuleName = 'Convert' 4 | $script:ModuleManifest = [System.IO.Path]::Combine($PSScriptRoot, '..', '..', '..', 'Artifacts', "$ModuleName.psd1") 5 | Import-Module $script:ModuleManifest -Force -ErrorAction 'Stop' 6 | } 7 | 8 | Context -Name 'Exported Functions' -Fixture { 9 | It -Name 'Exports the correct number of functions' -Test { 10 | $assertion = Get-Command -Module $script:ModuleName -CommandType Function 11 | $assertion | Should -HaveCount 30 12 | } 13 | 14 | It -Name '<_>' -TestCases @( 15 | 'ConvertFrom-Base64' 16 | 'ConvertFrom-Base64ToByteArray' 17 | 'ConvertFrom-Base64ToMemoryStream' 18 | 'ConvertFrom-Base64ToString' 19 | 'ConvertFrom-ByteArrayToBase64' 20 | 'ConvertFrom-ByteArrayToMemoryStream' 21 | 'ConvertFrom-CompressedByteArrayToString' 22 | 'ConvertFrom-EscapedUrl' 23 | 'ConvertFrom-HashTable' 24 | 'ConvertFrom-MemoryStream' 25 | 'ConvertFrom-MemoryStreamToBase64' 26 | 'ConvertFrom-MemoryStreamToByteArray' 27 | 'ConvertFrom-MemoryStreamToSecureString' 28 | 'ConvertFrom-MemoryStreamToString' 29 | 'ConvertFrom-StringToBase64' 30 | 'ConvertFrom-StringToByteArray' 31 | 'ConvertFrom-StringToCompressedByteArray' 32 | 'ConvertFrom-StringToMemoryStream' 33 | 'ConvertFrom-UnixTime' 34 | 'ConvertTo-Base64' 35 | 'ConvertTo-EscapedUrl' 36 | 'ConvertTo-Hash' 37 | 'ConvertTo-MemoryStream' 38 | 'ConvertTo-String' 39 | 'ConvertTo-TitleCase' 40 | 'ConvertTo-UnixTime' 41 | 'Get-UnixTime' 42 | ) -Test { 43 | {Get-Command -Name $_ -Module $script:ModuleName -ErrorAction Stop} | Should -Not -Throw 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-Base64ToByteArray.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://msdn.microsoft.com/en-us/library/system.convert.frombase64string%28v=vs.110%29.aspx 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-Base64ToByteArray 9 | 10 | ## SYNOPSIS 11 | Converts a Base 64 Encoded String to a Byte Array 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-Base64ToByteArray [-String] [-ProgressAction ] [] 17 | ``` 18 | 19 | ## DESCRIPTION 20 | Converts a Base 64 Encoded String to a Byte Array 21 | 22 | ## EXAMPLES 23 | 24 | ### EXAMPLE 1 25 | ``` 26 | ConvertFrom-Base64ToByteArray -String 'dGVzdA==' 27 | 116 28 | 101 29 | 115 30 | 116 31 | ``` 32 | 33 | Converts the base64 string to its byte array representation. 34 | 35 | ## PARAMETERS 36 | 37 | ### -String 38 | The Base 64 Encoded String to be converted 39 | 40 | ```yaml 41 | Type: String 42 | Parameter Sets: (All) 43 | Aliases: Base64String 44 | 45 | Required: True 46 | Position: 1 47 | Default value: None 48 | Accept pipeline input: False 49 | Accept wildcard characters: False 50 | ``` 51 | 52 | ### -ProgressAction 53 | {{ Fill ProgressAction Description }} 54 | 55 | ```yaml 56 | Type: ActionPreference 57 | Parameter Sets: (All) 58 | Aliases: proga 59 | 60 | Required: False 61 | Position: Named 62 | Default value: None 63 | Accept pipeline input: False 64 | Accept wildcard characters: False 65 | ``` 66 | 67 | ### CommonParameters 68 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 69 | 70 | ## INPUTS 71 | 72 | ## OUTPUTS 73 | 74 | ## NOTES 75 | 76 | ## RELATED LINKS 77 | 78 | [https://msdn.microsoft.com/en-us/library/system.convert.frombase64string%28v=vs.110%29.aspx](https://msdn.microsoft.com/en-us/library/system.convert.frombase64string%28v=vs.110%29.aspx) 79 | 80 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertTo-Hash.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a string to a hash. 4 | 5 | .DESCRIPTION 6 | Converts a string to a hash. 7 | 8 | .PARAMETER String 9 | A string to convert. 10 | 11 | .PARAMETER Algorithm 12 | The hashing algorithm to use. Defaults to 'SHA256'. 13 | 14 | .EXAMPLE 15 | ConvertTo-Hash -String 'MyString' 16 | 38F92FF0761E08356B7C51C5A1ED88602882C2768F37C2DCC3F0AC6EE3F950F5 17 | 18 | .OUTPUTS 19 | [String[]] 20 | 21 | .LINK 22 | https://austoonz.github.io/Convert/functions/ConvertTo-Hash/ 23 | #> 24 | function ConvertTo-Hash { 25 | [CmdletBinding()] 26 | param ( 27 | [Parameter(ParameterSetName = 'String', ValueFromPipeline, ValueFromPipelineByPropertyName)] 28 | [string[]]$String, 29 | 30 | [ValidateSet('MD5', 'SHA1', 'SHA256', 'SHA384', 'SHA512')] 31 | [string]$Algorithm = 'SHA256', 32 | 33 | [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF8')] 34 | [String] 35 | $Encoding = 'UTF8' 36 | ) 37 | 38 | begin { 39 | $userErrorActionPreference = $ErrorActionPreference 40 | $nullPtr = [IntPtr]::Zero 41 | } 42 | 43 | process { 44 | foreach ($s in $String) { 45 | try { 46 | $ptr = $nullPtr 47 | try { 48 | $ptr = [ConvertCoreInterop]::compute_hash($s, $Algorithm, $Encoding) 49 | 50 | if ($ptr -eq $nullPtr) { 51 | $errorMsg = GetRustError -DefaultMessage "Hash computation failed for algorithm '$Algorithm' with encoding '$Encoding'" 52 | throw $errorMsg 53 | } 54 | 55 | ConvertPtrToString -Ptr $ptr 56 | } finally { 57 | if ($ptr -ne $nullPtr) { 58 | [ConvertCoreInterop]::free_string($ptr) 59 | } 60 | } 61 | } catch { 62 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-ByteArrayToMemoryStream.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://msdn.microsoft.com/en-us/library/system.io.memorystream(v=vs.110).aspx 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-ByteArrayToMemoryStream 9 | 10 | ## SYNOPSIS 11 | Converts a Byte Array to a MemoryStream 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-ByteArrayToMemoryStream [-ByteArray] [-ProgressAction ] 17 | [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts a Byte Array to a MemoryStream 22 | 23 | ## EXAMPLES 24 | 25 | ### EXAMPLE 1 26 | ``` 27 | ConvertFrom-ByteArrayToMemoryStream -ByteArray ([Byte[]] (,0xFF * 100)) 28 | ``` 29 | 30 | This command uses the ConvertFrom-ByteArrayToMemoryStream cmdlet to convert a Byte Array into a Memory Stream. 31 | 32 | ## PARAMETERS 33 | 34 | ### -ByteArray 35 | The Byte Array to be converted 36 | 37 | ```yaml 38 | Type: Byte[] 39 | Parameter Sets: (All) 40 | Aliases: Bytes 41 | 42 | Required: True 43 | Position: 1 44 | Default value: None 45 | Accept pipeline input: False 46 | Accept wildcard characters: False 47 | ``` 48 | 49 | ### -ProgressAction 50 | {{ Fill ProgressAction Description }} 51 | 52 | ```yaml 53 | Type: ActionPreference 54 | Parameter Sets: (All) 55 | Aliases: proga 56 | 57 | Required: False 58 | Position: Named 59 | Default value: None 60 | Accept pipeline input: False 61 | Accept wildcard characters: False 62 | ``` 63 | 64 | ### CommonParameters 65 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 66 | 67 | ## INPUTS 68 | 69 | ## OUTPUTS 70 | 71 | ## NOTES 72 | Additional information: 73 | https://msdn.microsoft.com/en-us/library/63z365ty(v=vs.110).aspx 74 | 75 | ## RELATED LINKS 76 | 77 | [https://msdn.microsoft.com/en-us/library/system.io.memorystream(v=vs.110).aspx](https://msdn.microsoft.com/en-us/library/system.io.memorystream(v=vs.110).aspx) 78 | 79 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-HashTable.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-HashTable/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-HashTable 9 | 10 | ## SYNOPSIS 11 | Converts HashTable objects to PSCustomObject objects. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-HashTable [[-HashTable] ] [-ProgressAction ] [] 17 | ``` 18 | 19 | ## DESCRIPTION 20 | Converts HashTable objects to PSCustomObject objects. 21 | 22 | ## EXAMPLES 23 | 24 | ### EXAMPLE 1 25 | ``` 26 | ConvertFrom-HashTable -HashTable @{'foo'='bar'} 27 | ``` 28 | 29 | Returns a PSCustomObject with the property 'foo' with value 'bar'. 30 | 31 | ### EXAMPLE 2 32 | ``` 33 | @{'foo'='bar'} | ConvertFrom-HashTable 34 | ``` 35 | 36 | Returns a PSCustomObject with the property 'foo' with value 'bar'. 37 | 38 | ## PARAMETERS 39 | 40 | ### -HashTable 41 | A list of HashTable objects to convert 42 | 43 | ```yaml 44 | Type: Hashtable[] 45 | Parameter Sets: (All) 46 | Aliases: 47 | 48 | Required: False 49 | Position: 1 50 | Default value: None 51 | Accept pipeline input: True (ByPropertyName, ByValue) 52 | Accept wildcard characters: False 53 | ``` 54 | 55 | ### -ProgressAction 56 | {{ Fill ProgressAction Description }} 57 | 58 | ```yaml 59 | Type: ActionPreference 60 | Parameter Sets: (All) 61 | Aliases: proga 62 | 63 | Required: False 64 | Position: Named 65 | Default value: None 66 | Accept pipeline input: False 67 | Accept wildcard characters: False 68 | ``` 69 | 70 | ### CommonParameters 71 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 72 | 73 | ## INPUTS 74 | 75 | ## OUTPUTS 76 | 77 | ### [PSCustomObject[]] 78 | ## NOTES 79 | 80 | ## RELATED LINKS 81 | 82 | [https://austoonz.github.io/Convert/functions/ConvertFrom-HashTable/](https://austoonz.github.io/Convert/functions/ConvertFrom-HashTable/) 83 | 84 | -------------------------------------------------------------------------------- /docs/functions/Get-UnixTime.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/Get-UnixTime/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # Get-UnixTime 9 | 10 | ## SYNOPSIS 11 | Gets the current date time represented in Unix time. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | Get-UnixTime [-AsMilliseconds] [-ProgressAction ] [] 17 | ``` 18 | 19 | ## DESCRIPTION 20 | Gets the current date time represented in Unix time, which is the time in seconds that have elapsed since 00:00:00 UTC on 21 | 1 January, 1970. 22 | 23 | A switch is provided to return the time value represented in milliseconds. 24 | 25 | ## EXAMPLES 26 | 27 | ### EXAMPLE 1 28 | ``` 29 | Get-UnixTime 30 | ``` 31 | 32 | 1674712340 33 | 34 | ### EXAMPLE 2 35 | ``` 36 | Get-UnixTime -AsMilliseconds 37 | ``` 38 | 39 | 1674712353731 40 | 41 | ## PARAMETERS 42 | 43 | ### -AsMilliseconds 44 | If specified, returns the time in milliseconds that have elapsed since 00:00:00 UTC on 1 January, 1970. 45 | 46 | ```yaml 47 | Type: SwitchParameter 48 | Parameter Sets: (All) 49 | Aliases: 50 | 51 | Required: False 52 | Position: Named 53 | Default value: False 54 | Accept pipeline input: False 55 | Accept wildcard characters: False 56 | ``` 57 | 58 | ### -ProgressAction 59 | {{ Fill ProgressAction Description }} 60 | 61 | ```yaml 62 | Type: ActionPreference 63 | Parameter Sets: (All) 64 | Aliases: proga 65 | 66 | Required: False 67 | Position: Named 68 | Default value: None 69 | Accept pipeline input: False 70 | Accept wildcard characters: False 71 | ``` 72 | 73 | ### CommonParameters 74 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 75 | 76 | ## INPUTS 77 | 78 | ## OUTPUTS 79 | 80 | ### [long] 81 | ## NOTES 82 | 83 | ## RELATED LINKS 84 | 85 | [https://austoonz.github.io/Convert/functions/Get-UnixTime/](https://austoonz.github.io/Convert/functions/Get-UnixTime/) 86 | 87 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertFrom-EscapedUrl.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe $function { 4 | It 'Converts an escaped URL to a URL' { 5 | $url = 'http%3A%2F%2Ftest.com%3Fvalue%3Dmy%20%23%24%25%40%60%2F%3A%3B%3C%3D%3E%3F%5B%5C%5D%5E%7B%7C%7D~%22%27%2B%2Cvalue' 6 | $expected = 'http://test.com?value=my #$%@`/:;<=>?[\]^{|}~"' + "'" + '+,value' 7 | 8 | $assertion = ConvertFrom-EscapedUrl -Url $url 9 | $assertion | Should -BeExactly $expected 10 | } 11 | 12 | It 'Supports the PowerShell pipeline' { 13 | $url = 'http%3A%2F%2Ftest.com%3Fvalue%3Dmy%20%23%24%25%40%60%2F%3A%3B%3C%3D%3E%3F%5B%5C%5D%5E%7B%7C%7D~%22%27%2B%2Cvalue' 14 | $expected = 'http://test.com?value=my #$%@`/:;<=>?[\]^{|}~"' + "'" + '+,value' 15 | 16 | $assertion = $Url,$Url | ConvertFrom-EscapedUrl 17 | $assertion | Should -BeExactly $expected,$expected 18 | } 19 | 20 | It 'Supports the PowerShell pipeline by value name' { 21 | $url = [PSCustomObject]@{ 22 | Url = 'http%3A%2F%2Ftest.com%3Fvalue%3Dmy%20%23%24%25%40%60%2F%3A%3B%3C%3D%3E%3F%5B%5C%5D%5E%7B%7C%7D~%22%27%2B%2Cvalue' 23 | } 24 | $expected = 'http://test.com?value=my #$%@`/:;<=>?[\]^{|}~"' + "'" + '+,value' 25 | 26 | $assertion = $Url | ConvertFrom-EscapedUrl 27 | $assertion | Should -BeExactly $expected 28 | } 29 | 30 | Context 'Error Handling' { 31 | It 'Handles invalid URL-encoded string' { 32 | $result = ConvertFrom-EscapedUrl -Url '%ZZ' -ErrorAction SilentlyContinue 33 | $result | Should -BeNullOrEmpty 34 | } 35 | 36 | It 'Respects ErrorAction Continue' { 37 | $ErrorActionPreference = 'Continue' 38 | $result = ConvertFrom-EscapedUrl -Url '%GG%HH' -ErrorAction SilentlyContinue 39 | $result | Should -BeNullOrEmpty 40 | } 41 | 42 | It 'Provides error message for malformed input' { 43 | $ErrorActionPreference = 'Continue' 44 | ConvertFrom-EscapedUrl -Url '%' -ErrorAction SilentlyContinue -ErrorVariable err 45 | $err | Should -Not -BeNullOrEmpty 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-Base64ToMemoryStream.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToMemoryStream/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-Base64ToMemoryStream 9 | 10 | ## SYNOPSIS 11 | Converts a base64 encoded string to a MemoryStream. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-Base64ToMemoryStream [-String] [-ProgressAction ] [] 17 | ``` 18 | 19 | ## DESCRIPTION 20 | Converts a base64 encoded string to a MemoryStream. 21 | 22 | ## EXAMPLES 23 | 24 | ### EXAMPLE 1 25 | ``` 26 | ConvertFrom-Base64ToMemoryStream -String 'QSBzdHJpbmc=' 27 | ``` 28 | 29 | ### EXAMPLE 2 30 | ``` 31 | ConvertFrom-Base64ToMemoryStream -String 'A string','Another string' 32 | ``` 33 | 34 | ### EXAMPLE 3 35 | ``` 36 | 'QSBzdHJpbmc=' | ConvertFrom-Base64ToMemoryStream 37 | ``` 38 | 39 | ### EXAMPLE 4 40 | ``` 41 | 'QSBzdHJpbmc=','QW5vdGhlciBzdHJpbmc=' | ConvertFrom-Base64ToMemoryStream 42 | ``` 43 | 44 | ## PARAMETERS 45 | 46 | ### -String 47 | A Base64 Encoded String 48 | 49 | ```yaml 50 | Type: String[] 51 | Parameter Sets: (All) 52 | Aliases: Base64String 53 | 54 | Required: True 55 | Position: 1 56 | Default value: None 57 | Accept pipeline input: True (ByPropertyName, ByValue) 58 | Accept wildcard characters: False 59 | ``` 60 | 61 | ### -ProgressAction 62 | {{ Fill ProgressAction Description }} 63 | 64 | ```yaml 65 | Type: ActionPreference 66 | Parameter Sets: (All) 67 | Aliases: proga 68 | 69 | Required: False 70 | Position: Named 71 | Default value: None 72 | Accept pipeline input: False 73 | Accept wildcard characters: False 74 | ``` 75 | 76 | ### CommonParameters 77 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 78 | 79 | ## INPUTS 80 | 81 | ## OUTPUTS 82 | 83 | ### [String[]] 84 | ## NOTES 85 | 86 | ## RELATED LINKS 87 | 88 | [https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToMemoryStream/](https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToMemoryStream/) 89 | 90 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-UnixTime.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a date time represented in Unix time to a PowerShell DateTime object. 4 | 5 | .DESCRIPTION 6 | Converts a date time represented in Unix time to a PowerShell DateTime object. 7 | 8 | Supports Unix time in seconds by default, or a switch to support Unix time in milliseconds. 9 | 10 | .OUTPUTS 11 | [datetime] 12 | 13 | .LINK 14 | https://austoonz.github.io/Convert/functions/ConvertFrom-UnixTime/ 15 | 16 | .EXAMPLE 17 | ConvertFrom-UnixTime -UnixTime 1674712047 18 | 19 | Thursday, January 26, 2023 5:47:27 AM 20 | 21 | .EXAMPLE 22 | 1674712047 | ConvertFrom-UnixTime 23 | 24 | Thursday, January 26, 2023 5:47:27 AM 25 | 26 | .EXAMPLE 27 | ConvertFrom-UnixTime -UnixTime 1674712048705 -FromMilliseconds 28 | 29 | Thursday, January 26, 2023 5:47:28 AM 30 | 31 | .EXAMPLE 32 | 1674712048705 | ConvertFrom-UnixTime -FromMilliseconds 33 | 34 | Thursday, January 26, 2023 5:47:28 AM 35 | #> 36 | function ConvertFrom-UnixTime { 37 | [CmdletBinding()] 38 | param ( 39 | # The Unix time to convert. Represented in seconds by default, or in milliseconds if the FromMilliseconds 40 | # parameter is specified. 41 | [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] 42 | [long]$UnixTime, 43 | 44 | # If specified, returns the time in milliseconds that have elapsed since 00:00:00 UTC on 1 January, 1970. 45 | [switch]$FromMilliseconds 46 | ) 47 | 48 | process { 49 | $year = 0 50 | $month = 0 51 | $day = 0 52 | $hour = 0 53 | $minute = 0 54 | $second = 0 55 | 56 | $success = [ConvertCoreInterop]::from_unix_time( 57 | $UnixTime, 58 | $FromMilliseconds.IsPresent, 59 | [ref]$year, 60 | [ref]$month, 61 | [ref]$day, 62 | [ref]$hour, 63 | [ref]$minute, 64 | [ref]$second 65 | ) 66 | 67 | if (-not $success) { 68 | $errorMsg = GetRustError 69 | Write-Error -Message "Failed to convert Unix time: $errorMsg" 70 | return 71 | } 72 | 73 | [datetime]::new($year, $month, $day, $hour, $minute, $second, [System.DateTimeKind]::Utc) 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /.build/Invoke-PesterTests.ps1: -------------------------------------------------------------------------------- 1 | param( 2 | [Parameter(Mandatory)] 3 | [string]$ModuleName, 4 | 5 | [Parameter(Mandatory)] 6 | [string]$ManifestPath, 7 | 8 | [Parameter(Mandatory)] 9 | [string]$TestPath, 10 | 11 | [Parameter(Mandatory)] 12 | [string]$TestReportPath, 13 | 14 | [Parameter(Mandatory = $false)] 15 | [switch]$EnableCoverage, 16 | 17 | [Parameter(Mandatory = $false)] 18 | [string]$CoveragePath, 19 | 20 | [Parameter(Mandatory = $false)] 21 | [int]$CoverageThreshold, 22 | 23 | [Parameter(Mandatory = $false)] 24 | [string]$CoverageFormat, 25 | 26 | [Parameter(Mandatory = $false)] 27 | [string]$CoverageFilesPath, 28 | 29 | [Parameter(Mandatory)] 30 | [string]$ModuleSource 31 | ) 32 | 33 | Import-Module Pester 34 | 35 | # Remove any existing module to ensure clean load 36 | Get-Module -Name $ModuleName | Remove-Module -Force -ErrorAction SilentlyContinue 37 | 38 | # Import module from source or artifact 39 | Import-Module $ManifestPath -Force -Global 40 | 41 | # Verify module loaded correctly 42 | $module = Get-Module -Name $ModuleName 43 | if (-not $module) { 44 | Write-Error 'Module failed to load' 45 | exit 1 46 | } 47 | Write-Host "Module root: $($module.ModuleBase)" -ForegroundColor Cyan 48 | Write-Host "Module manifest: $ManifestPath" -ForegroundColor Cyan 49 | 50 | $config = New-PesterConfiguration 51 | $config.Run.Path = $TestPath 52 | $config.Run.PassThru = $true 53 | $config.TestResult.Enabled = $true 54 | $config.TestResult.OutputPath = $TestReportPath 55 | $config.TestResult.OutputFormat = 'JUnitXml' 56 | $config.Output.Verbosity = 'Normal' 57 | $config.Output.CIFormat = 'GithubActions' 58 | $config.CodeCoverage.Enabled = $EnableCoverage.IsPresent 59 | 60 | if ($EnableCoverage.IsPresent) { 61 | $config.CodeCoverage.CoveragePercentTarget = $CoverageThreshold 62 | $config.CodeCoverage.OutputPath = $CoveragePath 63 | $config.CodeCoverage.OutputFormat = $CoverageFormat 64 | 65 | if ($CoverageFilesPath -and [System.IO.File]::Exists($CoverageFilesPath)) { 66 | $coverageFiles = Get-Content -Path $CoverageFilesPath | Where-Object { $_.Trim() -ne '' } 67 | $config.CodeCoverage.Path = $coverageFiles 68 | } 69 | } 70 | 71 | $result = Invoke-Pester -Configuration $config 72 | exit $result.FailedCount 73 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-ByteArrayToBase64.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-ByteArrayToBase64/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-ByteArrayToBase64 9 | 10 | ## SYNOPSIS 11 | Converts a byte array to a base64 encoded string. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-ByteArrayToBase64 [-ByteArray] [-Compress] [-ProgressAction ] 17 | [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts a byte array to a base64 encoded string. 22 | 23 | ## EXAMPLES 24 | 25 | ### EXAMPLE 1 26 | ``` 27 | $bytes = ConvertFrom-StringToCompressedByteArray -String 'A string' 28 | ConvertFrom-ByteArrayToBase64 -ByteArray $bytes 29 | ``` 30 | 31 | H4sIAAAAAAAAC3NUKC4pysxLBwCMN9RgCAAAAA== 32 | 33 | ## PARAMETERS 34 | 35 | ### -ByteArray 36 | A byte array object for conversion. 37 | 38 | ```yaml 39 | Type: Byte[] 40 | Parameter Sets: (All) 41 | Aliases: Bytes 42 | 43 | Required: True 44 | Position: 1 45 | Default value: None 46 | Accept pipeline input: True (ByPropertyName, ByValue) 47 | Accept wildcard characters: False 48 | ``` 49 | 50 | ### -Compress 51 | If supplied, the output will be compressed using Gzip. 52 | 53 | ```yaml 54 | Type: SwitchParameter 55 | Parameter Sets: (All) 56 | Aliases: 57 | 58 | Required: False 59 | Position: Named 60 | Default value: False 61 | Accept pipeline input: False 62 | Accept wildcard characters: False 63 | ``` 64 | 65 | ### -ProgressAction 66 | {{ Fill ProgressAction Description }} 67 | 68 | ```yaml 69 | Type: ActionPreference 70 | Parameter Sets: (All) 71 | Aliases: proga 72 | 73 | Required: False 74 | Position: Named 75 | Default value: None 76 | Accept pipeline input: False 77 | Accept wildcard characters: False 78 | ``` 79 | 80 | ### CommonParameters 81 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 82 | 83 | ## INPUTS 84 | 85 | ## OUTPUTS 86 | 87 | ### [String[]] 88 | ## NOTES 89 | 90 | ## RELATED LINKS 91 | 92 | [https://austoonz.github.io/Convert/functions/ConvertFrom-ByteArrayToBase64/](https://austoonz.github.io/Convert/functions/ConvertFrom-ByteArrayToBase64/) 93 | 94 | -------------------------------------------------------------------------------- /.build/Invoke-DocumentationGeneration.ps1: -------------------------------------------------------------------------------- 1 | [CmdletBinding()] 2 | param( 3 | [Parameter()] 4 | [string]$ModulePath = 'Artifacts', 5 | 6 | [Parameter()] 7 | [string]$OutputPath = 'docs/functions', 8 | 9 | [Parameter()] 10 | [switch]$Force 11 | ) 12 | 13 | $ErrorActionPreference = 'Stop' 14 | 15 | Write-Host 'Generating PowerShell documentation...' -ForegroundColor Cyan 16 | 17 | $ModulePath = [System.IO.Path]::GetFullPath($ModulePath) 18 | if (-not [System.IO.Directory]::Exists($ModulePath)) { 19 | throw "Module path not found: $ModulePath" 20 | } 21 | 22 | $manifestPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($ModulePath, 'Convert.psd1')) 23 | if (-not [System.IO.File]::Exists($manifestPath)) { 24 | throw "Module manifest not found: $manifestPath" 25 | } 26 | 27 | $OutputPath = [System.IO.Path]::GetFullPath($OutputPath) 28 | if (-not [System.IO.Directory]::Exists($OutputPath)) { 29 | $null = [System.IO.Directory]::CreateDirectory($OutputPath) 30 | } 31 | 32 | try { 33 | Import-Module -Name 'platyPS' -ErrorAction Stop 34 | } catch { 35 | throw "PlatyPS module not found. Run install_modules.ps1 first." 36 | } 37 | 38 | Write-Host "Importing module from: $ModulePath" -ForegroundColor Gray 39 | Import-Module -Name $manifestPath -Force -ErrorAction Stop 40 | 41 | $moduleName = 'Convert' 42 | $module = Get-Module -Name $moduleName 43 | 44 | if (-not $module) { 45 | throw "Failed to import module: $moduleName" 46 | } 47 | 48 | Write-Host "Module imported successfully: $($module.Name) v$($module.Version)" -ForegroundColor Green 49 | 50 | $commands = Get-Command -Module $moduleName -CommandType Function | Sort-Object -Property Name 51 | 52 | Write-Host "Found $($commands.Count) functions to document" -ForegroundColor Gray 53 | 54 | foreach ($command in $commands) { 55 | $commandName = $command.Name 56 | $outputFile = [System.IO.Path]::Combine($OutputPath, "$commandName.md") 57 | 58 | Write-Host " Generating: $commandName.md" -ForegroundColor Gray 59 | 60 | if ([System.IO.File]::Exists($outputFile) -and -not $Force) { 61 | Update-MarkdownHelp -Path $outputFile -ErrorAction Stop | Out-Null 62 | } else { 63 | New-MarkdownHelp -Command $commandName -OutputFolder $OutputPath -Force -ErrorAction Stop | Out-Null 64 | } 65 | } 66 | 67 | Write-Host 'Documentation generation complete!' -ForegroundColor Green 68 | Write-Host "Generated $($commands.Count) function documentation files in: $OutputPath" -ForegroundColor Cyan 69 | -------------------------------------------------------------------------------- /docs/functions/ConvertTo-Hash.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertTo-Hash/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertTo-Hash 9 | 10 | ## SYNOPSIS 11 | Converts a string to a hash. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertTo-Hash [-String ] [-Algorithm ] [-Encoding ] 17 | [-ProgressAction ] [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts a string to a hash. 22 | 23 | ## EXAMPLES 24 | 25 | ### EXAMPLE 1 26 | ``` 27 | ConvertTo-Hash -String 'MyString' 28 | 38F92FF0761E08356B7C51C5A1ED88602882C2768F37C2DCC3F0AC6EE3F950F5 29 | ``` 30 | 31 | ## PARAMETERS 32 | 33 | ### -String 34 | A string to convert. 35 | 36 | ```yaml 37 | Type: String[] 38 | Parameter Sets: (All) 39 | Aliases: 40 | 41 | Required: False 42 | Position: Named 43 | Default value: None 44 | Accept pipeline input: True (ByPropertyName, ByValue) 45 | Accept wildcard characters: False 46 | ``` 47 | 48 | ### -Algorithm 49 | The hashing algorithm to use. 50 | Defaults to 'SHA256'. 51 | 52 | ```yaml 53 | Type: String 54 | Parameter Sets: (All) 55 | Aliases: 56 | 57 | Required: False 58 | Position: Named 59 | Default value: SHA256 60 | Accept pipeline input: False 61 | Accept wildcard characters: False 62 | ``` 63 | 64 | ### -Encoding 65 | {{ Fill Encoding Description }} 66 | 67 | ```yaml 68 | Type: String 69 | Parameter Sets: (All) 70 | Aliases: 71 | 72 | Required: False 73 | Position: Named 74 | Default value: UTF8 75 | Accept pipeline input: False 76 | Accept wildcard characters: False 77 | ``` 78 | 79 | ### -ProgressAction 80 | {{ Fill ProgressAction Description }} 81 | 82 | ```yaml 83 | Type: ActionPreference 84 | Parameter Sets: (All) 85 | Aliases: proga 86 | 87 | Required: False 88 | Position: Named 89 | Default value: None 90 | Accept pipeline input: False 91 | Accept wildcard characters: False 92 | ``` 93 | 94 | ### CommonParameters 95 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 96 | 97 | ## INPUTS 98 | 99 | ## OUTPUTS 100 | 101 | ### [String[]] 102 | ## NOTES 103 | 104 | ## RELATED LINKS 105 | 106 | [https://austoonz.github.io/Convert/functions/ConvertTo-Hash/](https://austoonz.github.io/Convert/functions/ConvertTo-Hash/) 107 | 108 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertTo-MemoryStream.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe -Name $function -Fixture { 4 | BeforeEach { 5 | $String = 'ThisIsMyString' 6 | 7 | # Use the variables so IDe does not complain 8 | $null = $String 9 | } 10 | 11 | Context 'String input' -Fixture { 12 | It -Name 'Converts a string to a MemoryStream Object' -Test { 13 | $assertion = ConvertTo-MemoryStream -String $String 14 | $assertion.GetType().Name | Should -BeExactly 'MemoryStream' 15 | } 16 | 17 | It -Name 'Is a valid MemoryStream Object with the correct data' -Test { 18 | $memoryStream = ConvertTo-MemoryStream -String $String 19 | $reader = [System.IO.StreamReader]::new($memoryStream) 20 | $memoryStream.Position = 0 21 | 22 | $assertion = $reader.ReadToEnd() 23 | $assertion | Should -BeExactly $String 24 | } 25 | } 26 | 27 | Context -Name 'Pipeline' -Fixture { 28 | It -Name 'Supports the Pipeline' -Test { 29 | $assertion = $String | ConvertTo-MemoryStream 30 | $assertion.GetType().Name | Should -BeExactly 'MemoryStream' 31 | } 32 | 33 | It -Name 'Supports array Pipeline input' -Test { 34 | $assertion = @($String, $String) | ConvertTo-MemoryStream 35 | $assertion | Should -HaveCount 2 36 | } 37 | } 38 | 39 | Context -Name 'Compressed stream' -Fixture { 40 | It -Name 'Returns a gzip compressed MemoryStream' -Test { 41 | $assertion = ConvertTo-MemoryStream -String $String -Compress 42 | $assertion.GetType().Name | Should -BeExactly 'MemoryStream' 43 | } 44 | 45 | It -Name 'Returned a MemoryStream that can still be read' -Test { 46 | $assertion = ConvertTo-MemoryStream -String $String -Compress 47 | $assertion.CanRead | Should -BeTrue 48 | } 49 | 50 | It -Name 'Returned a MemoryStream with the correct compressed length' -Test { 51 | $assertion = ConvertTo-MemoryStream -String $String -Compress 52 | $assertion.Length | Should -BeExactly 10 53 | } 54 | 55 | It -Name 'Compressed stream is shorter than the non-compressed stream' -Test { 56 | $testString = 'This string has multiple string values' 57 | $nonCompressed = ConvertTo-MemoryStream -String $testString 58 | $compressed = ConvertTo-MemoryStream -String $testString -Compress 59 | $compressed.Length | Should -BeLessThan $nonCompressed.Length 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertTo-Celsius.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a temperature from Fahrenheit to Celsius. 4 | 5 | .DESCRIPTION 6 | The ConvertTo-Celsius function converts a temperature value from Fahrenheit to Celsius. 7 | It accepts input via parameter or pipeline, validates that the temperature is not below absolute zero 8 | (-459.67°F), and returns the result rounded to two decimal places. 9 | 10 | .PARAMETER Fahrenheit 11 | The temperature in Fahrenheit to convert. Must be greater than or equal to -459.67°F (absolute zero). 12 | This parameter accepts pipeline input. 13 | 14 | .EXAMPLE 15 | ConvertTo-Celsius -Fahrenheit 32 16 | 0 17 | 18 | Converts 32°F to Celsius (0°C). 19 | 20 | .EXAMPLE 21 | ConvertTo-Celsius -Fahrenheit 98.6 22 | 37 23 | 24 | Converts normal body temperature (98.6°F) to Celsius (37°C). 25 | 26 | .EXAMPLE 27 | 212 | ConvertTo-Celsius 28 | 100 29 | 30 | Demonstrates pipeline input, converting 212°F to Celsius (100°C). 31 | 32 | .EXAMPLE 33 | ConvertTo-Celsius -Fahrenheit -40 34 | -40 35 | 36 | Converts -40°F to Celsius (-40°C), demonstrating the point where both scales intersect. 37 | 38 | .INPUTS 39 | System.Double 40 | You can pipe a double value representing the temperature in Fahrenheit to this function. 41 | 42 | .OUTPUTS 43 | System.Double 44 | Returns the temperature in Celsius as a double value, rounded to two decimal places. 45 | 46 | .NOTES 47 | The formula used is: °C = (°F - 32) × 5/9 48 | 49 | .LINK 50 | ConvertTo-Fahrenheit 51 | 52 | .LINK 53 | https://en.wikipedia.org/wiki/Celsius 54 | #> 55 | function ConvertTo-Celsius { 56 | [CmdletBinding()] 57 | [OutputType([double])] 58 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')] 59 | param ( 60 | [Parameter(Mandatory = $true, 61 | ValueFromPipeline = $true, 62 | Position = 0)] 63 | [ValidateRange(-459.67, [double]::MaxValue)] 64 | [double] 65 | $Fahrenheit 66 | ) 67 | 68 | process { 69 | try { 70 | $celsius = [ConvertCoreInterop]::fahrenheit_to_celsius($Fahrenheit) 71 | return [Math]::Round($celsius, 2) 72 | } catch { 73 | $PSCmdlet.ThrowTerminatingError( 74 | [System.Management.Automation.ErrorRecord]::new( 75 | $_.Exception.Message, 76 | 'TemperatureConversionError', 77 | [System.Management.Automation.ErrorCategory]::InvalidOperation, 78 | $Fahrenheit 79 | ) 80 | ) 81 | } 82 | } 83 | } 84 | 85 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertTo-Fahrenheit.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a temperature from Celsius to Fahrenheit. 4 | 5 | .DESCRIPTION 6 | The ConvertTo-Fahrenheit function converts a temperature value from Celsius to Fahrenheit. 7 | It accepts input via parameter or pipeline, validates that the temperature is not below absolute zero 8 | (-273.15°C), and returns the result rounded to two decimal places. 9 | 10 | .PARAMETER Celsius 11 | The temperature in Celsius to convert. Must be greater than or equal to -273.15°C (absolute zero). 12 | This parameter accepts pipeline input. 13 | 14 | .EXAMPLE 15 | ConvertTo-Fahrenheit -Celsius 0 16 | 32 17 | 18 | Converts 0°C to Fahrenheit (32°F). 19 | 20 | .EXAMPLE 21 | ConvertTo-Fahrenheit -Celsius 37 22 | 98.6 23 | 24 | Converts body temperature (37°C) to Fahrenheit (98.6°F). 25 | 26 | .EXAMPLE 27 | 100 | ConvertTo-Fahrenheit 28 | 212 29 | 30 | Demonstrates pipeline input, converting 100°C to Fahrenheit (212°F). 31 | 32 | .EXAMPLE 33 | ConvertTo-Fahrenheit -Celsius -40 34 | -40 35 | 36 | Converts -40°C to Fahrenheit (-40°F), demonstrating the point where both scales intersect. 37 | 38 | .INPUTS 39 | System.Double 40 | You can pipe a double value representing the temperature in Celsius to this function. 41 | 42 | .OUTPUTS 43 | System.Double 44 | Returns the temperature in Fahrenheit as a double value, rounded to two decimal places. 45 | 46 | .NOTES 47 | Author: Your Name 48 | Version: 1.0 49 | Date: Current Date 50 | 51 | The formula used is: °F = (°C × 9/5) + 32 52 | 53 | .LINK 54 | ConvertTo-Celsius 55 | 56 | .LINK 57 | https://en.wikipedia.org/wiki/Fahrenheit 58 | #> 59 | function ConvertTo-Fahrenheit { 60 | [CmdletBinding()] 61 | [OutputType([double])] 62 | param ( 63 | [Parameter(Mandatory = $true, 64 | ValueFromPipeline = $true, 65 | Position = 0)] 66 | [ValidateRange(-273.15, [double]::MaxValue)] 67 | [double] 68 | $Celsius 69 | ) 70 | 71 | process { 72 | try { 73 | $fahrenheit = [ConvertCoreInterop]::celsius_to_fahrenheit($Celsius) 74 | return [Math]::Round($fahrenheit, 2) 75 | } catch { 76 | $PSCmdlet.ThrowTerminatingError( 77 | [System.Management.Automation.ErrorRecord]::new( 78 | $_.Exception.Message, 79 | 'TemperatureConversionError', 80 | [System.Management.Automation.ErrorCategory]::InvalidOperation, 81 | $Celsius 82 | ) 83 | ) 84 | } 85 | } 86 | } 87 | 88 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: Convert - PowerShell Module for object conversions 2 | site_description: Convert is a PowerShell module that simplifies object conversions by exposing common requirements as PowerShell functions. 3 | site_url: https://austoonz.github.io/Convert/ 4 | repo_url: https://github.com/austoonz/Convert 5 | site_author: Andrew Pearce 6 | edit_uri: edit/main/docs/ 7 | theme: readthedocs 8 | copyright: "Convert is licensed under the MIT license" 9 | nav: 10 | - Home: index.md 11 | - Change Log: CHANGELOG.md 12 | - Functions: 13 | - ConvertFrom-Base64: functions/ConvertFrom-Base64.md 14 | - ConvertFrom-Base64ToByteArray: functions/ConvertFrom-Base64ToByteArray.md 15 | - ConvertFrom-Base64ToMemoryStream: functions/ConvertFrom-Base64ToMemoryStream.md 16 | - ConvertFrom-Base64ToString: functions/ConvertFrom-Base64ToString.md 17 | - ConvertFrom-ByteArrayToBase64: functions/ConvertFrom-ByteArrayToBase64.md 18 | - ConvertFrom-ByteArrayToMemoryStream: functions/ConvertFrom-ByteArrayToMemoryStream.md 19 | - ConvertFrom-Clixml: functions/ConvertFrom-Clixml.md 20 | - ConvertFrom-CompressedByteArrayToString: functions/ConvertFrom-CompressedByteArrayToString.md 21 | - ConvertFrom-EscapedUrl: functions/ConvertFrom-EscapedUrl.md 22 | - ConvertFrom-HashTable: functions/ConvertFrom-HashTable.md 23 | - ConvertFrom-MemoryStream: functions/ConvertFrom-MemoryStream.md 24 | - ConvertFrom-MemoryStreamToBase64: functions/ConvertFrom-MemoryStreamToBase64.md 25 | - ConvertFrom-MemoryStreamToByteArray: functions/ConvertFrom-MemoryStreamToByteArray.md 26 | - ConvertFrom-MemoryStreamToString: functions/ConvertFrom-MemoryStreamToString.md 27 | - ConvertFrom-MemoryStreamToSecureString: functions/ConvertFrom-MemoryStreamToSecureString.md 28 | - ConvertFrom-StringToBase64: functions/ConvertFrom-StringToBase64.md 29 | - ConvertFrom-StringToByteArray: functions/ConvertFrom-StringToByteArray.md 30 | - ConvertFrom-StringToCompressedByteArray: functions/ConvertFrom-StringToCompressedByteArray.md 31 | - ConvertFrom-StringToMemoryStream: functions/ConvertFrom-StringToMemoryStream.md 32 | - ConvertFrom-UnixTime: functions/ConvertFrom-UnixTime.md 33 | - ConvertTo-Base64: functions/ConvertTo-Base64.md 34 | - ConvertTo-Celsius: functions/ConvertTo-Celsius.md 35 | - ConvertTo-Clixml: functions/ConvertTo-Clixml.md 36 | - ConvertTo-EscapedUrl: functions/ConvertTo-EscapedUrl.md 37 | - ConvertTo-Fahrenheit: functions/ConvertTo-Fahrenheit.md 38 | - ConvertTo-Hash: functions/ConvertTo-Hash.md 39 | - ConvertTo-HmacHash: functions/ConvertTo-HmacHash.md 40 | - ConvertTo-MemoryStream: functions/ConvertTo-MemoryStream.md 41 | - ConvertTo-String: functions/ConvertTo-String.md 42 | - ConvertTo-TitleCase: functions/ConvertTo-TitleCase.md 43 | - ConvertTo-UnixTime: functions/ConvertTo-UnixTime.md 44 | - Get-UnixTime: functions/Get-UnixTime.md 45 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-MemoryStreamToSecureString.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a Memory Stream to a Secure String 4 | 5 | .DESCRIPTION 6 | This cmdlet converts a Memory Stream to a Secure String using a Stream Reader object. 7 | 8 | .PARAMETER MemoryStream 9 | A System.IO.MemoryStream object for conversion. 10 | 11 | .PARAMETER Stream 12 | A System.IO.Stream object for conversion. 13 | 14 | .EXAMPLE 15 | $string = 'My Super Secret Value' 16 | $bytes = [System.Text.Encoding]::UTF8.GetBytes($string) 17 | $memoryStream = [System.IO.MemoryStream]::new($bytes, 0, $bytes.Length) 18 | $secure = ConvertFrom-MemoryStreamToSecureString -MemoryStream $memoryStream 19 | $credential = [PSCredential]::new('MyValue', $secure) 20 | 21 | Converts the provided MemoryStream to a SecureString. 22 | 23 | .LINK 24 | https://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx 25 | 26 | .NOTES 27 | Additional information: 28 | https://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx 29 | https://msdn.microsoft.com/en-us/library/system.security.securestring%28v=vs.110%29.aspx 30 | #> 31 | function ConvertFrom-MemoryStreamToSecureString { 32 | [CmdletBinding(DefaultParameterSetName = 'MemoryStream')] 33 | param 34 | ( 35 | [Parameter( 36 | Mandatory = $true, 37 | ValueFromPipeline = $true, 38 | ValueFromPipelineByPropertyName = $true, 39 | ParameterSetName = 'MemoryStream')] 40 | [ValidateNotNullOrEmpty()] 41 | [System.IO.MemoryStream[]] 42 | $MemoryStream, 43 | 44 | [Parameter( 45 | Mandatory = $true, 46 | ValueFromPipelineByPropertyName = $true, 47 | ParameterSetName = 'Stream')] 48 | [ValidateNotNullOrEmpty()] 49 | [System.IO.Stream[]] 50 | $Stream 51 | ) 52 | 53 | begin { 54 | $userErrorActionPreference = $ErrorActionPreference 55 | } 56 | 57 | process { 58 | switch ($PSCmdlet.ParameterSetName) { 59 | 'MemoryStream' { 60 | $inputObject = $MemoryStream 61 | } 62 | 'Stream' { 63 | $inputObject = $Stream 64 | } 65 | } 66 | 67 | foreach ($object in $inputObject) { 68 | try { 69 | $secureString = [System.Security.SecureString]::new() 70 | $reader = [System.IO.StreamReader]::new($object) 71 | 72 | while ($reader.Peek() -ge 0) { 73 | $secureString.AppendChar($reader.Read()) 74 | } 75 | $secureString.MakeReadOnly() 76 | 77 | $secureString 78 | } catch { 79 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 80 | } finally { 81 | $reader.Dispose() 82 | } 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-Clixml.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-Clixml/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-Clixml 9 | 10 | ## SYNOPSIS 11 | 12 | Converts Clixml to an object. 13 | 14 | ## SYNTAX 15 | 16 | ```powershell 17 | ConvertFrom-Clixml [-String] [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | 22 | Converts Clixml to an object. 23 | 24 | ## EXAMPLES 25 | 26 | ### EXAMPLE 1 27 | 28 | ```powershell 29 | $xml = @" 30 | 31 | ThisIsMyString 32 | 33 | "@ 34 | ConvertFrom-Clixml -String $xml 35 | 36 | ThisIsMyString 37 | ``` 38 | 39 | ### EXAMPLE 2 40 | 41 | ```powershell 42 | $xml = @" 43 | 44 | ThisIsMyString 45 | 46 | "@ 47 | $xml | ConvertFrom-Clixml 48 | 49 | ThisIsMyString 50 | ``` 51 | 52 | ### EXAMPLE 3 53 | 54 | ```powershell 55 | $xml = @" 56 | 57 | ThisIsMyString 58 | 59 | "@ 60 | $xml2 = @" 61 | 62 | This is another string 63 | 64 | "@ 65 | ConvertFrom-Clixml -String $xml,$xml2 66 | 67 | ThisIsMyString 68 | This is another string 69 | ``` 70 | 71 | ### EXAMPLE 4 72 | 73 | ```powershell 74 | $xml = @" 75 | 76 | ThisIsMyString 77 | 78 | "@ 79 | $xml2 = @" 80 | 81 | This is another string 82 | 83 | "@ 84 | $xml,$xml2 | ConvertFrom-Clixml 85 | 86 | ThisIsMyString 87 | This is another string 88 | ``` 89 | 90 | ## PARAMETERS 91 | 92 | ### -String 93 | 94 | Clixml as a string object. 95 | 96 | ```yaml 97 | Type: String[] 98 | Parameter Sets: (All) 99 | Aliases: 100 | 101 | Required: True 102 | Position: 1 103 | Default value: None 104 | Accept pipeline input: True (ByPropertyName, ByValue) 105 | Accept wildcard characters: False 106 | ``` 107 | 108 | ### CommonParameters 109 | 110 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 111 | 112 | ## INPUTS 113 | 114 | ## OUTPUTS 115 | 116 | ### [Object[]] 117 | 118 | ## NOTES 119 | 120 | ## RELATED LINKS 121 | 122 | [https://austoonz.github.io/Convert/functions/ConvertFrom-Clixml/](https://austoonz.github.io/Convert/functions/ConvertFrom-Clixml/) 123 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertFrom-StringToMemoryStream.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe -Name $function -Fixture { 4 | BeforeAll { 5 | $String = 'ThisIsMyString' 6 | 7 | # Use the variables so IDe does not complain 8 | $null = $String 9 | } 10 | 11 | Context 'Input/Output' -Fixture { 12 | It -Name 'Converts a base64 encoded string to a MemoryStream Object' -Test { 13 | $assertion = ConvertFrom-StringToMemoryStream -String $String 14 | $assertion.GetType().Name | Should -BeExactly 'MemoryStream' 15 | } 16 | 17 | It -Name 'Returned a MemoryStream with the correct length' -Test { 18 | $assertion = ConvertFrom-StringToMemoryStream -String $String 19 | $assertion.Length | Should -BeExactly 14 20 | } 21 | 22 | It -Name 'Is a valid MemoryStream Object with the correct data' -Test { 23 | $memoryStream = ConvertFrom-StringToMemoryStream -String $String 24 | $reader = [System.IO.StreamReader]::new($memoryStream) 25 | $memoryStream.Position = 0 26 | 27 | $assertion = $reader.ReadToEnd() 28 | $assertion | Should -BeExactly $String 29 | } 30 | } 31 | 32 | Context -Name 'Pipeline' -Fixture { 33 | It -Name 'Supports the Pipeline' -Test { 34 | $assertion = $String | ConvertFrom-StringToMemoryStream 35 | $assertion.GetType().Name | Should -BeExactly 'MemoryStream' 36 | } 37 | 38 | It -Name 'Supports the Pipeline' -Test { 39 | $assertion = @($String, $String) | ConvertFrom-StringToMemoryStream 40 | $assertion | Should -HaveCount 2 41 | } 42 | } 43 | 44 | Context -Name 'Compressed stream' -Fixture { 45 | It -Name 'Returns a gzip compressed MemoryStream' -Test { 46 | $assertion = ConvertFrom-StringToMemoryStream -String $String -Compress 47 | $assertion.GetType().Name | Should -BeExactly 'MemoryStream' 48 | } 49 | 50 | It -Name 'Returned a MemoryStream that can still be read' -Test { 51 | $assertion = ConvertFrom-StringToMemoryStream -String $String -Compress 52 | $assertion.CanRead | Should -BeTrue 53 | } 54 | 55 | It -Name 'Returned a MemoryStream with the correct compressed length' -Test { 56 | $assertion = ConvertFrom-StringToMemoryStream -String $String -Compress 57 | $assertion.Length | Should -BeExactly 10 58 | } 59 | 60 | It -Name 'Compressed stream is shorter than the non-compressed stream' -Test { 61 | $testString = 'This string has multiple string values' 62 | $nonCompressed = ConvertFrom-StringToMemoryStream -String $testString 63 | $compressed = ConvertFrom-StringToMemoryStream -String $testString -Compress 64 | $compressed.Length | Should -BeLessThan $nonCompressed.Length 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /docs/functions/ConvertTo-UnixTime.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertTo-UnixTime/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertTo-UnixTime 9 | 10 | ## SYNOPSIS 11 | Converts a date time to the date time represented in Unix time. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertTo-UnixTime [[-DateTime] ] [-AsMilliseconds] [-ProgressAction ] 17 | [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts a date time to the date time represented in Unix time, which is the time in seconds that have elapsed since 22 | 00:00:00 UTC on 1 January, 1970. 23 | 24 | A switch is provided to return the time value represented in milliseconds. 25 | 26 | ## EXAMPLES 27 | 28 | ### EXAMPLE 1 29 | ``` 30 | ConvertTo-UnixTime 31 | ``` 32 | 33 | 1674712201 34 | 35 | ### EXAMPLE 2 36 | ``` 37 | Get-Date | ConvertTo-UnixTime 38 | ``` 39 | 40 | 1674683490 41 | 42 | ### EXAMPLE 3 43 | ``` 44 | ConvertTo-UnixTime -DateTime (Get-Date).AddMonths(6) 45 | ``` 46 | 47 | 1690321833 48 | 49 | ### EXAMPLE 4 50 | ``` 51 | ConvertTo-UnixTime -AsMilliseconds 52 | ``` 53 | 54 | 1674712253812 55 | 56 | ## PARAMETERS 57 | 58 | ### -DateTime 59 | A DateTime object representing the time to convert. 60 | Defaults to \`\[datetime\]::UtcNow\`. 61 | 62 | ```yaml 63 | Type: DateTime 64 | Parameter Sets: (All) 65 | Aliases: 66 | 67 | Required: False 68 | Position: 1 69 | Default value: [datetime]::UtcNow 70 | Accept pipeline input: True (ByPropertyName, ByValue) 71 | Accept wildcard characters: False 72 | ``` 73 | 74 | ### -AsMilliseconds 75 | If specified, returns the time in milliseconds that have elapsed since 00:00:00 UTC on 1 January, 1970. 76 | 77 | ```yaml 78 | Type: SwitchParameter 79 | Parameter Sets: (All) 80 | Aliases: 81 | 82 | Required: False 83 | Position: Named 84 | Default value: False 85 | Accept pipeline input: False 86 | Accept wildcard characters: False 87 | ``` 88 | 89 | ### -ProgressAction 90 | {{ Fill ProgressAction Description }} 91 | 92 | ```yaml 93 | Type: ActionPreference 94 | Parameter Sets: (All) 95 | Aliases: proga 96 | 97 | Required: False 98 | Position: Named 99 | Default value: None 100 | Accept pipeline input: False 101 | Accept wildcard characters: False 102 | ``` 103 | 104 | ### CommonParameters 105 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 106 | 107 | ## INPUTS 108 | 109 | ## OUTPUTS 110 | 111 | ### [long] 112 | ## NOTES 113 | 114 | ## RELATED LINKS 115 | 116 | [https://austoonz.github.io/Convert/functions/ConvertTo-UnixTime/](https://austoonz.github.io/Convert/functions/ConvertTo-UnixTime/) 117 | 118 | -------------------------------------------------------------------------------- /.github/workflows/docs.yml: -------------------------------------------------------------------------------- 1 | name: Documentation 2 | 3 | on: 4 | workflow_run: 5 | workflows: ["CI"] 6 | types: 7 | - completed 8 | branches: 9 | - main 10 | workflow_dispatch: 11 | 12 | permissions: 13 | contents: read 14 | pages: write 15 | id-token: write 16 | actions: read 17 | 18 | concurrency: 19 | group: 'pages' 20 | cancel-in-progress: false 21 | 22 | env: 23 | MODULE_NAME: Convert 24 | 25 | jobs: 26 | build-docs: 27 | name: Build documentation 28 | runs-on: ubuntu-latest 29 | if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }} 30 | 31 | steps: 32 | - name: Checkout code 33 | uses: actions/checkout@v4 34 | 35 | - name: Download module artifact from CI 36 | if: ${{ github.event_name == 'workflow_run' }} 37 | uses: actions/download-artifact@v4 38 | with: 39 | name: Convert-Module-Universal 40 | path: Artifacts/ 41 | run-id: ${{ github.event.workflow_run.id }} 42 | github-token: ${{ secrets.GITHUB_TOKEN }} 43 | 44 | - name: Build module (manual trigger only) 45 | if: ${{ github.event_name == 'workflow_dispatch' }} 46 | shell: pwsh 47 | run: | 48 | ./build.ps1 -Rust -Build -Targets @('x86_64-unknown-linux-gnu') 49 | mkdir -p src/Convert/Private/bin/x64 50 | cp lib/target/x86_64-unknown-linux-gnu/release/libconvert_core.so src/Convert/Private/bin/x64/ 51 | ./build.ps1 -PowerShell -Build 52 | 53 | - name: Setup Pages 54 | uses: actions/configure-pages@v5 55 | 56 | - name: Cache PowerShell modules 57 | uses: actions/cache@v4 58 | with: 59 | path: ~/.local/share/powershell/Modules 60 | key: ${{ runner.os }}-psmodules-${{ hashFiles('install_modules.ps1') }} 61 | 62 | - name: Install PowerShell modules 63 | shell: pwsh 64 | run: ./install_modules.ps1 65 | 66 | - name: Generate function documentation 67 | shell: pwsh 68 | run: ./.build/Invoke-DocumentationGeneration.ps1 -Force 69 | 70 | - name: Setup Python 71 | uses: actions/setup-python@v5 72 | with: 73 | python-version: '3.11' 74 | cache: 'pip' 75 | 76 | - name: Install MkDocs dependencies 77 | run: pip install -r docs/requirements.txt 78 | 79 | - name: Build MkDocs site 80 | run: mkdocs build --strict 81 | 82 | - name: Upload artifact 83 | uses: actions/upload-pages-artifact@v3 84 | with: 85 | path: site/ 86 | 87 | deploy-docs: 88 | name: Deploy to GitHub Pages 89 | runs-on: ubuntu-latest 90 | needs: build-docs 91 | environment: 92 | name: github-pages 93 | url: ${{ steps.deployment.outputs.page_url }} 94 | 95 | steps: 96 | - name: Deploy to GitHub Pages 97 | id: deployment 98 | uses: actions/deploy-pages@v4 99 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-CompressedByteArrayToString.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-CompressedByteArrayToString/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-CompressedByteArrayToString 9 | 10 | ## SYNOPSIS 11 | Converts a string to a byte array object. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-CompressedByteArrayToString [-ByteArray] [[-Encoding] ] 17 | [-ProgressAction ] [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts a string to a byte array object. 22 | 23 | ## EXAMPLES 24 | 25 | ### EXAMPLE 1 26 | ``` 27 | $bytes = ConvertFrom-CompressedByteArrayToString -ByteArray $byteArray 28 | $bytes.GetType() 29 | ``` 30 | 31 | IsPublic IsSerial Name BaseType 32 | -------- -------- ---- -------- 33 | True True Object\[\] System.Array 34 | 35 | $bytes\[0\].GetType() 36 | 37 | IsPublic IsSerial Name BaseType 38 | -------- -------- ---- -------- 39 | True True Byte System.ValueType 40 | 41 | ## PARAMETERS 42 | 43 | ### -ByteArray 44 | The array of bytes to convert. 45 | 46 | ```yaml 47 | Type: Byte[] 48 | Parameter Sets: (All) 49 | Aliases: 50 | 51 | Required: True 52 | Position: 1 53 | Default value: None 54 | Accept pipeline input: True (ByPropertyName, ByValue) 55 | Accept wildcard characters: False 56 | ``` 57 | 58 | ### -Encoding 59 | The encoding to use for conversion. 60 | Defaults to UTF8. 61 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 62 | 63 | ```yaml 64 | Type: String 65 | Parameter Sets: (All) 66 | Aliases: 67 | 68 | Required: False 69 | Position: 2 70 | Default value: UTF8 71 | Accept pipeline input: False 72 | Accept wildcard characters: False 73 | ``` 74 | 75 | ### -ProgressAction 76 | {{ Fill ProgressAction Description }} 77 | 78 | ```yaml 79 | Type: ActionPreference 80 | Parameter Sets: (All) 81 | Aliases: proga 82 | 83 | Required: False 84 | Position: Named 85 | Default value: None 86 | Accept pipeline input: False 87 | Accept wildcard characters: False 88 | ``` 89 | 90 | ### CommonParameters 91 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 92 | 93 | ## INPUTS 94 | 95 | ## OUTPUTS 96 | 97 | ### [String] 98 | ## NOTES 99 | 100 | ## RELATED LINKS 101 | 102 | [https://austoonz.github.io/Convert/functions/ConvertFrom-CompressedByteArrayToString/](https://austoonz.github.io/Convert/functions/ConvertFrom-CompressedByteArrayToString/) 103 | 104 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-StringToCompressedByteArray.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-StringToCompressedByteArray/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-StringToCompressedByteArray 9 | 10 | ## SYNOPSIS 11 | Converts a string to a compressed byte array object. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-StringToCompressedByteArray [-String] [[-Encoding] ] 17 | [-ProgressAction ] [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts a string to a compressed byte array object. 22 | 23 | ## EXAMPLES 24 | 25 | ### EXAMPLE 1 26 | ``` 27 | $bytes = ConvertFrom-StringToCompressedByteArray -String 'A string' 28 | $bytes.GetType() 29 | ``` 30 | 31 | IsPublic IsSerial Name BaseType 32 | -------- -------- ---- -------- 33 | True True Byte\[\] System.Array 34 | 35 | $bytes\[0\].GetType() 36 | 37 | IsPublic IsSerial Name BaseType 38 | -------- -------- ---- -------- 39 | True True Byte System.ValueType 40 | 41 | ## PARAMETERS 42 | 43 | ### -String 44 | A string object for conversion. 45 | 46 | ```yaml 47 | Type: String[] 48 | Parameter Sets: (All) 49 | Aliases: 50 | 51 | Required: True 52 | Position: 1 53 | Default value: None 54 | Accept pipeline input: True (ByPropertyName, ByValue) 55 | Accept wildcard characters: False 56 | ``` 57 | 58 | ### -Encoding 59 | The encoding to use for conversion. 60 | Defaults to UTF8. 61 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 62 | 63 | ```yaml 64 | Type: String 65 | Parameter Sets: (All) 66 | Aliases: 67 | 68 | Required: False 69 | Position: 2 70 | Default value: UTF8 71 | Accept pipeline input: False 72 | Accept wildcard characters: False 73 | ``` 74 | 75 | ### -ProgressAction 76 | {{ Fill ProgressAction Description }} 77 | 78 | ```yaml 79 | Type: ActionPreference 80 | Parameter Sets: (All) 81 | Aliases: proga 82 | 83 | Required: False 84 | Position: Named 85 | Default value: None 86 | Accept pipeline input: False 87 | Accept wildcard characters: False 88 | ``` 89 | 90 | ### CommonParameters 91 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 92 | 93 | ## INPUTS 94 | 95 | ## OUTPUTS 96 | 97 | ### [System.Collections.Generic.List[Byte[]]] 98 | ## NOTES 99 | 100 | ## RELATED LINKS 101 | 102 | [https://austoonz.github.io/Convert/functions/ConvertFrom-StringToCompressedByteArray/](https://austoonz.github.io/Convert/functions/ConvertFrom-StringToCompressedByteArray/) 103 | 104 | -------------------------------------------------------------------------------- /docs/functions/ConvertTo-Celsius.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertTo-Celsius 9 | 10 | ## SYNOPSIS 11 | Converts a temperature from Fahrenheit to Celsius. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertTo-Celsius [-Fahrenheit] [-ProgressAction ] [] 17 | ``` 18 | 19 | ## DESCRIPTION 20 | The ConvertTo-Celsius function converts a temperature value from Fahrenheit to Celsius. 21 | It accepts input via parameter or pipeline, validates that the temperature is not below absolute zero 22 | (-459.67°F), and returns the result rounded to two decimal places. 23 | 24 | ## EXAMPLES 25 | 26 | ### EXAMPLE 1 27 | ``` 28 | ConvertTo-Celsius -Fahrenheit 32 29 | 0 30 | ``` 31 | 32 | Converts 32°F to Celsius (0°C). 33 | 34 | ### EXAMPLE 2 35 | ``` 36 | ConvertTo-Celsius -Fahrenheit 98.6 37 | 37 38 | ``` 39 | 40 | Converts normal body temperature (98.6°F) to Celsius (37°C). 41 | 42 | ### EXAMPLE 3 43 | ``` 44 | 212 | ConvertTo-Celsius 45 | 100 46 | ``` 47 | 48 | Demonstrates pipeline input, converting 212°F to Celsius (100°C). 49 | 50 | ### EXAMPLE 4 51 | ``` 52 | ConvertTo-Celsius -Fahrenheit -40 53 | -40 54 | ``` 55 | 56 | Converts -40°F to Celsius (-40°C), demonstrating the point where both scales intersect. 57 | 58 | ## PARAMETERS 59 | 60 | ### -Fahrenheit 61 | The temperature in Fahrenheit to convert. 62 | Must be greater than or equal to -459.67°F (absolute zero). 63 | This parameter accepts pipeline input. 64 | 65 | ```yaml 66 | Type: Double 67 | Parameter Sets: (All) 68 | Aliases: 69 | 70 | Required: True 71 | Position: 1 72 | Default value: 0 73 | Accept pipeline input: True (ByValue) 74 | Accept wildcard characters: False 75 | ``` 76 | 77 | ### -ProgressAction 78 | {{ Fill ProgressAction Description }} 79 | 80 | ```yaml 81 | Type: ActionPreference 82 | Parameter Sets: (All) 83 | Aliases: proga 84 | 85 | Required: False 86 | Position: Named 87 | Default value: None 88 | Accept pipeline input: False 89 | Accept wildcard characters: False 90 | ``` 91 | 92 | ### CommonParameters 93 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 94 | 95 | ## INPUTS 96 | 97 | ### System.Double 98 | ### You can pipe a double value representing the temperature in Fahrenheit to this function. 99 | ## OUTPUTS 100 | 101 | ### System.Double 102 | ### Returns the temperature in Celsius as a double value, rounded to two decimal places. 103 | ## NOTES 104 | The formula used is: °C = (°F - 32) × 5/9 105 | 106 | ## RELATED LINKS 107 | 108 | [ConvertTo-Fahrenheit]() 109 | 110 | [https://en.wikipedia.org/wiki/Celsius](https://en.wikipedia.org/wiki/Celsius) 111 | 112 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-UnixTime.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-UnixTime/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-UnixTime 9 | 10 | ## SYNOPSIS 11 | Converts a date time represented in Unix time to a PowerShell DateTime object. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-UnixTime [[-UnixTime] ] [-FromMilliseconds] [-ProgressAction ] 17 | [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts a date time represented in Unix time to a PowerShell DateTime object. 22 | 23 | Supports Unix time in seconds by default, or a switch to support Unix time in milliseconds. 24 | 25 | ## EXAMPLES 26 | 27 | ### EXAMPLE 1 28 | ``` 29 | ConvertFrom-UnixTime -UnixTime 1674712047 30 | ``` 31 | 32 | Thursday, January 26, 2023 5:47:27 AM 33 | 34 | ### EXAMPLE 2 35 | ``` 36 | 1674712047 | ConvertFrom-UnixTime 37 | ``` 38 | 39 | Thursday, January 26, 2023 5:47:27 AM 40 | 41 | ### EXAMPLE 3 42 | ``` 43 | ConvertFrom-UnixTime -UnixTime 1674712048705 -FromMilliseconds 44 | ``` 45 | 46 | Thursday, January 26, 2023 5:47:28 AM 47 | 48 | ### EXAMPLE 4 49 | ``` 50 | 1674712048705 | ConvertFrom-UnixTime -FromMilliseconds 51 | ``` 52 | 53 | Thursday, January 26, 2023 5:47:28 AM 54 | 55 | ## PARAMETERS 56 | 57 | ### -UnixTime 58 | The Unix time to convert. 59 | Represented in seconds by default, or in milliseconds if the FromMilliseconds 60 | parameter is specified. 61 | 62 | ```yaml 63 | Type: Int64 64 | Parameter Sets: (All) 65 | Aliases: 66 | 67 | Required: False 68 | Position: 1 69 | Default value: 0 70 | Accept pipeline input: True (ByPropertyName, ByValue) 71 | Accept wildcard characters: False 72 | ``` 73 | 74 | ### -FromMilliseconds 75 | If specified, returns the time in milliseconds that have elapsed since 00:00:00 UTC on 1 January, 1970. 76 | 77 | ```yaml 78 | Type: SwitchParameter 79 | Parameter Sets: (All) 80 | Aliases: 81 | 82 | Required: False 83 | Position: Named 84 | Default value: False 85 | Accept pipeline input: False 86 | Accept wildcard characters: False 87 | ``` 88 | 89 | ### -ProgressAction 90 | {{ Fill ProgressAction Description }} 91 | 92 | ```yaml 93 | Type: ActionPreference 94 | Parameter Sets: (All) 95 | Aliases: proga 96 | 97 | Required: False 98 | Position: Named 99 | Default value: None 100 | Accept pipeline input: False 101 | Accept wildcard characters: False 102 | ``` 103 | 104 | ### CommonParameters 105 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 106 | 107 | ## INPUTS 108 | 109 | ## OUTPUTS 110 | 111 | ### [datetime] 112 | ## NOTES 113 | 114 | ## RELATED LINKS 115 | 116 | [https://austoonz.github.io/Convert/functions/ConvertFrom-UnixTime/](https://austoonz.github.io/Convert/functions/ConvertFrom-UnixTime/) 117 | 118 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-MemoryStreamToSecureString.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-MemoryStreamToSecureString 9 | 10 | ## SYNOPSIS 11 | Converts a Memory Stream to a Secure String 12 | 13 | ## SYNTAX 14 | 15 | ### MemoryStream (Default) 16 | ``` 17 | ConvertFrom-MemoryStreamToSecureString -MemoryStream [-ProgressAction ] 18 | [] 19 | ``` 20 | 21 | ### Stream 22 | ``` 23 | ConvertFrom-MemoryStreamToSecureString -Stream [-ProgressAction ] 24 | [] 25 | ``` 26 | 27 | ## DESCRIPTION 28 | This cmdlet converts a Memory Stream to a Secure String using a Stream Reader object. 29 | 30 | ## EXAMPLES 31 | 32 | ### EXAMPLE 1 33 | ``` 34 | $string = 'My Super Secret Value' 35 | $bytes = [System.Text.Encoding]::UTF8.GetBytes($string) 36 | $memoryStream = [System.IO.MemoryStream]::new($bytes, 0, $bytes.Length) 37 | $secure = ConvertFrom-MemoryStreamToSecureString -MemoryStream $memoryStream 38 | $credential = [PSCredential]::new('MyValue', $secure) 39 | ``` 40 | 41 | Converts the provided MemoryStream to a SecureString. 42 | 43 | ## PARAMETERS 44 | 45 | ### -MemoryStream 46 | A System.IO.MemoryStream object for conversion. 47 | 48 | ```yaml 49 | Type: MemoryStream[] 50 | Parameter Sets: MemoryStream 51 | Aliases: 52 | 53 | Required: True 54 | Position: Named 55 | Default value: None 56 | Accept pipeline input: True (ByPropertyName, ByValue) 57 | Accept wildcard characters: False 58 | ``` 59 | 60 | ### -Stream 61 | A System.IO.Stream object for conversion. 62 | 63 | ```yaml 64 | Type: Stream[] 65 | Parameter Sets: Stream 66 | Aliases: 67 | 68 | Required: True 69 | Position: Named 70 | Default value: None 71 | Accept pipeline input: True (ByPropertyName) 72 | Accept wildcard characters: False 73 | ``` 74 | 75 | ### -ProgressAction 76 | {{ Fill ProgressAction Description }} 77 | 78 | ```yaml 79 | Type: ActionPreference 80 | Parameter Sets: (All) 81 | Aliases: proga 82 | 83 | Required: False 84 | Position: Named 85 | Default value: None 86 | Accept pipeline input: False 87 | Accept wildcard characters: False 88 | ``` 89 | 90 | ### CommonParameters 91 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 92 | 93 | ## INPUTS 94 | 95 | ## OUTPUTS 96 | 97 | ## NOTES 98 | Additional information: 99 | https://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx 100 | https://msdn.microsoft.com/en-us/library/system.security.securestring%28v=vs.110%29.aspx 101 | 102 | ## RELATED LINKS 103 | 104 | [https://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx](https://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx) 105 | 106 | -------------------------------------------------------------------------------- /docs/functions/ConvertTo-Clixml.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertTo-Clixml/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertTo-Clixml 9 | 10 | ## SYNOPSIS 11 | 12 | Converts an object to Clixml. 13 | 14 | ## SYNTAX 15 | 16 | ```powershell 17 | ConvertTo-Clixml [-InputObject] [[-Depth] ] [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | 22 | Converts an object to Clixml. 23 | 24 | ## EXAMPLES 25 | 26 | ### EXAMPLE 1 27 | 28 | ```powershell 29 | $string = 'A string' 30 | ConvertTo-Clixml -InputObject $string 31 | 32 | \ 33 | \A string\ 34 | \ 35 | ``` 36 | 37 | ### EXAMPLE 2 38 | 39 | ```powershell 40 | $string = 'A string' 41 | $string | ConvertTo-Clixml 42 | 43 | \ 44 | \A string\ 45 | \ 46 | ``` 47 | 48 | ### EXAMPLE 3 49 | 50 | ```powershell 51 | $string1 = 'A string' 52 | $string2 = 'Another string' 53 | ConvertTo-Clixml -InputObject $string1,$string2 54 | 55 | \ 56 | \A string\ 57 | \ 58 | \ 59 | \Another string\ 60 | \ 61 | ``` 62 | 63 | ### EXAMPLE 4 64 | 65 | ```powershell 66 | $string1 = 'A string' 67 | $string2 = 'Another string' 68 | $string1,$string2 | ConvertTo-Clixml 69 | 70 | \ 71 | \A string\ 72 | \ 73 | \ 74 | \Another string\ 75 | \ 76 | ``` 77 | 78 | ## PARAMETERS 79 | 80 | ### -InputObject 81 | 82 | The input object to serialize 83 | 84 | ```yaml 85 | Type: PSObject 86 | Parameter Sets: (All) 87 | Aliases: 88 | 89 | Required: True 90 | Position: 1 91 | Default value: None 92 | Accept pipeline input: True (ByPropertyName, ByValue) 93 | Accept wildcard characters: False 94 | ``` 95 | 96 | ### -Depth 97 | 98 | The depth of the members to serialize 99 | 100 | ```yaml 101 | Type: Int32 102 | Parameter Sets: (All) 103 | Aliases: 104 | 105 | Required: False 106 | Position: 2 107 | Default value: 1 108 | Accept pipeline input: False 109 | Accept wildcard characters: False 110 | ``` 111 | 112 | ### CommonParameters 113 | 114 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 115 | 116 | ## INPUTS 117 | 118 | ## OUTPUTS 119 | 120 | ### [String[]] 121 | 122 | ## NOTES 123 | 124 | ## RELATED LINKS 125 | 126 | [https://austoonz.github.io/Convert/functions/ConvertTo-Clixml/](https://austoonz.github.io/Convert/functions/ConvertTo-Clixml/) 127 | -------------------------------------------------------------------------------- /docs/functions/ConvertTo-Fahrenheit.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertTo-Fahrenheit 9 | 10 | ## SYNOPSIS 11 | Converts a temperature from Celsius to Fahrenheit. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertTo-Fahrenheit [-Celsius] [-ProgressAction ] [] 17 | ``` 18 | 19 | ## DESCRIPTION 20 | The ConvertTo-Fahrenheit function converts a temperature value from Celsius to Fahrenheit. 21 | It accepts input via parameter or pipeline, validates that the temperature is not below absolute zero 22 | (-273.15°C), and returns the result rounded to two decimal places. 23 | 24 | ## EXAMPLES 25 | 26 | ### EXAMPLE 1 27 | ``` 28 | ConvertTo-Fahrenheit -Celsius 0 29 | 32 30 | ``` 31 | 32 | Converts 0°C to Fahrenheit (32°F). 33 | 34 | ### EXAMPLE 2 35 | ``` 36 | ConvertTo-Fahrenheit -Celsius 37 37 | 98.6 38 | ``` 39 | 40 | Converts body temperature (37°C) to Fahrenheit (98.6°F). 41 | 42 | ### EXAMPLE 3 43 | ``` 44 | 100 | ConvertTo-Fahrenheit 45 | 212 46 | ``` 47 | 48 | Demonstrates pipeline input, converting 100°C to Fahrenheit (212°F). 49 | 50 | ### EXAMPLE 4 51 | ``` 52 | ConvertTo-Fahrenheit -Celsius -40 53 | -40 54 | ``` 55 | 56 | Converts -40°C to Fahrenheit (-40°F), demonstrating the point where both scales intersect. 57 | 58 | ## PARAMETERS 59 | 60 | ### -Celsius 61 | The temperature in Celsius to convert. 62 | Must be greater than or equal to -273.15°C (absolute zero). 63 | This parameter accepts pipeline input. 64 | 65 | ```yaml 66 | Type: Double 67 | Parameter Sets: (All) 68 | Aliases: 69 | 70 | Required: True 71 | Position: 1 72 | Default value: 0 73 | Accept pipeline input: True (ByValue) 74 | Accept wildcard characters: False 75 | ``` 76 | 77 | ### -ProgressAction 78 | {{ Fill ProgressAction Description }} 79 | 80 | ```yaml 81 | Type: ActionPreference 82 | Parameter Sets: (All) 83 | Aliases: proga 84 | 85 | Required: False 86 | Position: Named 87 | Default value: None 88 | Accept pipeline input: False 89 | Accept wildcard characters: False 90 | ``` 91 | 92 | ### CommonParameters 93 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 94 | 95 | ## INPUTS 96 | 97 | ### System.Double 98 | ### You can pipe a double value representing the temperature in Celsius to this function. 99 | ## OUTPUTS 100 | 101 | ### System.Double 102 | ### Returns the temperature in Fahrenheit as a double value, rounded to two decimal places. 103 | ## NOTES 104 | Author: Your Name 105 | Version: 1.0 106 | Date: Current Date 107 | 108 | The formula used is: °F = (°C × 9/5) + 32 109 | 110 | ## RELATED LINKS 111 | 112 | [ConvertTo-Celsius]() 113 | 114 | [https://en.wikipedia.org/wiki/Fahrenheit](https://en.wikipedia.org/wiki/Fahrenheit) 115 | 116 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-Base64ToString.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToString/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-Base64ToString 9 | 10 | ## SYNOPSIS 11 | Converts a base64 encoded string to a string. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-Base64ToString [-String] [[-Encoding] ] [-Decompress] 17 | [-ProgressAction ] [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts a base64 encoded string to a string. 22 | 23 | ## EXAMPLES 24 | 25 | ### EXAMPLE 1 26 | ``` 27 | ConvertFrom-Base64ToString -String 'QSBzdHJpbmc=' 28 | ``` 29 | 30 | A string 31 | 32 | ### EXAMPLE 2 33 | ``` 34 | ConvertTo-Base64 -String 'A string','Another string' 35 | ``` 36 | 37 | QSBzdHJpbmc= 38 | QW5vdGhlciBzdHJpbmc= 39 | 40 | ### EXAMPLE 3 41 | ``` 42 | 'QSBzdHJpbmc=' | ConvertFrom-Base64ToString 43 | ``` 44 | 45 | A string 46 | 47 | ### EXAMPLE 4 48 | ``` 49 | 'QSBzdHJpbmc=','QW5vdGhlciBzdHJpbmc=' | ConvertFrom-Base64ToString 50 | ``` 51 | 52 | A string 53 | Another string 54 | 55 | ## PARAMETERS 56 | 57 | ### -String 58 | A Base64 Encoded String 59 | 60 | ```yaml 61 | Type: String[] 62 | Parameter Sets: (All) 63 | Aliases: Base64String 64 | 65 | Required: True 66 | Position: 1 67 | Default value: None 68 | Accept pipeline input: True (ByPropertyName, ByValue) 69 | Accept wildcard characters: False 70 | ``` 71 | 72 | ### -Encoding 73 | The encoding to use for conversion. 74 | Defaults to UTF8. 75 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 76 | 77 | ```yaml 78 | Type: String 79 | Parameter Sets: (All) 80 | Aliases: 81 | 82 | Required: False 83 | Position: 2 84 | Default value: UTF8 85 | Accept pipeline input: False 86 | Accept wildcard characters: False 87 | ``` 88 | 89 | ### -Decompress 90 | If supplied, the output will be decompressed using Gzip. 91 | 92 | ```yaml 93 | Type: SwitchParameter 94 | Parameter Sets: (All) 95 | Aliases: 96 | 97 | Required: False 98 | Position: Named 99 | Default value: False 100 | Accept pipeline input: False 101 | Accept wildcard characters: False 102 | ``` 103 | 104 | ### -ProgressAction 105 | {{ Fill ProgressAction Description }} 106 | 107 | ```yaml 108 | Type: ActionPreference 109 | Parameter Sets: (All) 110 | Aliases: proga 111 | 112 | Required: False 113 | Position: Named 114 | Default value: None 115 | Accept pipeline input: False 116 | Accept wildcard characters: False 117 | ``` 118 | 119 | ### CommonParameters 120 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 121 | 122 | ## INPUTS 123 | 124 | ## OUTPUTS 125 | 126 | ### [String[]] 127 | ## NOTES 128 | 129 | ## RELATED LINKS 130 | 131 | [https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToString/](https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToString/) 132 | 133 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertTo-String.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe -Name $function -Fixture { 4 | BeforeEach { 5 | $String = 'ThisIsMyString' 6 | 7 | # Use the variables so IDe does not complain 8 | $null = $String 9 | } 10 | 11 | Context -Name 'String input' -Fixture { 12 | It -Name 'Converts to base64 correctly' -Test { 13 | $base64 = 'VGhpc0lzTXlTdHJpbmc=' 14 | $assertion = ConvertTo-String -Base64EncodedString $base64 -Encoding UTF8 15 | 16 | $assertion | Should -BeExactly $String 17 | } 18 | 19 | It -Name 'Converts from Pipeline' -Test { 20 | $base64 = 'VGhpc0lzTXlTdHJpbmc=' 21 | $assertion = $base64 | ConvertTo-String -Encoding UTF8 22 | 23 | $assertion | Should -BeExactly $String 24 | } 25 | 26 | It -Name 'Converts an array from Pipeline' -Test { 27 | $base64 = 'VGhpc0lzTXlTdHJpbmc=' 28 | $assertion = $base64, $base64 | ConvertTo-String -Encoding UTF8 29 | 30 | $assertion | Should -HaveCount 2 31 | } 32 | 33 | It -Name 'Converts from compressed base64 string' -Test { 34 | $base64 = 'H4sIAAAAAAAEAAthyGDIZChm8ARiX4ZKhmCGEoYioEgeQzoDAC8A9r4cAAAA' 35 | $assertion = ConvertTo-String -Base64EncodedString $base64 -Encoding Unicode -Decompress 36 | 37 | $assertion | Should -BeExactly $String 38 | } 39 | } 40 | 41 | Context -Name 'MemoryStream input' -Fixture { 42 | It -Name 'Converts to base64 correctly' -Test { 43 | $stream = [System.IO.MemoryStream]::new() 44 | $writer = [System.IO.StreamWriter]::new($stream) 45 | $writer.Write($string) 46 | $writer.Flush() 47 | 48 | $assertion = ConvertTo-String -MemoryStream $stream 49 | 50 | $assertion | Should -BeExactly $String 51 | 52 | $stream.Dispose() 53 | $writer.Dispose() 54 | } 55 | 56 | It -Name 'Converts from Pipeline' -Test { 57 | $stream = [System.IO.MemoryStream]::new() 58 | $writer = [System.IO.StreamWriter]::new($stream) 59 | $writer.Write($string) 60 | $writer.Flush() 61 | 62 | $assertion = $stream | ConvertTo-String 63 | 64 | $assertion | Should -BeExactly $String 65 | 66 | $stream.Dispose() 67 | $writer.Dispose() 68 | } 69 | 70 | It -Name 'Converts an array from Pipeline' -Test { 71 | $stream = [System.IO.MemoryStream]::new() 72 | $writer = [System.IO.StreamWriter]::new($stream) 73 | $writer.Write($string) 74 | $writer.Flush() 75 | 76 | $stream2 = [System.IO.MemoryStream]::new() 77 | $writer2 = [System.IO.StreamWriter]::new($stream2) 78 | $writer2.Write($String) 79 | $writer2.Flush() 80 | 81 | $assertion = @($stream, $stream2) | ConvertTo-String 82 | 83 | $assertion | Should -HaveCount 2 84 | 85 | $stream.Dispose() 86 | $stream2.Dispose() 87 | $writer.Dispose() 88 | $writer2.Dispose() 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-StringToCompressedByteArray.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a string to a compressed byte array object. 4 | 5 | .DESCRIPTION 6 | Converts a string to a compressed byte array object. 7 | 8 | .PARAMETER String 9 | A string object for conversion. 10 | 11 | .PARAMETER Encoding 12 | The encoding to use for conversion. 13 | Defaults to UTF8. 14 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 15 | 16 | .EXAMPLE 17 | $bytes = ConvertFrom-StringToCompressedByteArray -String 'A string' 18 | $bytes.GetType() 19 | 20 | IsPublic IsSerial Name BaseType 21 | -------- -------- ---- -------- 22 | True True Byte[] System.Array 23 | 24 | $bytes[0].GetType() 25 | 26 | IsPublic IsSerial Name BaseType 27 | -------- -------- ---- -------- 28 | True True Byte System.ValueType 29 | 30 | .OUTPUTS 31 | [System.Collections.Generic.List[Byte[]]] 32 | 33 | .LINK 34 | https://austoonz.github.io/Convert/functions/ConvertFrom-StringToCompressedByteArray/ 35 | #> 36 | function ConvertFrom-StringToCompressedByteArray { 37 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-StringToCompressedByteArray/')] 38 | param 39 | ( 40 | [Parameter( 41 | Mandatory = $true, 42 | ValueFromPipeline = $true, 43 | ValueFromPipelineByPropertyName = $true)] 44 | [ValidateNotNullOrEmpty()] 45 | [String[]] 46 | $String, 47 | 48 | [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF8')] 49 | [String] 50 | $Encoding = 'UTF8' 51 | ) 52 | 53 | begin { 54 | $userErrorActionPreference = $ErrorActionPreference 55 | $nullPtr = [IntPtr]::Zero 56 | } 57 | 58 | process { 59 | foreach ($s in $String) { 60 | $byteArrayObject = [System.Collections.Generic.List[Byte[]]]::new() 61 | try { 62 | $ptr = $nullPtr 63 | try { 64 | $length = [UIntPtr]::Zero 65 | $ptr = [ConvertCoreInterop]::compress_string($s, $Encoding, [ref]$length) 66 | 67 | if ($ptr -eq $nullPtr) { 68 | $errorMsg = GetRustError -DefaultMessage "Compression failed for encoding '$Encoding'" 69 | throw $errorMsg 70 | } 71 | 72 | $bytes = New-Object byte[] $length.ToUInt64() 73 | [System.Runtime.InteropServices.Marshal]::Copy($ptr, $bytes, 0, $bytes.Length) 74 | 75 | $null = $byteArrayObject.Add($bytes) 76 | $byteArrayObject 77 | } finally { 78 | if ($ptr -ne $nullPtr) { 79 | [ConvertCoreInterop]::free_bytes($ptr) 80 | } 81 | } 82 | } catch { 83 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 84 | } 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-CompressedByteArrayToString.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a string to a byte array object. 4 | 5 | .DESCRIPTION 6 | Converts a string to a byte array object. 7 | 8 | .PARAMETER ByteArray 9 | The array of bytes to convert. 10 | 11 | .PARAMETER Encoding 12 | The encoding to use for conversion. 13 | Defaults to UTF8. 14 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 15 | 16 | .EXAMPLE 17 | $bytes = ConvertFrom-CompressedByteArrayToString -ByteArray $byteArray 18 | $bytes.GetType() 19 | 20 | IsPublic IsSerial Name BaseType 21 | -------- -------- ---- -------- 22 | True True Object[] System.Array 23 | 24 | $bytes[0].GetType() 25 | 26 | IsPublic IsSerial Name BaseType 27 | -------- -------- ---- -------- 28 | True True Byte System.ValueType 29 | 30 | .OUTPUTS 31 | [String] 32 | 33 | .LINK 34 | https://austoonz.github.io/Convert/functions/ConvertFrom-CompressedByteArrayToString/ 35 | #> 36 | function ConvertFrom-CompressedByteArrayToString { 37 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-CompressedByteArrayToString/')] 38 | param 39 | ( 40 | [Parameter( 41 | Mandatory = $true, 42 | ValueFromPipeline = $true, 43 | ValueFromPipelineByPropertyName = $true)] 44 | [ValidateNotNullOrEmpty()] 45 | [Byte[]] 46 | $ByteArray, 47 | 48 | [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF8')] 49 | [String] 50 | $Encoding = 'UTF8' 51 | ) 52 | 53 | begin { 54 | $userErrorActionPreference = $ErrorActionPreference 55 | $nullPtr = [IntPtr]::Zero 56 | } 57 | 58 | process { 59 | try { 60 | $ptr = $nullPtr 61 | try { 62 | # Pin the byte array in memory and get a pointer to it 63 | $pinnedArray = [System.Runtime.InteropServices.GCHandle]::Alloc($ByteArray, [System.Runtime.InteropServices.GCHandleType]::Pinned) 64 | try { 65 | $byteArrayPtr = $pinnedArray.AddrOfPinnedObject() 66 | $length = [UIntPtr]::new($ByteArray.Length) 67 | 68 | $ptr = [ConvertCoreInterop]::decompress_string($byteArrayPtr, $length, $Encoding) 69 | 70 | if ($ptr -eq $nullPtr) { 71 | $errorMsg = GetRustError -DefaultMessage "Decompression failed for encoding '$Encoding'" 72 | throw $errorMsg 73 | } 74 | 75 | ConvertPtrToString -Ptr $ptr 76 | } finally { 77 | if ($pinnedArray.IsAllocated) { 78 | $pinnedArray.Free() 79 | } 80 | } 81 | } finally { 82 | if ($ptr -ne $nullPtr) { 83 | [ConvertCoreInterop]::free_string($ptr) 84 | } 85 | } 86 | } catch { 87 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 88 | } 89 | } 90 | } -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-Base64ToString.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a base64 encoded string to a string. 4 | 5 | .DESCRIPTION 6 | Converts a base64 encoded string to a string. 7 | 8 | .PARAMETER String 9 | A Base64 Encoded String 10 | 11 | .PARAMETER Encoding 12 | The encoding to use for conversion. 13 | Defaults to UTF8. 14 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 15 | 16 | .PARAMETER Decompress 17 | If supplied, the output will be decompressed using Gzip. 18 | 19 | .EXAMPLE 20 | ConvertFrom-Base64ToString -String 'QSBzdHJpbmc=' 21 | 22 | A string 23 | 24 | .EXAMPLE 25 | ConvertTo-Base64 -String 'A string','Another string' 26 | 27 | QSBzdHJpbmc= 28 | QW5vdGhlciBzdHJpbmc= 29 | 30 | .EXAMPLE 31 | 'QSBzdHJpbmc=' | ConvertFrom-Base64ToString 32 | 33 | A string 34 | 35 | .EXAMPLE 36 | 'QSBzdHJpbmc=','QW5vdGhlciBzdHJpbmc=' | ConvertFrom-Base64ToString 37 | 38 | A string 39 | Another string 40 | 41 | .OUTPUTS 42 | [String[]] 43 | 44 | .LINK 45 | https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToString/ 46 | #> 47 | function ConvertFrom-Base64ToString { 48 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-Base64ToString/')] 49 | [OutputType('String')] 50 | [Alias('ConvertFrom-Base64StringToString')] 51 | param 52 | ( 53 | [Parameter( 54 | Mandatory = $true, 55 | ValueFromPipeline = $true, 56 | ValueFromPipelineByPropertyName = $true)] 57 | [ValidateNotNullOrEmpty()] 58 | [Alias('Base64String')] 59 | [String[]] 60 | $String, 61 | 62 | [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF8')] 63 | [String] 64 | $Encoding = 'UTF8', 65 | 66 | [Parameter(Mandatory = $false)] 67 | [Switch] 68 | $Decompress 69 | ) 70 | 71 | begin { 72 | $userErrorActionPreference = $ErrorActionPreference 73 | $nullPtr = [IntPtr]::Zero 74 | } 75 | 76 | process { 77 | foreach ($s in $String) { 78 | try { 79 | if ($Decompress) { 80 | $bytes = [System.Convert]::FromBase64String($s) 81 | ConvertFrom-CompressedByteArrayToString -ByteArray $bytes -Encoding $Encoding 82 | } else { 83 | $ptr = $nullPtr 84 | try { 85 | $ptr = [ConvertCoreInterop]::base64_to_string($s, $Encoding) 86 | 87 | if ($ptr -eq $nullPtr) { 88 | $errorMsg = GetRustError -DefaultMessage "Base64 decoding failed for encoding '$Encoding'" 89 | throw $errorMsg 90 | } 91 | 92 | ConvertPtrToString -Ptr $ptr 93 | } finally { 94 | if ($ptr -ne $nullPtr) { 95 | [ConvertCoreInterop]::free_string($ptr) 96 | } 97 | } 98 | } 99 | } catch { 100 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 101 | } 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /.build/Invoke-PublishModule.ps1: -------------------------------------------------------------------------------- 1 | param( 2 | [Parameter(Mandatory)] 3 | [string]$ModuleName, 4 | 5 | [Parameter(Mandatory)] 6 | [string]$ArtifactPath 7 | ) 8 | 9 | $ErrorActionPreference = 'Stop' 10 | 11 | # Validate API key from environment 12 | $apiKey = $env:PSGALLERY_API_KEY 13 | if ([string]::IsNullOrEmpty($apiKey)) { 14 | Write-Error 'PSGALLERY_API_KEY environment variable is not set' 15 | } 16 | 17 | # Find manifest 18 | $manifestPath = [System.IO.Path]::Combine($ArtifactPath, "$ModuleName.psd1") 19 | if (-not [System.IO.File]::Exists($manifestPath)) { 20 | # Try recursive search as fallback 21 | $found = Get-ChildItem -Path $ArtifactPath -Filter "$ModuleName.psd1" -Recurse -ErrorAction SilentlyContinue | 22 | Select-Object -First 1 -ExpandProperty FullName 23 | if ($found) { 24 | $manifestPath = $found 25 | } else { 26 | Write-Error "$ModuleName.psd1 not found in $ArtifactPath" 27 | } 28 | } 29 | 30 | Write-Host "Found manifest at: $manifestPath" 31 | $modulePath = [System.IO.Path]::GetDirectoryName($manifestPath) 32 | Write-Host "Module path: $modulePath" 33 | 34 | # Extract version from manifest 35 | $manifest = Import-PowerShellDataFile -Path $manifestPath 36 | $version = $manifest.ModuleVersion 37 | $prerelease = $manifest.PrivateData.PSData.Prerelease 38 | $fullVersion = if ($prerelease) { "$version-$prerelease" } else { $version } 39 | 40 | Write-Host "Module version: $fullVersion" 41 | 42 | # Check if version exists on Gallery 43 | $findParams = @{ Name = $ModuleName; RequiredVersion = $version; ErrorAction = 'SilentlyContinue' } 44 | if ($prerelease) { $findParams['AllowPrerelease'] = $true } 45 | 46 | if (Find-Module @findParams) { 47 | Write-Host "::notice::Version $fullVersion already exists on PowerShell Gallery - skipping publish" 48 | 49 | if ($env:GITHUB_STEP_SUMMARY) { 50 | @" 51 | ## ⏭️ Publish Skipped 52 | 53 | **Module:** $ModuleName 54 | **Version:** $fullVersion 55 | 56 | This version already exists on [PowerShell Gallery](https://www.powershellgallery.com/packages/$ModuleName/$version). 57 | 58 | To publish a new version, increment the version in ``src/$ModuleName/$ModuleName.psd1``. 59 | "@ >> $env:GITHUB_STEP_SUMMARY 60 | } 61 | exit 0 62 | } 63 | 64 | # Create proper module structure: ModuleName\Version\ 65 | $moduleVersionPath = [System.IO.Path]::Combine((Get-Location), 'publish', $publishRoot, $ModuleName, $version) 66 | 67 | Write-Host "Creating module structure at: $moduleVersionPath" 68 | New-Item -Path $moduleVersionPath -ItemType Directory -Force | Out-Null 69 | Copy-Item -Path ([System.IO.Path]::Combine($modulePath, '*')) -Destination $moduleVersionPath -Recurse -Force 70 | 71 | Write-Host 'Module structure created successfully' 72 | 73 | # Publish 74 | Write-Host "Publishing $ModuleName $fullVersion to PowerShell Gallery..." 75 | Publish-Module -Path $moduleVersionPath -NuGetApiKey $apiKey -Repository 'PSGallery' 76 | 77 | # Write success summary 78 | if ($env:GITHUB_STEP_SUMMARY) { 79 | $installCmd = if ($prerelease) { "Install-Module -Name $ModuleName -AllowPrerelease" } else { "Install-Module -Name $ModuleName" } 80 | @" 81 | ## ✅ Published Successfully 82 | 83 | **Module:** $ModuleName 84 | **Version:** $fullVersion 85 | 86 | ### Installation 87 | ``````powershell 88 | $installCmd 89 | `````` 90 | 91 | [View on PowerShell Gallery](https://www.powershellgallery.com/packages/$ModuleName/$version) 92 | "@ >> $env:GITHUB_STEP_SUMMARY 93 | } 94 | 95 | Write-Host "Successfully published $ModuleName $fullVersion" 96 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-Base64.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a base64 encoded string to a string. 4 | 5 | .DESCRIPTION 6 | Converts a base64 encoded string to a string. 7 | 8 | .PARAMETER Base64 9 | A Base64 Encoded String. 10 | 11 | .PARAMETER Encoding 12 | The encoding to use for conversion. 13 | Defaults to UTF8. 14 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 15 | 16 | .PARAMETER ToString 17 | Switch parameter to specify a conversion to a string object. 18 | 19 | .PARAMETER Decompress 20 | If supplied, the output will be decompressed using Gzip. 21 | 22 | .EXAMPLE 23 | ConvertFrom-Base64 -Base64 'QSBzdHJpbmc=' -ToString 24 | 25 | A string 26 | 27 | .EXAMPLE 28 | ConvertTo-Base64 -Base64 'A string','Another string' -ToString 29 | 30 | QSBzdHJpbmc= 31 | QW5vdGhlciBzdHJpbmc= 32 | 33 | .EXAMPLE 34 | 'QSBzdHJpbmc=' | ConvertFrom-Base64 -ToString 35 | 36 | A string 37 | 38 | .EXAMPLE 39 | 'QSBzdHJpbmc=','QW5vdGhlciBzdHJpbmc=' | ConvertFrom-Base64 -ToString 40 | 41 | A string 42 | Another string 43 | 44 | .OUTPUTS 45 | [String[]] 46 | 47 | .LINK 48 | https://austoonz.github.io/Convert/functions/ConvertFrom-Base64/ 49 | #> 50 | function ConvertFrom-Base64 { 51 | [CmdletBinding( 52 | DefaultParameterSetName = 'Default', 53 | HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-Base64/')] 54 | [OutputType('String')] 55 | param 56 | ( 57 | [Parameter( 58 | ParameterSetName = 'Default', 59 | Mandatory = $true, 60 | ValueFromPipeline = $true, 61 | ValueFromPipelineByPropertyName = $true)] 62 | [Parameter( 63 | ParameterSetName = 'ToString', 64 | Mandatory = $true, 65 | ValueFromPipeline = $true, 66 | ValueFromPipelineByPropertyName = $true)] 67 | [ValidateNotNullOrEmpty()] 68 | [Alias('String', 'Base64String')] 69 | [String[]] 70 | $Base64, 71 | 72 | [Parameter(ParameterSetName = 'ToString')] 73 | [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF8')] 74 | [String] 75 | $Encoding = 'UTF8', 76 | 77 | [Parameter(ParameterSetName = 'ToString')] 78 | [Parameter(Mandatory = $false)] 79 | [Switch] 80 | $ToString, 81 | 82 | [Parameter(ParameterSetName = 'ToString')] 83 | [Parameter(Mandatory = $false)] 84 | [Switch] 85 | $Decompress 86 | ) 87 | 88 | begin { 89 | $userErrorActionPreference = $ErrorActionPreference 90 | } 91 | 92 | process { 93 | foreach ($b64 in $Base64) { 94 | try { 95 | $bytes = [System.Convert]::FromBase64String($b64) 96 | 97 | if ($ToString) { 98 | if ($Decompress) { 99 | ConvertFrom-CompressedByteArrayToString -ByteArray $bytes -Encoding $Encoding 100 | } else { 101 | [System.Text.Encoding]::$Encoding.GetString($bytes) 102 | } 103 | } else { 104 | $bytes 105 | } 106 | } catch { 107 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 108 | } 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-MemoryStreamToBase64.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts MemoryStream to a base64 encoded string. 4 | 5 | .DESCRIPTION 6 | Converts MemoryStream to a base64 encoded string. 7 | 8 | .PARAMETER MemoryStream 9 | A MemoryStream object for conversion. 10 | 11 | .PARAMETER Encoding 12 | The encoding to use for conversion. 13 | Defaults to UTF8. 14 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 15 | 16 | .EXAMPLE 17 | $string = 'A string' 18 | $stream = [System.IO.MemoryStream]::new() 19 | $writer = [System.IO.StreamWriter]::new($stream) 20 | $writer.Write($string) 21 | $writer.Flush() 22 | 23 | ConvertFrom-MemoryStreamToBase64 -MemoryStream $stream 24 | 25 | QSBzdHJpbmc= 26 | 27 | .EXAMPLE 28 | $string = 'A string' 29 | $stream = [System.IO.MemoryStream]::new() 30 | $writer = [System.IO.StreamWriter]::new($stream) 31 | $writer.Write($string) 32 | $writer.Flush() 33 | 34 | $stream | ConvertFrom-MemoryStreamToBase64 35 | 36 | QSBzdHJpbmc= 37 | 38 | .EXAMPLE 39 | $string1 = 'A string' 40 | $stream1 = [System.IO.MemoryStream]::new() 41 | $writer1 = [System.IO.StreamWriter]::new($stream1) 42 | $writer1.Write($string1) 43 | $writer1.Flush() 44 | 45 | $string2 = 'Another string' 46 | $stream2 = [System.IO.MemoryStream]::new() 47 | $writer2 = [System.IO.StreamWriter]::new($stream2) 48 | $writer2.Write($string2) 49 | $writer2.Flush() 50 | 51 | ConvertFrom-MemoryStreamToBase64 -MemoryStream $stream1,$stream2 52 | 53 | QSBzdHJpbmc= 54 | QW5vdGhlciBzdHJpbmc= 55 | 56 | .EXAMPLE 57 | $string1 = 'A string' 58 | $stream1 = [System.IO.MemoryStream]::new() 59 | $writer1 = [System.IO.StreamWriter]::new($stream1) 60 | $writer1.Write($string1) 61 | $writer1.Flush() 62 | 63 | $string2 = 'Another string' 64 | $stream2 = [System.IO.MemoryStream]::new() 65 | $writer2 = [System.IO.StreamWriter]::new($stream2) 66 | $writer2.Write($string2) 67 | $writer2.Flush() 68 | 69 | $stream1,$stream2 | ConvertFrom-MemoryStreamToBase64 70 | 71 | QSBzdHJpbmc= 72 | QW5vdGhlciBzdHJpbmc= 73 | 74 | .OUTPUTS 75 | [String[]] 76 | 77 | .LINK 78 | https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToBase64/ 79 | #> 80 | function ConvertFrom-MemoryStreamToBase64 { 81 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToBase64/')] 82 | param 83 | ( 84 | [Parameter( 85 | Mandatory = $true, 86 | ValueFromPipeline = $true, 87 | ValueFromPipelineByPropertyName = $true)] 88 | [ValidateNotNullOrEmpty()] 89 | [System.IO.MemoryStream[]] 90 | $MemoryStream 91 | ) 92 | 93 | begin { 94 | $userErrorActionPreference = $ErrorActionPreference 95 | } 96 | 97 | process { 98 | foreach ($m in $MemoryStream) { 99 | try { 100 | $byteArray = ConvertFrom-MemoryStreamToByteArray -MemoryStream $m 101 | ConvertFrom-ByteArrayToBase64 -ByteArray $byteArray 102 | } catch { 103 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 104 | } 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-StringToByteArray.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-StringToByteArray/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-StringToByteArray 9 | 10 | ## SYNOPSIS 11 | Converts a string to a byte array object. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-StringToByteArray [-String] [[-Encoding] ] [-ProgressAction ] 17 | [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts a string to a byte array object. 22 | 23 | ## EXAMPLES 24 | 25 | ### EXAMPLE 1 26 | ``` 27 | $bytes = ConvertFrom-StringToByteArray -String 'A string' 28 | $bytes.GetType() 29 | ``` 30 | 31 | IsPublic IsSerial Name BaseType 32 | -------- -------- ---- -------- 33 | True True Byte\[\] System.Array 34 | 35 | $bytes\[0\].GetType() 36 | 37 | IsPublic IsSerial Name BaseType 38 | -------- -------- ---- -------- 39 | True True Byte System.ValueType 40 | 41 | ### EXAMPLE 2 42 | ``` 43 | $bytes = 'A string','Another string' | ConvertFrom-StringToByteArray 44 | ``` 45 | 46 | $bytes.Count 47 | 2 48 | 49 | $bytes.GetType() 50 | 51 | IsPublic IsSerial Name BaseType 52 | -------- -------- ---- -------- 53 | True True Object\[\] System.Array 54 | 55 | $bytes\[0\].GetType() 56 | 57 | IsPublic IsSerial Name BaseType 58 | -------- -------- ---- -------- 59 | True True Byte\[\] System.Array 60 | 61 | ## PARAMETERS 62 | 63 | ### -String 64 | A string object for conversion. 65 | 66 | ```yaml 67 | Type: String[] 68 | Parameter Sets: (All) 69 | Aliases: 70 | 71 | Required: True 72 | Position: 1 73 | Default value: None 74 | Accept pipeline input: True (ByPropertyName, ByValue) 75 | Accept wildcard characters: False 76 | ``` 77 | 78 | ### -Encoding 79 | The encoding to use for conversion. 80 | Defaults to UTF8. 81 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 82 | 83 | ```yaml 84 | Type: String 85 | Parameter Sets: (All) 86 | Aliases: 87 | 88 | Required: False 89 | Position: 2 90 | Default value: UTF8 91 | Accept pipeline input: False 92 | Accept wildcard characters: False 93 | ``` 94 | 95 | ### -ProgressAction 96 | {{ Fill ProgressAction Description }} 97 | 98 | ```yaml 99 | Type: ActionPreference 100 | Parameter Sets: (All) 101 | Aliases: proga 102 | 103 | Required: False 104 | Position: Named 105 | Default value: None 106 | Accept pipeline input: False 107 | Accept wildcard characters: False 108 | ``` 109 | 110 | ### CommonParameters 111 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 112 | 113 | ## INPUTS 114 | 115 | ## OUTPUTS 116 | 117 | ### [System.Collections.Generic.List[Byte[]]] 118 | ## NOTES 119 | 120 | ## RELATED LINKS 121 | 122 | [https://austoonz.github.io/Convert/functions/ConvertFrom-StringToByteArray/](https://austoonz.github.io/Convert/functions/ConvertFrom-StringToByteArray/) 123 | 124 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-MemoryStreamToBase64.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToBase64/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-MemoryStreamToBase64 9 | 10 | ## SYNOPSIS 11 | Converts MemoryStream to a base64 encoded string. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-MemoryStreamToBase64 [-MemoryStream] [-ProgressAction ] 17 | [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts MemoryStream to a base64 encoded string. 22 | 23 | ## EXAMPLES 24 | 25 | ### EXAMPLE 1 26 | ``` 27 | $string = 'A string' 28 | $stream = [System.IO.MemoryStream]::new() 29 | $writer = [System.IO.StreamWriter]::new($stream) 30 | $writer.Write($string) 31 | $writer.Flush() 32 | ``` 33 | 34 | ConvertFrom-MemoryStreamToBase64 -MemoryStream $stream 35 | 36 | QSBzdHJpbmc= 37 | 38 | ### EXAMPLE 2 39 | ``` 40 | $string = 'A string' 41 | $stream = [System.IO.MemoryStream]::new() 42 | $writer = [System.IO.StreamWriter]::new($stream) 43 | $writer.Write($string) 44 | $writer.Flush() 45 | ``` 46 | 47 | $stream | ConvertFrom-MemoryStreamToBase64 48 | 49 | QSBzdHJpbmc= 50 | 51 | ### EXAMPLE 3 52 | ``` 53 | $string1 = 'A string' 54 | $stream1 = [System.IO.MemoryStream]::new() 55 | $writer1 = [System.IO.StreamWriter]::new($stream1) 56 | $writer1.Write($string1) 57 | $writer1.Flush() 58 | ``` 59 | 60 | $string2 = 'Another string' 61 | $stream2 = \[System.IO.MemoryStream\]::new() 62 | $writer2 = \[System.IO.StreamWriter\]::new($stream2) 63 | $writer2.Write($string2) 64 | $writer2.Flush() 65 | 66 | ConvertFrom-MemoryStreamToBase64 -MemoryStream $stream1,$stream2 67 | 68 | QSBzdHJpbmc= 69 | QW5vdGhlciBzdHJpbmc= 70 | 71 | ### EXAMPLE 4 72 | ``` 73 | $string1 = 'A string' 74 | $stream1 = [System.IO.MemoryStream]::new() 75 | $writer1 = [System.IO.StreamWriter]::new($stream1) 76 | $writer1.Write($string1) 77 | $writer1.Flush() 78 | ``` 79 | 80 | $string2 = 'Another string' 81 | $stream2 = \[System.IO.MemoryStream\]::new() 82 | $writer2 = \[System.IO.StreamWriter\]::new($stream2) 83 | $writer2.Write($string2) 84 | $writer2.Flush() 85 | 86 | $stream1,$stream2 | ConvertFrom-MemoryStreamToBase64 87 | 88 | QSBzdHJpbmc= 89 | QW5vdGhlciBzdHJpbmc= 90 | 91 | ## PARAMETERS 92 | 93 | ### -MemoryStream 94 | A MemoryStream object for conversion. 95 | 96 | ```yaml 97 | Type: MemoryStream[] 98 | Parameter Sets: (All) 99 | Aliases: 100 | 101 | Required: True 102 | Position: 1 103 | Default value: None 104 | Accept pipeline input: True (ByPropertyName, ByValue) 105 | Accept wildcard characters: False 106 | ``` 107 | 108 | ### -ProgressAction 109 | {{ Fill ProgressAction Description }} 110 | 111 | ```yaml 112 | Type: ActionPreference 113 | Parameter Sets: (All) 114 | Aliases: proga 115 | 116 | Required: False 117 | Position: Named 118 | Default value: None 119 | Accept pipeline input: False 120 | Accept wildcard characters: False 121 | ``` 122 | 123 | ### CommonParameters 124 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 125 | 126 | ## INPUTS 127 | 128 | ## OUTPUTS 129 | 130 | ### [String[]] 131 | ## NOTES 132 | 133 | ## RELATED LINKS 134 | 135 | [https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToBase64/](https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToBase64/) 136 | 137 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-ByteArrayToBase64.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a byte array to a base64 encoded string. 4 | 5 | .DESCRIPTION 6 | Converts a byte array to a base64 encoded string. 7 | 8 | .PARAMETER ByteArray 9 | A byte array object for conversion. 10 | 11 | .PARAMETER Compress 12 | If supplied, the output will be compressed using Gzip. 13 | 14 | .EXAMPLE 15 | $bytes = ConvertFrom-StringToCompressedByteArray -String 'A string' 16 | ConvertFrom-ByteArrayToBase64 -ByteArray $bytes 17 | 18 | H4sIAAAAAAAAC3NUKC4pysxLBwCMN9RgCAAAAA== 19 | 20 | .OUTPUTS 21 | [String[]] 22 | 23 | .LINK 24 | https://austoonz.github.io/Convert/functions/ConvertFrom-ByteArrayToBase64/ 25 | #> 26 | function ConvertFrom-ByteArrayToBase64 { 27 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-ByteArrayToBase64/')] 28 | [Alias('ConvertFrom-ByteArrayToBase64String')] 29 | param 30 | ( 31 | [Parameter( 32 | Mandatory = $true, 33 | ValueFromPipeline = $true, 34 | ValueFromPipelineByPropertyName = $true)] 35 | [ValidateNotNullOrEmpty()] 36 | [Alias('Bytes')] 37 | [Byte[]] 38 | $ByteArray, 39 | 40 | [Switch] 41 | $Compress 42 | ) 43 | 44 | begin { 45 | $userErrorActionPreference = $ErrorActionPreference 46 | $nullPtr = [IntPtr]::Zero 47 | } 48 | 49 | process { 50 | $ptr = $nullPtr 51 | $pinnedArray = $null 52 | 53 | try { 54 | if ($Compress) { 55 | # Compression path uses .NET GzipStream (compression not yet migrated to Rust) 56 | [System.IO.MemoryStream] $output = [System.IO.MemoryStream]::new() 57 | $gzipStream = [System.IO.Compression.GzipStream]::new($output, ([IO.Compression.CompressionMode]::Compress)) 58 | $gzipStream.Write( $ByteArray, 0, $ByteArray.Length ) 59 | $gzipStream.Close() 60 | $output.Close() 61 | 62 | [System.Convert]::ToBase64String($output.ToArray()) 63 | } else { 64 | # Direct Base64 encoding via Rust for improved performance 65 | 66 | # Pin the byte array in memory to prevent garbage collection during FFI call 67 | $pinnedArray = [System.Runtime.InteropServices.GCHandle]::Alloc($ByteArray, [System.Runtime.InteropServices.GCHandleType]::Pinned) 68 | $bytePtr = $pinnedArray.AddrOfPinnedObject() 69 | 70 | # Call Rust bytes_to_base64 function 71 | $ptr = [ConvertCoreInterop]::bytes_to_base64($bytePtr, [UIntPtr]::new($ByteArray.Length)) 72 | 73 | # Check for errors (null pointer indicates failure) 74 | if ($ptr -eq $nullPtr) { 75 | $errorMsg = GetRustError -DefaultMessage "Failed to encode byte array to Base64" 76 | throw $errorMsg 77 | } 78 | 79 | # Convert C string pointer to PowerShell string 80 | ConvertPtrToString -Ptr $ptr 81 | } 82 | } catch { 83 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 84 | } finally { 85 | # Clean up: free Rust-allocated string memory 86 | if ($ptr -ne $nullPtr) { 87 | [ConvertCoreInterop]::free_string($ptr) 88 | } 89 | 90 | # Clean up: unpin the byte array to allow garbage collection 91 | if ($null -ne $pinnedArray) { 92 | $pinnedArray.Free() 93 | } 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-Base64.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-Base64/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-Base64 9 | 10 | ## SYNOPSIS 11 | Converts a base64 encoded string to a string. 12 | 13 | ## SYNTAX 14 | 15 | ### Default (Default) 16 | ``` 17 | ConvertFrom-Base64 -Base64 [-ToString] [-Decompress] [-ProgressAction ] 18 | [] 19 | ``` 20 | 21 | ### ToString 22 | ``` 23 | ConvertFrom-Base64 -Base64 [-Encoding ] [-ToString] [-Decompress] 24 | [-ProgressAction ] [] 25 | ``` 26 | 27 | ## DESCRIPTION 28 | Converts a base64 encoded string to a string. 29 | 30 | ## EXAMPLES 31 | 32 | ### EXAMPLE 1 33 | ``` 34 | ConvertFrom-Base64 -Base64 'QSBzdHJpbmc=' -ToString 35 | ``` 36 | 37 | A string 38 | 39 | ### EXAMPLE 2 40 | ``` 41 | ConvertTo-Base64 -Base64 'A string','Another string' -ToString 42 | ``` 43 | 44 | QSBzdHJpbmc= 45 | QW5vdGhlciBzdHJpbmc= 46 | 47 | ### EXAMPLE 3 48 | ``` 49 | 'QSBzdHJpbmc=' | ConvertFrom-Base64 -ToString 50 | ``` 51 | 52 | A string 53 | 54 | ### EXAMPLE 4 55 | ``` 56 | 'QSBzdHJpbmc=','QW5vdGhlciBzdHJpbmc=' | ConvertFrom-Base64 -ToString 57 | ``` 58 | 59 | A string 60 | Another string 61 | 62 | ## PARAMETERS 63 | 64 | ### -Base64 65 | A Base64 Encoded String. 66 | 67 | ```yaml 68 | Type: String[] 69 | Parameter Sets: (All) 70 | Aliases: String, Base64String 71 | 72 | Required: True 73 | Position: Named 74 | Default value: None 75 | Accept pipeline input: True (ByPropertyName, ByValue) 76 | Accept wildcard characters: False 77 | ``` 78 | 79 | ### -Encoding 80 | The encoding to use for conversion. 81 | Defaults to UTF8. 82 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 83 | 84 | ```yaml 85 | Type: String 86 | Parameter Sets: ToString 87 | Aliases: 88 | 89 | Required: False 90 | Position: Named 91 | Default value: UTF8 92 | Accept pipeline input: False 93 | Accept wildcard characters: False 94 | ``` 95 | 96 | ### -ToString 97 | Switch parameter to specify a conversion to a string object. 98 | 99 | ```yaml 100 | Type: SwitchParameter 101 | Parameter Sets: (All) 102 | Aliases: 103 | 104 | Required: False 105 | Position: Named 106 | Default value: False 107 | Accept pipeline input: False 108 | Accept wildcard characters: False 109 | ``` 110 | 111 | ### -Decompress 112 | If supplied, the output will be decompressed using Gzip. 113 | 114 | ```yaml 115 | Type: SwitchParameter 116 | Parameter Sets: (All) 117 | Aliases: 118 | 119 | Required: False 120 | Position: Named 121 | Default value: False 122 | Accept pipeline input: False 123 | Accept wildcard characters: False 124 | ``` 125 | 126 | ### -ProgressAction 127 | {{ Fill ProgressAction Description }} 128 | 129 | ```yaml 130 | Type: ActionPreference 131 | Parameter Sets: (All) 132 | Aliases: proga 133 | 134 | Required: False 135 | Position: Named 136 | Default value: None 137 | Accept pipeline input: False 138 | Accept wildcard characters: False 139 | ``` 140 | 141 | ### CommonParameters 142 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 143 | 144 | ## INPUTS 145 | 146 | ## OUTPUTS 147 | 148 | ### [String[]] 149 | ## NOTES 150 | 151 | ## RELATED LINKS 152 | 153 | [https://austoonz.github.io/Convert/functions/ConvertFrom-Base64/](https://austoonz.github.io/Convert/functions/ConvertFrom-Base64/) 154 | 155 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-StringToBase64.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-StringToBase64/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-StringToBase64 9 | 10 | ## SYNOPSIS 11 | Converts a string to a base64 encoded string. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertFrom-StringToBase64 [-String] [[-Encoding] ] [-Compress] 17 | [-ProgressAction ] [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts a string to a base64 encoded string. 22 | 23 | ## EXAMPLES 24 | 25 | ### EXAMPLE 1 26 | ``` 27 | ConvertFrom-StringToBase64 -String 'A string' 28 | QSBzdHJpbmc= 29 | ``` 30 | 31 | ### EXAMPLE 2 32 | ``` 33 | 'A string' | ConvertFrom-StringToBase64 34 | QSBzdHJpbmc= 35 | ``` 36 | 37 | ### EXAMPLE 3 38 | ``` 39 | ConvertFrom-StringToBase64 -String 'A string' -Encoding Unicode 40 | QQAgAHMAdAByAGkAbgBnAA== 41 | ``` 42 | 43 | ### EXAMPLE 4 44 | ``` 45 | 'A string' | ConvertFrom-StringToBase64 -Encoding Unicode 46 | QQAgAHMAdAByAGkAbgBnAA== 47 | ``` 48 | 49 | ### EXAMPLE 5 50 | ``` 51 | ConvertFrom-StringToBase64 -String 'A string','Another string' 52 | QSBzdHJpbmc= 53 | QW5vdGhlciBzdHJpbmc= 54 | ``` 55 | 56 | ### EXAMPLE 6 57 | ``` 58 | 'A string','Another string' | ConvertFrom-StringToBase64 59 | QSBzdHJpbmc= 60 | QW5vdGhlciBzdHJpbmc= 61 | ``` 62 | 63 | ### EXAMPLE 7 64 | ``` 65 | ConvertFrom-StringToBase64 -String 'A string','Another string' -Encoding Unicode 66 | QQAgAHMAdAByAGkAbgBnAA== 67 | QQBuAG8AdABoAGUAcgAgAHMAdAByAGkAbgBnAA== 68 | ``` 69 | 70 | ### EXAMPLE 8 71 | ``` 72 | 'A string','Another string' | ConvertFrom-StringToBase64 -Encoding Unicode 73 | QQAgAHMAdAByAGkAbgBnAA== 74 | QQBuAG8AdABoAGUAcgAgAHMAdAByAGkAbgBnAA== 75 | ``` 76 | 77 | ## PARAMETERS 78 | 79 | ### -String 80 | A string object for conversion. 81 | 82 | ```yaml 83 | Type: String[] 84 | Parameter Sets: (All) 85 | Aliases: 86 | 87 | Required: True 88 | Position: 1 89 | Default value: None 90 | Accept pipeline input: True (ByPropertyName, ByValue) 91 | Accept wildcard characters: False 92 | ``` 93 | 94 | ### -Encoding 95 | The encoding to use for conversion. 96 | Defaults to UTF8. 97 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 98 | 99 | ```yaml 100 | Type: String 101 | Parameter Sets: (All) 102 | Aliases: 103 | 104 | Required: False 105 | Position: 2 106 | Default value: UTF8 107 | Accept pipeline input: False 108 | Accept wildcard characters: False 109 | ``` 110 | 111 | ### -Compress 112 | If supplied, the output will be compressed using Gzip. 113 | 114 | ```yaml 115 | Type: SwitchParameter 116 | Parameter Sets: (All) 117 | Aliases: 118 | 119 | Required: False 120 | Position: Named 121 | Default value: False 122 | Accept pipeline input: False 123 | Accept wildcard characters: False 124 | ``` 125 | 126 | ### -ProgressAction 127 | {{ Fill ProgressAction Description }} 128 | 129 | ```yaml 130 | Type: ActionPreference 131 | Parameter Sets: (All) 132 | Aliases: proga 133 | 134 | Required: False 135 | Position: Named 136 | Default value: None 137 | Accept pipeline input: False 138 | Accept wildcard characters: False 139 | ``` 140 | 141 | ### CommonParameters 142 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 143 | 144 | ## INPUTS 145 | 146 | ## OUTPUTS 147 | 148 | ### [String[]] 149 | ## NOTES 150 | 151 | ## RELATED LINKS 152 | 153 | [https://austoonz.github.io/Convert/functions/ConvertFrom-StringToBase64/](https://austoonz.github.io/Convert/functions/ConvertFrom-StringToBase64/) 154 | 155 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-MemoryStreamToByteArray.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts MemoryStream to a byte array. 4 | 5 | .DESCRIPTION 6 | Converts MemoryStream to a byte array. 7 | 8 | .PARAMETER MemoryStream 9 | A System.IO.MemoryStream object for conversion. 10 | 11 | .PARAMETER Stream 12 | A System.IO.Stream object for conversion. 13 | 14 | .EXAMPLE 15 | $string = 'A string' 16 | $stream = [System.IO.MemoryStream]::new() 17 | $writer = [System.IO.StreamWriter]::new($stream) 18 | $writer.Write($string) 19 | $writer.Flush() 20 | 21 | ConvertFrom-MemoryStreamToByteArray -MemoryStream $stream 22 | 23 | .EXAMPLE 24 | $string = 'A string' 25 | $stream = [System.IO.MemoryStream]::new() 26 | $writer = [System.IO.StreamWriter]::new($stream) 27 | $writer.Write($string) 28 | $writer.Flush() 29 | 30 | $stream | ConvertFrom-MemoryStreamToByteArray 31 | 32 | .EXAMPLE 33 | $string1 = 'A string' 34 | $stream1 = [System.IO.MemoryStream]::new() 35 | $writer1 = [System.IO.StreamWriter]::new($stream1) 36 | $writer1.Write($string1) 37 | $writer1.Flush() 38 | 39 | $string2 = 'Another string' 40 | $stream2 = [System.IO.MemoryStream]::new() 41 | $writer2 = [System.IO.StreamWriter]::new($stream2) 42 | $writer2.Write($string2) 43 | $writer2.Flush() 44 | 45 | ConvertFrom-MemoryStreamToByteArray -MemoryStream $stream1,$stream2 46 | 47 | .EXAMPLE 48 | $string1 = 'A string' 49 | $stream1 = [System.IO.MemoryStream]::new() 50 | $writer1 = [System.IO.StreamWriter]::new($stream1) 51 | $writer1.Write($string1) 52 | $writer1.Flush() 53 | 54 | $string2 = 'Another string' 55 | $stream2 = [System.IO.MemoryStream]::new() 56 | $writer2 = [System.IO.StreamWriter]::new($stream2) 57 | $writer2.Write($string2) 58 | $writer2.Flush() 59 | 60 | $stream1,$stream2 | ConvertFrom-MemoryStreamToByteArray 61 | 62 | .OUTPUTS 63 | [Byte[]] 64 | 65 | .LINK 66 | https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToByteArray/ 67 | #> 68 | function ConvertFrom-MemoryStreamToByteArray { 69 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToByteArray/')] 70 | param 71 | ( 72 | [Parameter( 73 | Mandatory = $true, 74 | ValueFromPipeline = $true, 75 | ValueFromPipelineByPropertyName = $true, 76 | ParameterSetName = 'MemoryStream')] 77 | [ValidateNotNullOrEmpty()] 78 | [System.IO.MemoryStream[]] 79 | $MemoryStream, 80 | 81 | [Parameter( 82 | Mandatory = $true, 83 | ValueFromPipelineByPropertyName = $true, 84 | ParameterSetName = 'Stream')] 85 | [ValidateNotNullOrEmpty()] 86 | [System.IO.Stream[]] 87 | $Stream 88 | ) 89 | 90 | begin { 91 | $userErrorActionPreference = $ErrorActionPreference 92 | } 93 | 94 | process { 95 | switch ($PSCmdlet.ParameterSetName) { 96 | 'MemoryStream' { 97 | $inputObject = $MemoryStream 98 | } 99 | 'Stream' { 100 | $inputObject = $Stream 101 | } 102 | } 103 | 104 | foreach ($object in $inputObject) { 105 | try { 106 | if ($PSCmdlet.ParameterSetName -eq 'MemoryStream') { 107 | $object.Position = 0 108 | } 109 | $object.ToArray() 110 | } catch { 111 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 112 | } 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-MemoryStreamToByteArray.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToByteArray/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-MemoryStreamToByteArray 9 | 10 | ## SYNOPSIS 11 | Converts MemoryStream to a byte array. 12 | 13 | ## SYNTAX 14 | 15 | ### MemoryStream 16 | ``` 17 | ConvertFrom-MemoryStreamToByteArray -MemoryStream [-ProgressAction ] 18 | [] 19 | ``` 20 | 21 | ### Stream 22 | ``` 23 | ConvertFrom-MemoryStreamToByteArray -Stream [-ProgressAction ] 24 | [] 25 | ``` 26 | 27 | ## DESCRIPTION 28 | Converts MemoryStream to a byte array. 29 | 30 | ## EXAMPLES 31 | 32 | ### EXAMPLE 1 33 | ``` 34 | $string = 'A string' 35 | $stream = [System.IO.MemoryStream]::new() 36 | $writer = [System.IO.StreamWriter]::new($stream) 37 | $writer.Write($string) 38 | $writer.Flush() 39 | ``` 40 | 41 | ConvertFrom-MemoryStreamToByteArray -MemoryStream $stream 42 | 43 | ### EXAMPLE 2 44 | ``` 45 | $string = 'A string' 46 | $stream = [System.IO.MemoryStream]::new() 47 | $writer = [System.IO.StreamWriter]::new($stream) 48 | $writer.Write($string) 49 | $writer.Flush() 50 | ``` 51 | 52 | $stream | ConvertFrom-MemoryStreamToByteArray 53 | 54 | ### EXAMPLE 3 55 | ``` 56 | $string1 = 'A string' 57 | $stream1 = [System.IO.MemoryStream]::new() 58 | $writer1 = [System.IO.StreamWriter]::new($stream1) 59 | $writer1.Write($string1) 60 | $writer1.Flush() 61 | ``` 62 | 63 | $string2 = 'Another string' 64 | $stream2 = \[System.IO.MemoryStream\]::new() 65 | $writer2 = \[System.IO.StreamWriter\]::new($stream2) 66 | $writer2.Write($string2) 67 | $writer2.Flush() 68 | 69 | ConvertFrom-MemoryStreamToByteArray -MemoryStream $stream1,$stream2 70 | 71 | ### EXAMPLE 4 72 | ``` 73 | $string1 = 'A string' 74 | $stream1 = [System.IO.MemoryStream]::new() 75 | $writer1 = [System.IO.StreamWriter]::new($stream1) 76 | $writer1.Write($string1) 77 | $writer1.Flush() 78 | ``` 79 | 80 | $string2 = 'Another string' 81 | $stream2 = \[System.IO.MemoryStream\]::new() 82 | $writer2 = \[System.IO.StreamWriter\]::new($stream2) 83 | $writer2.Write($string2) 84 | $writer2.Flush() 85 | 86 | $stream1,$stream2 | ConvertFrom-MemoryStreamToByteArray 87 | 88 | ## PARAMETERS 89 | 90 | ### -MemoryStream 91 | A System.IO.MemoryStream object for conversion. 92 | 93 | ```yaml 94 | Type: MemoryStream[] 95 | Parameter Sets: MemoryStream 96 | Aliases: 97 | 98 | Required: True 99 | Position: Named 100 | Default value: None 101 | Accept pipeline input: True (ByPropertyName, ByValue) 102 | Accept wildcard characters: False 103 | ``` 104 | 105 | ### -Stream 106 | A System.IO.Stream object for conversion. 107 | 108 | ```yaml 109 | Type: Stream[] 110 | Parameter Sets: Stream 111 | Aliases: 112 | 113 | Required: True 114 | Position: Named 115 | Default value: None 116 | Accept pipeline input: True (ByPropertyName) 117 | Accept wildcard characters: False 118 | ``` 119 | 120 | ### -ProgressAction 121 | {{ Fill ProgressAction Description }} 122 | 123 | ```yaml 124 | Type: ActionPreference 125 | Parameter Sets: (All) 126 | Aliases: proga 127 | 128 | Required: False 129 | Position: Named 130 | Default value: None 131 | Accept pipeline input: False 132 | Accept wildcard characters: False 133 | ``` 134 | 135 | ### CommonParameters 136 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 137 | 138 | ## INPUTS 139 | 140 | ## OUTPUTS 141 | 142 | ### [Byte[]] 143 | ## NOTES 144 | 145 | ## RELATED LINKS 146 | 147 | [https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToByteArray/](https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToByteArray/) 148 | 149 | -------------------------------------------------------------------------------- /docs/functions/ConvertFrom-MemoryStreamToString.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToString/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertFrom-MemoryStreamToString 9 | 10 | ## SYNOPSIS 11 | Converts MemoryStream to a string. 12 | 13 | ## SYNTAX 14 | 15 | ### MemoryStream 16 | ``` 17 | ConvertFrom-MemoryStreamToString -MemoryStream [-ProgressAction ] 18 | [] 19 | ``` 20 | 21 | ### Stream 22 | ``` 23 | ConvertFrom-MemoryStreamToString -Stream [-ProgressAction ] [] 24 | ``` 25 | 26 | ## DESCRIPTION 27 | Converts MemoryStream to a string. 28 | 29 | ## EXAMPLES 30 | 31 | ### EXAMPLE 1 32 | ``` 33 | $string = 'A string' 34 | $stream = [System.IO.MemoryStream]::new() 35 | $writer = [System.IO.StreamWriter]::new($stream) 36 | $writer.Write($string) 37 | $writer.Flush() 38 | ``` 39 | 40 | ConvertFrom-MemoryStreamToString -MemoryStream $stream 41 | 42 | A string 43 | 44 | ### EXAMPLE 2 45 | ``` 46 | $string = 'A string' 47 | $stream = [System.IO.MemoryStream]::new() 48 | $writer = [System.IO.StreamWriter]::new($stream) 49 | $writer.Write($string) 50 | $writer.Flush() 51 | ``` 52 | 53 | $stream | ConvertFrom-MemoryStreamToString 54 | 55 | A string 56 | 57 | ### EXAMPLE 3 58 | ``` 59 | $string1 = 'A string' 60 | $stream1 = [System.IO.MemoryStream]::new() 61 | $writer1 = [System.IO.StreamWriter]::new($stream1) 62 | $writer1.Write($string1) 63 | $writer1.Flush() 64 | ``` 65 | 66 | $string2 = 'Another string' 67 | $stream2 = \[System.IO.MemoryStream\]::new() 68 | $writer2 = \[System.IO.StreamWriter\]::new($stream2) 69 | $writer2.Write($string2) 70 | $writer2.Flush() 71 | 72 | ConvertFrom-MemoryStreamToString -MemoryStream $stream1,$stream2 73 | 74 | A string 75 | Another string 76 | 77 | ### EXAMPLE 4 78 | ``` 79 | $string1 = 'A string' 80 | $stream1 = [System.IO.MemoryStream]::new() 81 | $writer1 = [System.IO.StreamWriter]::new($stream1) 82 | $writer1.Write($string1) 83 | $writer1.Flush() 84 | ``` 85 | 86 | $string2 = 'Another string' 87 | $stream2 = \[System.IO.MemoryStream\]::new() 88 | $writer2 = \[System.IO.StreamWriter\]::new($stream2) 89 | $writer2.Write($string2) 90 | $writer2.Flush() 91 | 92 | $stream1,$stream2 | ConvertFrom-MemoryStreamToString 93 | 94 | A string 95 | Another string 96 | 97 | ## PARAMETERS 98 | 99 | ### -MemoryStream 100 | A System.IO.MemoryStream object for conversion. 101 | 102 | ```yaml 103 | Type: MemoryStream[] 104 | Parameter Sets: MemoryStream 105 | Aliases: 106 | 107 | Required: True 108 | Position: Named 109 | Default value: None 110 | Accept pipeline input: True (ByPropertyName, ByValue) 111 | Accept wildcard characters: False 112 | ``` 113 | 114 | ### -Stream 115 | A System.IO.Stream object for conversion. 116 | 117 | ```yaml 118 | Type: Stream[] 119 | Parameter Sets: Stream 120 | Aliases: 121 | 122 | Required: True 123 | Position: Named 124 | Default value: None 125 | Accept pipeline input: True (ByPropertyName) 126 | Accept wildcard characters: False 127 | ``` 128 | 129 | ### -ProgressAction 130 | {{ Fill ProgressAction Description }} 131 | 132 | ```yaml 133 | Type: ActionPreference 134 | Parameter Sets: (All) 135 | Aliases: proga 136 | 137 | Required: False 138 | Position: Named 139 | Default value: None 140 | Accept pipeline input: False 141 | Accept wildcard characters: False 142 | ``` 143 | 144 | ### CommonParameters 145 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 146 | 147 | ## INPUTS 148 | 149 | ## OUTPUTS 150 | 151 | ### [String[]] 152 | ## NOTES 153 | 154 | ## RELATED LINKS 155 | 156 | [https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToString/](https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToString/) 157 | 158 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-StringToByteArray.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a string to a byte array object. 4 | 5 | .DESCRIPTION 6 | Converts a string to a byte array object. 7 | 8 | .PARAMETER String 9 | A string object for conversion. 10 | 11 | .PARAMETER Encoding 12 | The encoding to use for conversion. 13 | Defaults to UTF8. 14 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 15 | 16 | .EXAMPLE 17 | $bytes = ConvertFrom-StringToByteArray -String 'A string' 18 | $bytes.GetType() 19 | 20 | IsPublic IsSerial Name BaseType 21 | -------- -------- ---- -------- 22 | True True Byte[] System.Array 23 | 24 | $bytes[0].GetType() 25 | 26 | IsPublic IsSerial Name BaseType 27 | -------- -------- ---- -------- 28 | True True Byte System.ValueType 29 | 30 | .EXAMPLE 31 | $bytes = 'A string','Another string' | ConvertFrom-StringToByteArray 32 | 33 | $bytes.Count 34 | 2 35 | 36 | $bytes.GetType() 37 | 38 | IsPublic IsSerial Name BaseType 39 | -------- -------- ---- -------- 40 | True True Object[] System.Array 41 | 42 | $bytes[0].GetType() 43 | 44 | IsPublic IsSerial Name BaseType 45 | -------- -------- ---- -------- 46 | True True Byte[] System.Array 47 | 48 | .OUTPUTS 49 | [System.Collections.Generic.List[Byte[]]] 50 | 51 | .LINK 52 | https://austoonz.github.io/Convert/functions/ConvertFrom-StringToByteArray/ 53 | #> 54 | function ConvertFrom-StringToByteArray { 55 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-StringToByteArray/')] 56 | param 57 | ( 58 | [Parameter( 59 | Mandatory = $true, 60 | ValueFromPipeline = $true, 61 | ValueFromPipelineByPropertyName = $true)] 62 | [ValidateNotNullOrEmpty()] 63 | [String[]] 64 | $String, 65 | 66 | [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF8')] 67 | [String] 68 | $Encoding = 'UTF8' 69 | ) 70 | 71 | begin { 72 | $userErrorActionPreference = $ErrorActionPreference 73 | } 74 | 75 | process { 76 | foreach ($s in $String) { 77 | # Creating a generic list to ensure an array of string being handed in 78 | # outputs an array of Byte arrays, rather than a single array with both 79 | # Byte arrays merged. 80 | $byteArrayObject = [System.Collections.Generic.List[Byte[]]]::new() 81 | 82 | $ptr = [IntPtr]::Zero 83 | try { 84 | $length = [UIntPtr]::Zero 85 | $ptr = [ConvertCoreInterop]::string_to_bytes($s, $Encoding, [ref]$length) 86 | 87 | if ($ptr -eq [IntPtr]::Zero) { 88 | $errorMsg = GetRustError -DefaultMessage "String to byte array conversion failed for encoding '$Encoding'" 89 | throw $errorMsg 90 | } 91 | 92 | $byteArray = New-Object byte[] $length.ToUInt64() 93 | [System.Runtime.InteropServices.Marshal]::Copy($ptr, $byteArray, 0, $byteArray.Length) 94 | 95 | $null = $byteArrayObject.Add($byteArray) 96 | $byteArrayObject 97 | } catch { 98 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 99 | } finally { 100 | if ($ptr -ne [IntPtr]::Zero) { 101 | [ConvertCoreInterop]::free_bytes($ptr) 102 | } 103 | } 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-MemoryStreamToString.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts MemoryStream to a string. 4 | 5 | .DESCRIPTION 6 | Converts MemoryStream to a string. 7 | 8 | .PARAMETER MemoryStream 9 | A System.IO.MemoryStream object for conversion. 10 | 11 | .PARAMETER Stream 12 | A System.IO.Stream object for conversion. 13 | 14 | .EXAMPLE 15 | $string = 'A string' 16 | $stream = [System.IO.MemoryStream]::new() 17 | $writer = [System.IO.StreamWriter]::new($stream) 18 | $writer.Write($string) 19 | $writer.Flush() 20 | 21 | ConvertFrom-MemoryStreamToString -MemoryStream $stream 22 | 23 | A string 24 | 25 | .EXAMPLE 26 | $string = 'A string' 27 | $stream = [System.IO.MemoryStream]::new() 28 | $writer = [System.IO.StreamWriter]::new($stream) 29 | $writer.Write($string) 30 | $writer.Flush() 31 | 32 | $stream | ConvertFrom-MemoryStreamToString 33 | 34 | A string 35 | 36 | .EXAMPLE 37 | $string1 = 'A string' 38 | $stream1 = [System.IO.MemoryStream]::new() 39 | $writer1 = [System.IO.StreamWriter]::new($stream1) 40 | $writer1.Write($string1) 41 | $writer1.Flush() 42 | 43 | $string2 = 'Another string' 44 | $stream2 = [System.IO.MemoryStream]::new() 45 | $writer2 = [System.IO.StreamWriter]::new($stream2) 46 | $writer2.Write($string2) 47 | $writer2.Flush() 48 | 49 | ConvertFrom-MemoryStreamToString -MemoryStream $stream1,$stream2 50 | 51 | A string 52 | Another string 53 | 54 | .EXAMPLE 55 | $string1 = 'A string' 56 | $stream1 = [System.IO.MemoryStream]::new() 57 | $writer1 = [System.IO.StreamWriter]::new($stream1) 58 | $writer1.Write($string1) 59 | $writer1.Flush() 60 | 61 | $string2 = 'Another string' 62 | $stream2 = [System.IO.MemoryStream]::new() 63 | $writer2 = [System.IO.StreamWriter]::new($stream2) 64 | $writer2.Write($string2) 65 | $writer2.Flush() 66 | 67 | $stream1,$stream2 | ConvertFrom-MemoryStreamToString 68 | 69 | A string 70 | Another string 71 | 72 | .OUTPUTS 73 | [String[]] 74 | 75 | .LINK 76 | https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToString/ 77 | #> 78 | function ConvertFrom-MemoryStreamToString { 79 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-MemoryStreamToString/')] 80 | [Alias('ConvertFrom-StreamToString')] 81 | param 82 | ( 83 | [Parameter( 84 | Mandatory = $true, 85 | ValueFromPipeline = $true, 86 | ValueFromPipelineByPropertyName = $true, 87 | ParameterSetName = 'MemoryStream')] 88 | [ValidateNotNullOrEmpty()] 89 | [System.IO.MemoryStream[]] 90 | $MemoryStream, 91 | 92 | [Parameter( 93 | Mandatory = $true, 94 | ValueFromPipelineByPropertyName = $true, 95 | ParameterSetName = 'Stream')] 96 | [ValidateNotNullOrEmpty()] 97 | [System.IO.Stream[]] 98 | $Stream 99 | ) 100 | 101 | begin { 102 | $userErrorActionPreference = $ErrorActionPreference 103 | } 104 | 105 | process { 106 | switch ($PSCmdlet.ParameterSetName) { 107 | 'MemoryStream' { 108 | $inputObject = $MemoryStream 109 | } 110 | 'Stream' { 111 | $inputObject = $Stream 112 | } 113 | } 114 | 115 | foreach ($object in $inputObject) { 116 | try { 117 | if ($PSCmdlet.ParameterSetName -eq 'MemoryStream') { 118 | $object.Position = 0 119 | } 120 | $reader = [System.IO.StreamReader]::new($object) 121 | $reader.ReadToEnd() 122 | } catch { 123 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 124 | } finally { 125 | if ($reader) { 126 | #$reader.Dispose() 127 | } 128 | } 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertFrom-MemoryStreamToString.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe -Name $function -Fixture { 4 | 5 | Context -Name 'Input/Output' -Fixture { 6 | It -Name "Converts a MemoryStream to a string" -Test { 7 | $string = 'ThisIsMyString' 8 | 9 | $stream = [System.IO.MemoryStream]::new() 10 | $writer = [System.IO.StreamWriter]::new($stream) 11 | $writer.Write($string) 12 | $writer.Flush() 13 | 14 | $assertion = ConvertFrom-MemoryStreamToString -MemoryStream $stream 15 | $assertion | Should -BeExactly $string 16 | 17 | $stream.Dispose() 18 | $writer.Dispose() 19 | } 20 | } 21 | 22 | Context -Name 'Pipeline' -Fixture { 23 | It -Name 'Supports the Pipeline' -Test { 24 | $string = 'ThisIsMyString' 25 | 26 | $stream = [System.IO.MemoryStream]::new() 27 | $writer = [System.IO.StreamWriter]::new($stream) 28 | $writer.Write($string) 29 | $writer.Flush() 30 | 31 | $assertion = $stream | ConvertFrom-MemoryStreamToString 32 | $assertion | Should -BeExactly $string 33 | 34 | $stream.Dispose() 35 | $writer.Dispose() 36 | } 37 | 38 | It -Name 'Supports the Pipeline with array input' -Test { 39 | $string = 'ThisIsMyString' 40 | 41 | $stream = [System.IO.MemoryStream]::new() 42 | $writer = [System.IO.StreamWriter]::new($stream) 43 | $writer.Write($string) 44 | $writer.Flush() 45 | 46 | $stream2 = [System.IO.MemoryStream]::new() 47 | $writer2 = [System.IO.StreamWriter]::new($stream2) 48 | $writer2.Write($string) 49 | $writer2.Flush() 50 | $stream2 51 | 52 | $assertion = @($stream, $stream2) | ConvertFrom-MemoryStreamToString 53 | $assertion | Should -HaveCount 2 54 | 55 | $stream.Dispose() 56 | $stream2.Dispose() 57 | $writer.Dispose() 58 | $writer2.Dispose() 59 | } 60 | } 61 | 62 | Context -Name 'EAP' -Fixture { 63 | It -Name 'Supports SilentlyContinue' -Test { 64 | $string = 'ThisIsMyString' 65 | 66 | $stream = [System.IO.MemoryStream]::new() 67 | $writer = [System.IO.StreamWriter]::new($stream) 68 | $writer.Write($string) 69 | $writer.Flush() 70 | 71 | # Disposing the StreamWriter will cause the function call to throw with 'Stream was not readable.' 72 | $writer.Dispose() 73 | 74 | $assertion = ConvertFrom-MemoryStreamToString -MemoryStream $stream -ErrorAction SilentlyContinue 75 | $assertion | Should -BeNullOrEmpty 76 | } 77 | 78 | It -Name 'Supports Stop' -Test { 79 | $string = 'ThisIsMyString' 80 | 81 | $stream = [System.IO.MemoryStream]::new() 82 | $writer = [System.IO.StreamWriter]::new($stream) 83 | $writer.Write($string) 84 | $writer.Flush() 85 | 86 | # Disposing the StreamWriter will cause the function call to throw with either 'Cannot access a closed Stream.' or 'Stream was not readable.' 87 | $writer.Dispose() 88 | 89 | { ConvertFrom-MemoryStreamToString -MemoryStream $stream -ErrorAction Stop } | Should -Throw 90 | } 91 | 92 | It -Name 'Supports Continue' -Test { 93 | $string = 'ThisIsMyString' 94 | 95 | $stream = [System.IO.MemoryStream]::new() 96 | $writer = [System.IO.StreamWriter]::new($stream) 97 | $writer.Write($string) 98 | $writer.Flush() 99 | 100 | # Disposing the StreamWriter will cause the function call to throw with either 'Cannot access a closed Stream.' or 'Stream was not readable.' 101 | $writer.Dispose() 102 | 103 | $assertion = ConvertFrom-MemoryStreamToString -MemoryStream $stream -ErrorAction Continue 2>&1 104 | $assertion.Exception.InnerException.Message | Should -BeIn @('Cannot access a closed Stream.', 'Stream was not readable.') 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/Tests/Unit/ConvertFrom-MemoryStreamToBase64.Tests.ps1: -------------------------------------------------------------------------------- 1 | $function = $MyInvocation.MyCommand.Name.Split('.')[0] 2 | 3 | Describe -Name $function -Fixture { 4 | BeforeEach { 5 | $String = 'ThisIsMyString' 6 | 7 | # Use the variables so IDe does not complain 8 | $null = $String 9 | } 10 | 11 | Context -Name 'Input/Output' -Fixture { 12 | It -Name 'Converts correctly' -Test { 13 | $string = 'ThisIsMyString' 14 | 15 | $stream = [System.IO.MemoryStream]::new() 16 | $writer = [System.IO.StreamWriter]::new($stream) 17 | $writer.Write($string) 18 | $writer.Flush() 19 | 20 | $assertion = ConvertFrom-MemoryStreamToBase64 -MemoryStream $stream 21 | $assertion | Should -BeExactly 'VGhpc0lzTXlTdHJpbmc=' 22 | 23 | $stream.Dispose() 24 | $writer.Dispose() 25 | } 26 | } 27 | 28 | Context -Name 'Pipeline' -Fixture { 29 | It -Name 'Supports the Pipeline' -Test { 30 | $string = 'ThisIsMyString' 31 | 32 | $stream = [System.IO.MemoryStream]::new() 33 | $writer = [System.IO.StreamWriter]::new($stream) 34 | $writer.Write($string) 35 | $writer.Flush() 36 | 37 | $assertion = $stream | ConvertFrom-MemoryStreamToBase64 38 | $assertion | Should -BeExactly 'VGhpc0lzTXlTdHJpbmc=' 39 | 40 | $stream.Dispose() 41 | $writer.Dispose() 42 | } 43 | 44 | It -Name 'Supports the Pipeline with array input' -Test { 45 | $string = 'ThisIsMyString' 46 | 47 | $stream = [System.IO.MemoryStream]::new() 48 | $writer = [System.IO.StreamWriter]::new($stream) 49 | $writer.Write($string) 50 | $writer.Flush() 51 | 52 | $stream2 = [System.IO.MemoryStream]::new() 53 | $writer2 = [System.IO.StreamWriter]::new($stream2) 54 | $writer2.Write($string) 55 | $writer2.Flush() 56 | $stream2 57 | 58 | $assertion = @($stream, $stream2) | ConvertFrom-MemoryStreamToString 59 | $assertion | Should -HaveCount 2 60 | 61 | $stream.Dispose() 62 | $stream2.Dispose() 63 | $writer.Dispose() 64 | $writer2.Dispose() 65 | } 66 | } 67 | 68 | Context -Name 'EAP' -Fixture { 69 | It -Name 'Supports SilentlyContinue' -Test { 70 | $string = 'ThisIsMyString' 71 | 72 | $stream = [System.IO.MemoryStream]::new() 73 | $writer = [System.IO.StreamWriter]::new($stream) 74 | $writer.Write($string) 75 | $writer.Flush() 76 | 77 | # Disposing the StreamWriter will cause the function call to throw with 'Stream was not readable.' 78 | $writer.Dispose() 79 | 80 | $assertion = ConvertFrom-MemoryStreamToString -MemoryStream $stream -ErrorAction SilentlyContinue 81 | $assertion | Should -BeNullOrEmpty 82 | } 83 | 84 | It -Name 'Supports Stop' -Test { 85 | $string = 'ThisIsMyString' 86 | 87 | $stream = [System.IO.MemoryStream]::new() 88 | $writer = [System.IO.StreamWriter]::new($stream) 89 | $writer.Write($string) 90 | $writer.Flush() 91 | 92 | # Disposing the StreamWriter will cause the function call to throw with either 'Cannot access a closed Stream.' or 'Stream was not readable.' 93 | $writer.Dispose() 94 | 95 | { ConvertFrom-MemoryStreamToString -MemoryStream $stream -ErrorAction Stop } | Should -Throw 96 | } 97 | 98 | It -Name 'Supports Continue' -Test { 99 | $string = 'ThisIsMyString' 100 | 101 | $stream = [System.IO.MemoryStream]::new() 102 | $writer = [System.IO.StreamWriter]::new($stream) 103 | $writer.Write($string) 104 | $writer.Flush() 105 | 106 | # Disposing the StreamWriter will cause the function call to throw with either 'Cannot access a closed Stream.' or 'Stream was not readable.' 107 | $writer.Dispose() 108 | 109 | $assertion = ConvertFrom-MemoryStreamToString -MemoryStream $stream -ErrorAction Continue 2>&1 110 | $assertion.Exception.InnerException.Message | Should -BeIn @('Cannot access a closed Stream.', 'Stream was not readable.') 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /docs/functions/ConvertTo-MemoryStream.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: Convert-help.xml 3 | Module Name: Convert 4 | online version: https://austoonz.github.io/Convert/functions/ConvertTo-MemoryStream/ 5 | schema: 2.0.0 6 | --- 7 | 8 | # ConvertTo-MemoryStream 9 | 10 | ## SYNOPSIS 11 | Converts an object to a MemoryStream object. 12 | 13 | ## SYNTAX 14 | 15 | ``` 16 | ConvertTo-MemoryStream -String [-Encoding ] [-Compress] [-ProgressAction ] 17 | [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | Converts an object to a MemoryStream object. 22 | 23 | ## EXAMPLES 24 | 25 | ### EXAMPLE 1 26 | ``` 27 | $string = 'A string' 28 | $stream = ConvertTo-MemoryStream -String $string 29 | $stream.GetType() 30 | ``` 31 | 32 | IsPublic IsSerial Name BaseType 33 | -------- -------- ---- -------- 34 | True True MemoryStream System.IO.Stream 35 | 36 | ### EXAMPLE 2 37 | ``` 38 | $string = 'A string' 39 | $stream = $string | ConvertTo-MemoryStream 40 | $stream.GetType() 41 | ``` 42 | 43 | IsPublic IsSerial Name BaseType 44 | -------- -------- ---- -------- 45 | True True MemoryStream System.IO.Stream 46 | 47 | ### EXAMPLE 3 48 | ``` 49 | $string1 = 'A string' 50 | $string2 = 'Another string' 51 | ``` 52 | 53 | $streams = ConvertTo-MemoryStream -String $string1,$string2 54 | $streams.GetType() 55 | 56 | IsPublic IsSerial Name BaseType 57 | -------- -------- ---- -------- 58 | True True Object\[\] System.Array 59 | 60 | $streams\[0\].GetType() 61 | 62 | IsPublic IsSerial Name BaseType 63 | -------- -------- ---- -------- 64 | True True MemoryStream System.IO.Stream 65 | 66 | ### EXAMPLE 4 67 | ``` 68 | $string1 = 'A string' 69 | $string2 = 'Another string' 70 | ``` 71 | 72 | $streams = $string1,$string2 | ConvertTo-MemoryStream 73 | $streams.GetType() 74 | 75 | IsPublic IsSerial Name BaseType 76 | -------- -------- ---- -------- 77 | True True Object\[\] System.Array 78 | 79 | $streams\[0\].GetType() 80 | 81 | IsPublic IsSerial Name BaseType 82 | -------- -------- ---- -------- 83 | True True MemoryStream System.IO.Stream 84 | 85 | ## PARAMETERS 86 | 87 | ### -String 88 | A string object for conversion. 89 | 90 | ```yaml 91 | Type: String[] 92 | Parameter Sets: (All) 93 | Aliases: 94 | 95 | Required: True 96 | Position: Named 97 | Default value: None 98 | Accept pipeline input: True (ByPropertyName, ByValue) 99 | Accept wildcard characters: False 100 | ``` 101 | 102 | ### -Encoding 103 | The encoding to use for conversion. 104 | Defaults to UTF8. 105 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 106 | 107 | ```yaml 108 | Type: String 109 | Parameter Sets: (All) 110 | Aliases: 111 | 112 | Required: False 113 | Position: Named 114 | Default value: UTF8 115 | Accept pipeline input: False 116 | Accept wildcard characters: False 117 | ``` 118 | 119 | ### -Compress 120 | If supplied, the output will be compressed using Gzip. 121 | 122 | ```yaml 123 | Type: SwitchParameter 124 | Parameter Sets: (All) 125 | Aliases: 126 | 127 | Required: False 128 | Position: Named 129 | Default value: False 130 | Accept pipeline input: False 131 | Accept wildcard characters: False 132 | ``` 133 | 134 | ### -ProgressAction 135 | {{ Fill ProgressAction Description }} 136 | 137 | ```yaml 138 | Type: ActionPreference 139 | Parameter Sets: (All) 140 | Aliases: proga 141 | 142 | Required: False 143 | Position: Named 144 | Default value: None 145 | Accept pipeline input: False 146 | Accept wildcard characters: False 147 | ``` 148 | 149 | ### CommonParameters 150 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 151 | 152 | ## INPUTS 153 | 154 | ## OUTPUTS 155 | 156 | ### [System.IO.MemoryStream[]] 157 | ## NOTES 158 | 159 | ## RELATED LINKS 160 | 161 | [https://austoonz.github.io/Convert/functions/ConvertTo-MemoryStream/](https://austoonz.github.io/Convert/functions/ConvertTo-MemoryStream/) 162 | 163 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertTo-MemoryStream.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts an object to a MemoryStream object. 4 | 5 | .DESCRIPTION 6 | Converts an object to a MemoryStream object. 7 | 8 | .PARAMETER String 9 | A string object for conversion. 10 | 11 | .PARAMETER Encoding 12 | The encoding to use for conversion. 13 | Defaults to UTF8. 14 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 15 | 16 | .PARAMETER Compress 17 | If supplied, the output will be compressed using Gzip. 18 | 19 | .EXAMPLE 20 | $string = 'A string' 21 | $stream = ConvertTo-MemoryStream -String $string 22 | $stream.GetType() 23 | 24 | IsPublic IsSerial Name BaseType 25 | -------- -------- ---- -------- 26 | True True MemoryStream System.IO.Stream 27 | 28 | .EXAMPLE 29 | $string = 'A string' 30 | $stream = $string | ConvertTo-MemoryStream 31 | $stream.GetType() 32 | 33 | IsPublic IsSerial Name BaseType 34 | -------- -------- ---- -------- 35 | True True MemoryStream System.IO.Stream 36 | 37 | .EXAMPLE 38 | $string1 = 'A string' 39 | $string2 = 'Another string' 40 | 41 | $streams = ConvertTo-MemoryStream -String $string1,$string2 42 | $streams.GetType() 43 | 44 | IsPublic IsSerial Name BaseType 45 | -------- -------- ---- -------- 46 | True True Object[] System.Array 47 | 48 | $streams[0].GetType() 49 | 50 | IsPublic IsSerial Name BaseType 51 | -------- -------- ---- -------- 52 | True True MemoryStream System.IO.Stream 53 | 54 | .EXAMPLE 55 | $string1 = 'A string' 56 | $string2 = 'Another string' 57 | 58 | $streams = $string1,$string2 | ConvertTo-MemoryStream 59 | $streams.GetType() 60 | 61 | IsPublic IsSerial Name BaseType 62 | -------- -------- ---- -------- 63 | True True Object[] System.Array 64 | 65 | $streams[0].GetType() 66 | 67 | IsPublic IsSerial Name BaseType 68 | -------- -------- ---- -------- 69 | True True MemoryStream System.IO.Stream 70 | 71 | .OUTPUTS 72 | [System.IO.MemoryStream[]] 73 | 74 | .LINK 75 | https://austoonz.github.io/Convert/functions/ConvertTo-MemoryStream/ 76 | #> 77 | function ConvertTo-MemoryStream { 78 | [CmdletBinding( 79 | DefaultParameterSetName = 'String', 80 | HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertTo-MemoryStream/')] 81 | param 82 | ( 83 | [Parameter( 84 | Mandatory = $true, 85 | ValueFromPipeline = $true, 86 | ValueFromPipelineByPropertyName = $true, 87 | ParameterSetName = 'String')] 88 | [ValidateNotNullOrEmpty()] 89 | [String[]] 90 | $String, 91 | 92 | [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF8')] 93 | [String] 94 | $Encoding = 'UTF8', 95 | 96 | [Switch] 97 | $Compress 98 | ) 99 | 100 | begin { 101 | $userErrorActionPreference = $ErrorActionPreference 102 | $eaSplat = @{ 103 | ErrorAction = $userErrorActionPreference 104 | } 105 | } 106 | 107 | process { 108 | switch ($PSCmdlet.ParameterSetName) { 109 | 'String' { 110 | foreach ($s in $string) { 111 | $params = @{ 112 | String = $s 113 | Encoding = $Encoding 114 | } 115 | 116 | if ($Compress) { 117 | ConvertFrom-StringToMemoryStream @params -Compress @eaSplat 118 | } else { 119 | ConvertFrom-StringToMemoryStream @params @eaSplat 120 | } 121 | } 122 | break 123 | } 124 | 125 | default { 126 | Write-Error -Message 'Invalid ParameterSetName' @eaSplat 127 | break 128 | } 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-StringToMemoryStream.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a string to a MemoryStream object. 4 | 5 | .DESCRIPTION 6 | Converts a string to a MemoryStream object. 7 | 8 | .PARAMETER String 9 | A string object for conversion. 10 | 11 | .PARAMETER Encoding 12 | The encoding to use for conversion. 13 | Defaults to UTF8. 14 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 15 | 16 | .PARAMETER Compress 17 | If supplied, the output will be compressed using Gzip. 18 | 19 | .EXAMPLE 20 | $stream = ConvertFrom-StringToMemoryStream -String 'A string' 21 | $stream.GetType() 22 | 23 | IsPublic IsSerial Name BaseType 24 | -------- -------- ---- -------- 25 | True True MemoryStream System.IO.Stream 26 | 27 | .EXAMPLE 28 | $stream = 'A string' | ConvertFrom-StringToMemoryStream 29 | $stream.GetType() 30 | 31 | IsPublic IsSerial Name BaseType 32 | -------- -------- ---- -------- 33 | True True MemoryStream System.IO.Stream 34 | 35 | .EXAMPLE 36 | $streams = ConvertFrom-StringToMemoryStream -String 'A string','Another string' 37 | $streams.GetType() 38 | 39 | IsPublic IsSerial Name BaseType 40 | -------- -------- ---- -------- 41 | True True Object[] System.Array 42 | 43 | $streams[0].GetType() 44 | 45 | IsPublic IsSerial Name BaseType 46 | -------- -------- ---- -------- 47 | True True MemoryStream System.IO.Stream 48 | 49 | .EXAMPLE 50 | $streams = 'A string','Another string' | ConvertFrom-StringToMemoryStream 51 | $streams.GetType() 52 | 53 | IsPublic IsSerial Name BaseType 54 | -------- -------- ---- -------- 55 | True True Object[] System.Array 56 | 57 | $streams[0].GetType() 58 | 59 | IsPublic IsSerial Name BaseType 60 | -------- -------- ---- -------- 61 | True True MemoryStream System.IO.Stream 62 | 63 | .EXAMPLE 64 | $stream = ConvertFrom-StringToMemoryStream -String 'This string has two string values' 65 | $stream.Length 66 | 67 | 33 68 | 69 | $stream = ConvertFrom-StringToMemoryStream -String 'This string has two string values' -Compress 70 | $stream.Length 71 | 72 | 10 73 | 74 | .OUTPUTS 75 | [System.IO.MemoryStream[]] 76 | 77 | .LINK 78 | https://austoonz.github.io/Convert/functions/ConvertFrom-StringToMemoryStream/ 79 | #> 80 | function ConvertFrom-StringToMemoryStream { 81 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-StringToMemoryStream/')] 82 | param 83 | ( 84 | [Parameter( 85 | Mandatory = $true, 86 | ValueFromPipeline = $true, 87 | ValueFromPipelineByPropertyName = $true)] 88 | [ValidateNotNullOrEmpty()] 89 | [String[]] 90 | $String, 91 | 92 | [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF8')] 93 | [String] 94 | $Encoding = 'UTF8', 95 | 96 | [Switch] 97 | $Compress 98 | ) 99 | 100 | begin { 101 | $userErrorActionPreference = $ErrorActionPreference 102 | } 103 | 104 | process { 105 | foreach ($s in $String) { 106 | try { 107 | [System.IO.MemoryStream]$stream = [System.IO.MemoryStream]::new() 108 | if ($Compress) { 109 | $byteArray = [System.Text.Encoding]::$Encoding.GetBytes($s) 110 | $gzipStream = [System.IO.Compression.GzipStream]::new($stream, ([IO.Compression.CompressionMode]::Compress)) 111 | $gzipStream.Write( $byteArray, 0, $byteArray.Length ) 112 | } else { 113 | $writer = [System.IO.StreamWriter]::new($stream) 114 | $writer.Write($s) 115 | $writer.Flush() 116 | } 117 | $stream 118 | } catch { 119 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 120 | } 121 | } 122 | } 123 | } -------------------------------------------------------------------------------- /src/Convert/Public/ConvertFrom-StringToBase64.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Converts a string to a base64 encoded string. 4 | 5 | .DESCRIPTION 6 | Converts a string to a base64 encoded string. 7 | 8 | .PARAMETER String 9 | A string object for conversion. 10 | 11 | .PARAMETER Encoding 12 | The encoding to use for conversion. 13 | Defaults to UTF8. 14 | Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, and UTF8. 15 | 16 | .PARAMETER Compress 17 | If supplied, the output will be compressed using Gzip. 18 | 19 | .EXAMPLE 20 | ConvertFrom-StringToBase64 -String 'A string' 21 | QSBzdHJpbmc= 22 | 23 | .EXAMPLE 24 | 'A string' | ConvertFrom-StringToBase64 25 | QSBzdHJpbmc= 26 | 27 | .EXAMPLE 28 | ConvertFrom-StringToBase64 -String 'A string' -Encoding Unicode 29 | QQAgAHMAdAByAGkAbgBnAA== 30 | 31 | .EXAMPLE 32 | 'A string' | ConvertFrom-StringToBase64 -Encoding Unicode 33 | QQAgAHMAdAByAGkAbgBnAA== 34 | 35 | .EXAMPLE 36 | ConvertFrom-StringToBase64 -String 'A string','Another string' 37 | QSBzdHJpbmc= 38 | QW5vdGhlciBzdHJpbmc= 39 | 40 | .EXAMPLE 41 | 'A string','Another string' | ConvertFrom-StringToBase64 42 | QSBzdHJpbmc= 43 | QW5vdGhlciBzdHJpbmc= 44 | 45 | .EXAMPLE 46 | ConvertFrom-StringToBase64 -String 'A string','Another string' -Encoding Unicode 47 | QQAgAHMAdAByAGkAbgBnAA== 48 | QQBuAG8AdABoAGUAcgAgAHMAdAByAGkAbgBnAA== 49 | 50 | .EXAMPLE 51 | 'A string','Another string' | ConvertFrom-StringToBase64 -Encoding Unicode 52 | QQAgAHMAdAByAGkAbgBnAA== 53 | QQBuAG8AdABoAGUAcgAgAHMAdAByAGkAbgBnAA== 54 | 55 | .OUTPUTS 56 | [String[]] 57 | 58 | .LINK 59 | https://austoonz.github.io/Convert/functions/ConvertFrom-StringToBase64/ 60 | #> 61 | function ConvertFrom-StringToBase64 { 62 | [CmdletBinding(HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertFrom-StringToBase64/')] 63 | param 64 | ( 65 | [Parameter( 66 | Mandatory = $true, 67 | ValueFromPipeline = $true, 68 | ValueFromPipelineByPropertyName = $true)] 69 | [ValidateNotNullOrEmpty()] 70 | [String[]] 71 | $String, 72 | 73 | [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF8')] 74 | [String] 75 | $Encoding = 'UTF8', 76 | 77 | [Parameter(Mandatory = $false)] 78 | [Switch] 79 | $Compress 80 | ) 81 | 82 | begin { 83 | $userErrorActionPreference = $ErrorActionPreference 84 | $nullPtr = [IntPtr]::Zero 85 | } 86 | 87 | process { 88 | foreach ($s in $String) { 89 | try { 90 | if ($Compress) { 91 | $compressPtr = $nullPtr 92 | try { 93 | $length = [UIntPtr]::Zero 94 | $compressPtr = [ConvertCoreInterop]::compress_string($s, $Encoding, [ref]$length) 95 | 96 | if ($compressPtr -eq $nullPtr) { 97 | $errorMsg = GetRustError -DefaultMessage "Compression failed for encoding '$Encoding'" 98 | throw $errorMsg 99 | } 100 | 101 | $bytes = New-Object byte[] $length.ToUInt64() 102 | [System.Runtime.InteropServices.Marshal]::Copy($compressPtr, $bytes, 0, $bytes.Length) 103 | 104 | [System.Convert]::ToBase64String($bytes) 105 | } finally { 106 | if ($compressPtr -ne $nullPtr) { 107 | [ConvertCoreInterop]::free_bytes($compressPtr) 108 | } 109 | } 110 | } else { 111 | $ptr = $nullPtr 112 | try { 113 | $ptr = [ConvertCoreInterop]::string_to_base64($s, $Encoding) 114 | 115 | if ($ptr -eq $nullPtr) { 116 | $errorMsg = GetRustError -DefaultMessage "Base64 encoding failed for encoding '$Encoding'" 117 | throw $errorMsg 118 | } 119 | 120 | ConvertPtrToString -Ptr $ptr 121 | } finally { 122 | if ($ptr -ne $nullPtr) { 123 | [ConvertCoreInterop]::free_string($ptr) 124 | } 125 | } 126 | } 127 | } catch { 128 | Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference 129 | } 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /src/Tests/Unit/Global.Tests.ps1: -------------------------------------------------------------------------------- 1 | Describe -Name 'Module Manifest' -Fixture { 2 | BeforeAll { 3 | $module = Get-Module -Name 'Convert' 4 | if (-not $module) { 5 | throw 'Convert module is not loaded. This should not happen in test context.' 6 | } 7 | $script:ModuleName = 'Convert' 8 | $script:ModuleManifestFilePath = [System.IO.Path]::Combine($module.ModuleBase, "$script:ModuleName.psd1") 9 | $script:Manifest = Test-ModuleManifest -Path $script:ModuleManifestFilePath 10 | } 11 | 12 | It -Name 'Has the correct root module' -Test { 13 | ($script:Manifest).RootModule | Should -BeExactly "$script:ModuleName.psm1" 14 | } 15 | 16 | It -Name 'Has a valid version' -Test { 17 | $assertion = ($script:Manifest).Version 18 | $assertion.ToString().Split('.') | Should -HaveCount 3 19 | } 20 | 21 | It -Name 'Is compatible with PSEdition: <_>' -TestCases @( 22 | 'Core' 23 | 'Desktop' 24 | ) -Test { 25 | ($script:Manifest).CompatiblePSEditions | Should -Contain $_ 26 | } 27 | 28 | It -Name 'Requires PowerShell version 5.1' -Test { 29 | $assertion = ($script:Manifest).PowerShellVersion 30 | $expected = [System.Version]'5.1' 31 | $assertion | Should -BeExactly $expected 32 | } 33 | 34 | It -Name 'Has no module dependencies' -Test { 35 | $assertion = ($script:Manifest).RequiredModules 36 | $assertion | Should -BeNullOrEmpty 37 | } 38 | 39 | Context -Name 'Exported Functions' -Fixture { 40 | It -Name 'Exports the correct number of functions' -Test { 41 | $assertion = Get-Command -Module $script:ModuleName -CommandType Function 42 | $assertion | Should -HaveCount 30 43 | } 44 | 45 | It -Name '<_>' -TestCases @( 46 | 'ConvertFrom-Base64' 47 | 'ConvertFrom-Base64ToByteArray' 48 | 'ConvertFrom-Base64ToMemoryStream' 49 | 'ConvertFrom-Base64ToString' 50 | 'ConvertFrom-ByteArrayToBase64' 51 | 'ConvertFrom-ByteArrayToMemoryStream' 52 | 'ConvertFrom-CompressedByteArrayToString' 53 | 'ConvertFrom-EscapedUrl' 54 | 'ConvertFrom-HashTable' 55 | 'ConvertFrom-MemoryStream' 56 | 'ConvertFrom-MemoryStreamToBase64' 57 | 'ConvertFrom-MemoryStreamToByteArray' 58 | 'ConvertFrom-MemoryStreamToSecureString' 59 | 'ConvertFrom-MemoryStreamToString' 60 | 'ConvertFrom-StringToBase64' 61 | 'ConvertFrom-StringToByteArray' 62 | 'ConvertFrom-StringToCompressedByteArray' 63 | 'ConvertFrom-StringToMemoryStream' 64 | 'ConvertFrom-UnixTime' 65 | 'ConvertTo-Base64' 66 | 'ConvertTo-Celsius' 67 | 'ConvertTo-EscapedUrl' 68 | 'ConvertTo-Fahrenheit' 69 | 'ConvertTo-Hash' 70 | 'ConvertTo-MemoryStream' 71 | 'ConvertTo-String' 72 | 'ConvertTo-TitleCase' 73 | 'ConvertTo-UnixTime' 74 | 'Get-UnixTime' 75 | ) -Test { 76 | { Get-Command -Name $_ -Module $script:ModuleName -ErrorAction Stop } | Should -Not -Throw 77 | } 78 | } 79 | 80 | It -Name 'Exports no cmdlets' -Test { 81 | ($script:Manifest).ExportedCmdlets.GetEnumerator() | Should -HaveCount 0 82 | } 83 | 84 | It -Name 'Exports no variables' -Test { 85 | ($script:Manifest).ExportedVariables.GetEnumerator() | Should -HaveCount 0 86 | } 87 | 88 | Context -Name 'Exported Aliases' -Fixture { 89 | It -Name 'Exports three aliases' -Test { 90 | ($script:Manifest).ExportedAliases.GetEnumerator() | Should -HaveCount 3 91 | } 92 | 93 | It -Name '' -TestCases @( 94 | @{ 95 | Alias = 'ConvertFrom-Base64StringToByteArray' 96 | ResolvedCommand = 'ConvertFrom-Base64ToByteArray' 97 | } 98 | @{ 99 | Alias = 'ConvertFrom-ByteArrayToBase64String' 100 | ResolvedCommand = 'ConvertFrom-ByteArrayToBase64' 101 | } 102 | @{ 103 | Alias = 'ConvertFrom-StreamToString' 104 | ResolvedCommand = 'ConvertFrom-MemoryStreamToString' 105 | } 106 | ) -Test { 107 | $assertion = Get-Alias -Name $Alias 108 | $assertion.Source | Should -BeExactly $script:ModuleName 109 | $assertion.Version | Should -BeExactly ($script:Manifest).Version 110 | $assertion.ResolvedCommand | Should -BeExactly $ResolvedCommand 111 | $assertion.Visibility | Should -BeExactly 'Public' 112 | } 113 | } 114 | 115 | It -Name 'Exports no DSC resources' -Test { 116 | ($script:Manifest).ExportedDscResources.GetEnumerator() | Should -HaveCount 0 117 | } 118 | } 119 | --------------------------------------------------------------------------------