Functions/Out-IniFile.ps1
Function Out-IniFile { <# .Synopsis Write hash content to INI file .Description Write hash content to INI file .Notes Author : Oliver Lipkau <oliver@lipkau.net> Blog : http://oliver.lipkau.net/blog/ Source : https://github.com/lipkau/PsIni http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91 #Requires -Version 2.0 .Inputs System.String System.Collections.IDictionary .Outputs System.IO.FileSystemInfo .Example Out-IniFile $IniVar "C:\myinifile.ini" ----------- Description Saves the content of the $IniVar Hashtable to the INI File c:\myinifile.ini .Example $IniVar | Out-IniFile "C:\myinifile.ini" -Force ----------- Description Saves the content of the $IniVar Hashtable to the INI File c:\myinifile.ini and overwrites the file if it is already present .Example $file = Out-IniFile $IniVar "C:\myinifile.ini" -PassThru ----------- Description Saves the content of the $IniVar Hashtable to the INI File c:\myinifile.ini and saves the file into $file .Example $Category1 = @{“Key1”=”Value1”;”Key2”=”Value2”} $Category2 = @{“Key1”=”Value1”;”Key2”=”Value2”} $NewINIContent = @{“Category1”=$Category1;”Category2”=$Category2} Out-IniFile -InputObject $NewINIContent -FilePath "C:\MyNewFile.ini" ----------- Description Creating a custom Hashtable and saving it to C:\MyNewFile.ini .Link Get-IniContent #> [CmdletBinding()] [OutputType( [System.IO.FileSystemInfo] )] Param( # Adds the output to the end of an existing file, instead of replacing the file contents. [switch] $Append, # Specifies the file encoding. The default is UTF8. # # Valid values are: # -- ASCII: Uses the encoding for the ASCII (7-bit) character set. # -- BigEndianUnicode: Encodes in UTF-16 format using the big-endian byte order. # -- Byte: Encodes a set of characters into a sequence of bytes. # -- String: Uses the encoding type for a string. # -- Unicode: Encodes in UTF-16 format using the little-endian byte order. # -- UTF7: Encodes in UTF-7 format. # -- UTF8: Encodes in UTF-8 format. [ValidateSet("Unicode", "UTF7", "UTF8", "ASCII", "BigEndianUnicode", "Byte", "String")] [Parameter()] [String] $Encoding = "UTF8", # Specifies the path to the output file. [ValidateNotNullOrEmpty()] [ValidateScript( {Test-Path $_ -IsValid} )] [Parameter( Position = 0, Mandatory = $true )] [String] $FilePath, # Allows the cmdlet to overwrite an existing read-only file. Even using the Force parameter, the cmdlet cannot override security restrictions. [Switch] $Force, # Specifies the Hashtable to be written to the file. Enter a variable that contains the objects or type a command or expression that gets the objects. [Parameter( Mandatory = $true, ValueFromPipeline = $true )] [System.Collections.IDictionary] $InputObject, # Passes an object representing the location to the pipeline. By default, this cmdlet does not generate any output. [Switch] $Passthru, # Adds spaces around the equal sign when writing the key = value [Switch] $Loose, # Writes the file as "pretty" as possible # # Adds an extra linebreak between Sections [Switch] $Pretty ) Begin { Write-Debug "PsBoundParameters:" $PSBoundParameters.GetEnumerator() | ForEach-Object { Write-Debug $_ } if ($PSBoundParameters['Debug']) { $DebugPreference = 'Continue' } Write-Debug "DebugPreference: $DebugPreference" Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started" function Out-Keys { param( [ValidateNotNullOrEmpty()] [Parameter( Mandatory, ValueFromPipeline )] [System.Collections.IDictionary] $InputObject, [ValidateSet("Unicode", "UTF7", "UTF8", "ASCII", "BigEndianUnicode", "Byte", "String")] [Parameter( Mandatory )] [string] $Encoding = "UTF8", [ValidateNotNullOrEmpty()] [ValidateScript( {Test-Path $_ -IsValid})] [Parameter( Mandatory, ValueFromPipelineByPropertyName )] [Alias("Path")] [string] $FilePath, [Parameter( Mandatory )] $Delimiter, [Parameter( Mandatory )] $MyInvocation ) Process { if (!($InputObject.get_keys())) { Write-Warning ("No data found in '{0}'." -f $FilePath) } Foreach ($key in $InputObject.get_keys()) { if ($key -match "^Comment\d+") { Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing comment: $key" "$($InputObject[$key])" | Out-File -Encoding $Encoding -FilePath $FilePath -Append } else { Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $key" $InputObject[$key] | ForEach-Object { "$key$delimiter$_" } | Out-File -Encoding $Encoding -FilePath $FilePath -Append } } } } $delimiter = '=' if ($Loose) { $delimiter = ' = ' } # Splatting Parameters $parameters = @{ Encoding = $Encoding; FilePath = $FilePath } } Process { $extraLF = "" if ($Append) { Write-Debug ("Appending to '{0}'." -f $FilePath) $outfile = Get-Item $FilePath } else { Write-Debug ("Creating new file '{0}'." -f $FilePath) $outFile = New-Item -ItemType file -Path $Filepath -Force:$Force } if (!(Test-Path $outFile.FullName)) {Throw "Could not create File"} Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing to file: $Filepath" foreach ($i in $InputObject.get_keys()) { if (!($InputObject[$i].GetType().GetInterface('IDictionary'))) { #Key value pair Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $i" "$i$delimiter$($InputObject[$i])" | Out-File -Append @parameters } elseif ($i -eq $script:NoSection) { #Key value pair of NoSection Out-Keys $InputObject[$i] ` @parameters ` -Delimiter $delimiter ` -MyInvocation $MyInvocation } else { #Sections Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing Section: [$i]" # Only write section, if it is not a dummy ($script:NoSection) if ($i -ne $script:NoSection) { "$extraLF[$i]" | Out-File -Append @parameters } if ($Pretty) { $extraLF = "`r`n" } if ( $InputObject[$i].Count) { Out-Keys $InputObject[$i] ` @parameters ` -Delimiter $delimiter ` -MyInvocation $MyInvocation } } } Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Writing to file: $FilePath" } End { if ($PassThru) { Write-Debug ("Returning file due to PassThru argument.") Write-Output (Get-Item $outFile) } Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended" } } Set-Alias oif Out-IniFile |