|
| 1 | +<# |
| 2 | +Copyright 2014 Cloudbase Solutions Srl |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +#> |
| 16 | + |
| 17 | +$Source = @" |
| 18 | +using System; |
| 19 | +using System.Text; |
| 20 | +using System.Runtime.InteropServices; |
| 21 | +
|
| 22 | +namespace PSCloudbase |
| 23 | +{ |
| 24 | + public sealed class Win32IniApi |
| 25 | + { |
| 26 | + [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)] |
| 27 | + public static extern uint GetPrivateProfileString( |
| 28 | + string lpAppName, |
| 29 | + string lpKeyName, |
| 30 | + string lpDefault, |
| 31 | + StringBuilder lpReturnedString, |
| 32 | + uint nSize, |
| 33 | + string lpFileName); |
| 34 | +
|
| 35 | + [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)] |
| 36 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 37 | + public static extern bool WritePrivateProfileString( |
| 38 | + string lpAppName, |
| 39 | + string lpKeyName, |
| 40 | + StringBuilder lpString, // Don't use string, as Powershell replaces $null with an empty string |
| 41 | + string lpFileName); |
| 42 | +
|
| 43 | + [DllImport("Kernel32.dll")] |
| 44 | + public static extern uint GetLastError(); |
| 45 | + } |
| 46 | +} |
| 47 | +"@ |
| 48 | + |
| 49 | +Add-Type -TypeDefinition $Source -Language CSharp |
| 50 | + |
| 51 | +function Get-IniFileValue |
| 52 | +{ |
| 53 | + [CmdletBinding()] |
| 54 | + param |
| 55 | + ( |
| 56 | + [parameter(Mandatory=$true, ValueFromPipeline=$true)] |
| 57 | + [string]$Key, |
| 58 | + |
| 59 | + [parameter()] |
| 60 | + [string]$Section = "DEFAULT", |
| 61 | + |
| 62 | + [parameter()] |
| 63 | + [string]$Default = $null, |
| 64 | + |
| 65 | + [parameter(Mandatory=$true)] |
| 66 | + [string]$Path |
| 67 | + ) |
| 68 | + process |
| 69 | + { |
| 70 | + $sb = New-Object -TypeName "System.Text.StringBuilder" -ArgumentList 1000 |
| 71 | + $retVal = [PSCloudbase.Win32IniApi]::GetPrivateProfileString($Section, $Key, $Default, $sb, $sb.Capacity, $Path) |
| 72 | + if (!$retVal) |
| 73 | + { |
| 74 | + $lastErr = [PSCloudbase.Win32IniApi]::GetLastError() |
| 75 | + if ($lastErr -ne 2) |
| 76 | + { |
| 77 | + throw "Cannot get value from ini file: " + [PSCloudbase.Win32IniApi]::GetLastError() |
| 78 | + } |
| 79 | + elseif (!(Test-Path $Path)) |
| 80 | + { |
| 81 | + throw "Ini file '$Path' does not exist" |
| 82 | + } |
| 83 | + } |
| 84 | + return $sb.ToString() |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function Set-IniFileValue |
| 89 | +{ |
| 90 | + [CmdletBinding()] |
| 91 | + param |
| 92 | + ( |
| 93 | + [parameter(Mandatory=$true, ValueFromPipeline=$true)] |
| 94 | + [string]$Key, |
| 95 | + |
| 96 | + [parameter()] |
| 97 | + [string]$Section = "DEFAULT", |
| 98 | + |
| 99 | + [parameter(Mandatory=$true)] |
| 100 | + [string]$Value, |
| 101 | + |
| 102 | + [parameter(Mandatory=$true)] |
| 103 | + [string]$Path |
| 104 | + ) |
| 105 | + process |
| 106 | + { |
| 107 | + $retVal = [PSCloudbase.Win32IniApi]::WritePrivateProfileString($Section, $Key, $Value, $Path) |
| 108 | + if (!$retVal -and [PSCloudbase.Win32IniApi]::GetLastError()) |
| 109 | + { |
| 110 | + throw "Cannot set value in ini file: " + [PSCloudbase.Win32IniApi]::GetLastError() |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +function Remove-IniFileValue |
| 116 | +{ |
| 117 | + [CmdletBinding()] |
| 118 | + param |
| 119 | + ( |
| 120 | + [parameter(Mandatory=$true, ValueFromPipeline=$true)] |
| 121 | + [string]$Key, |
| 122 | + |
| 123 | + [parameter()] |
| 124 | + [string]$Section = "DEFAULT", |
| 125 | + |
| 126 | + [parameter(Mandatory=$true)] |
| 127 | + [string]$Path |
| 128 | + ) |
| 129 | + process |
| 130 | + { |
| 131 | + $retVal = [PSCloudbase.Win32IniApi]::WritePrivateProfileString($Section, $Key, $null, $Path) |
| 132 | + if (!$retVal -and [PSCloudbase.Win32IniApi]::GetLastError()) |
| 133 | + { |
| 134 | + throw "Cannot remove value from ini file: " + [PSCloudbase.Win32IniApi]::GetLastError() |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments