ConvertFrom-PowerShellDataFile.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#─────────────────────────────────────────────────────────────────────────────────────── # Module Name: ConvertFrom-PowerShellDataFile # Version: 1.1.4 # Author: Nathaniel Wallis Praytor #─────────────────────────────────────────────────────────────────────────────────────── function ConvertFrom-PowerShellDataFile { <# .SYNOPSIS Constructs a System.Collections.Hashtable Object from the contents stored in a specified PowerShell Data File (.psd1 extension). .DESCRIPTION Constructs a Custom Object of the TypeName 'System.Collections.Hashtable' around/from the contents stored within a specified PowerShell Data File/Module Manifest (.psd1 extension). It accomplishes this by the following means: First, an Object with the TypeName 'System.Collections.Hashtable' is created using the built-in 'New-Object' Cmdlet, and the resulting Object is immediately stored in the $Manifest Variable. Next, the 'Import-PowerShellDataFile' Cmdlet is used to load the files contents into directly into the Object via the Variable it was stored in, followed by another Variable being initialized and being set a value equal to the files BaseName using 'Split-Path $Filename -Leaf'. Lastly, the final Output variable is created, this time using the 'New-Variable' Cmdlet, and passing the $BaseName Variable to its 'Name' Parameter, and the $Manifest Variable is passed to its 'Value' Parameter. .PARAMETER FilePath Specifies the Path to a PowerShell Data File. .EXAMPLE PS C:\> ConvertFrom-PowerShellDataFile -FilePath 'C:\test\MyModule\PSScriptAnalyzerSettings.psd1' (The file 'PSScriptAnalyzerSettings.psd1' is converted to a Hashtable and stored in a Variable called '$PSScriptAnalyzerSettings') .LINK https://github.com/SupernautFX/ConvertFrom-PowerShellDataFile https://github.com/SupernautFX/SFX.PoshUtilities.DataFileTools https://github.com/SupernautFX/Posh-Configuration-Tool .NOTES Filename: 'ConvertFrom-PowerShellDataFile.ps1' Version: 1.1.2 Author: Nathaniel Wallis Praytor .FORWARDHELPTARGETNAME ConvertFrom-PowerShellDataFile .FORWARDHELPCATEGORY Function .EXTERNALHELP ConvertFrom-PowerShellDataFile.psm1-help.xml #> [CmdletBinding(SupportsShouldProcess)] [OutputType([System.Collections.Hashtable])] param( [Parameter( Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, HelpMessage = 'Specifies the path to a PowerShell Data File.' )] [Alias('Path')] [string]$FilePath ) BEGIN { Write-Information "" } PROCESS { Try { $Manifest = New-Object hashtable $Filename = Split-Path $FilePath -Leaf $BaseName = $Filename.Split('.')[0] $Manifest = (Import-PowerShellDataFile -Path $FilePath) New-Variable -Name $BaseName -Value $Manifest } Catch { Write-Error $_ } } END { } } Set-Alias -Name cfpsd1 -Value ConvertFrom-PowerShellDataFile Set-Alias -Name ConvertFrom-PSD1 -Value ConvertFrom-PowerShellDataFile Export-ModuleMember -Function ConvertFrom-PowerShellDataFile Export-ModuleMember -Alias cfpsd1 -Function ConvertFrom-PowerShellDataFile Export-ModuleMember -Alias ConvertFrom-PSD1 -Function ConvertFrom-PowerShellDataFile |