Private/ConvertFrom-HPSSetupInformation.ps1
Function ConvertFrom-HPSSetupInformation { <# .SYNOPSIS Gets the content of an INI file .DESCRIPTION Gets the content of an INI file and returns it as a hashtable .INPUTS System.String .OUTPUTS System.Collections.Hashtable .PARAMETER Path Specifies the path to the input file. .EXAMPLE ConvertFrom-HPSSetupInformation "C:\myinifile.ini" .EXAMPLE $Inifilepath | $FileContent = ConvertFrom-HPSSetupInformation .LINK https://hardening.thomas-illiet.fr/Private/ConvertFrom-HPSSetupInformation/ .LINK https://github.com/thomas-illiet/Hardening/blob/master/Hardening/Private/ConvertFrom-HPSSetupInformation.ps1 .NOTES - File Name : ConvertFrom-HPSSetupInformation.ps1 - Author : Thomas ILLIET #> [CmdletBinding( HelpUri = "https://hardening.thomas-illiet.fr/Private/ConvertFrom-HPSSetupInformation/" )] [OutputType( [System.Collections.Hashtable] )] Param( # Specifies the path to the input file. [Parameter( ValueFromPipeline = $True, Mandatory = $True )] [ValidateNotNullOrEmpty()] [ValidateScript( { ( Test-Path $_ ) } )] [System.String] $Path ) begin { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started" } Process { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing file: $Path" $Ini = @{} switch -regex -file $Path { # Section "^\[(.+)\]$" { $Section = $matches[1] $Ini[$Section] = @{} $CommentCount = 0 } # Comment "^(;.*)$" { if (!($Section)) { $Section = "No Section" $Ini[$Section] = @{} } $Value = $matches[1] $CommentCount = $CommentCount + 1 $Name = "Comment" + $CommentCount $Ini[$Section][$Name] = $Value } # Key "(.+?)=(.*)" { if (!($Section)) { $Section = "No Section" $Ini[$Section] = @{} } $Name, $Value = ( $matches[1..2] -replace '\*' -replace '"' ).Trim() $Ini[$Section][$Name] = @{} switch ($Section) { "Registry Values" { $RegistryTypeList = @{ 1 = "REG_SZ" 2 = "REG_EXPAND_SZs" 3 = "REG_BINARY" 4 = "REG_DWORD" 7 = "REG_MULTI_SZ" } $RegistryTypeValue = $Value.split(',')[0] $Ini[$Section][$Name]["Type"] = $RegistryTypeList.Item([int]$RegistryTypeValue) if($RegistryTypeValue -eq 7) { $Ini[$Section][$Name]["Data"] = $Value.split(',') | select-object -skip 1 } else { if($Value.split(',')[1] -match "^\d+$") { $Ini[$Section][$Name]["Data"] = [Int]$Value.split(',')[1] } else { $Ini[$Section][$Name]["Data"] = [String]$Value.split(',')[1] } } } Default { if ( $Value -match "^\d+$" ) { $Ini[$Section][$Name]["Data"] = [Int]$Value } else { $Ini[$Section][$Name]["Data"] = [String]$Value } } } } } Write-Verbose "[$($MyInvocation.MyCommand.Name)] Finished Processing file: $Path" Return $Ini } end { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete" } } |