本帖最后由 ivor 于 2019-1-9 20:06 编辑
转载自:https://gallery.technet.microsof ... 0-bf32-eca99b4e42f4- $path = "C:\windows\SysWOW64\eapagent.ini"
- $ini=Get-IniContent $path
- $ini["HTTP_SERVER"]["default.Host"]="128.36.10.115"
- Out-IniFile -InputObject $ini -FilePath $path -Force
-
-
- Function Get-IniContent {
-
- [CmdletBinding()]
- Param(
- [ValidateNotNullOrEmpty()]
- [ValidateScript({(Test-Path $_) -and ((Get-Item $_).Extension -eq ".ini")})]
- [Parameter(ValueFromPipeline=$True,Mandatory=$True)]
- [string]$FilePath
- )
-
- Begin
- {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}
-
- Process
- {
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath"
-
- $ini = @{}
- switch -regex -file $FilePath
- {
- "^\[(.+)\]$" # 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
- }
- "(.+?)\s*=\s*(.*)" # Key
- {
- if (!($section))
- {
- $section = "No-Section"
- $ini[$section] = @{}
- }
- $name,$value = $matches[1..2]
- $ini[$section][$name] = $value
- }
- }
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath"
- Return $ini
- }
-
- End
- {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}
- }
-
- Function Out-IniFile {
-
- [CmdletBinding()]
- Param(
- [switch]$Append,
-
- [ValidateSet("Unicode","UTF7","UTF8","UTF32","ASCII","BigEndianUnicode","Default","OEM")]
- [Parameter()]
- [string]$Encoding = "Unicode",
-
-
- [ValidateNotNullOrEmpty()]
- [ValidatePattern('^([a-zA-Z]\:)?.+\.ini$')]
- [Parameter(Mandatory=$True)]
- [string]$FilePath,
-
- [switch]$Force,
-
- [ValidateNotNullOrEmpty()]
- [Parameter(ValueFromPipeline=$True,Mandatory=$True)]
- [Hashtable]$InputObject,
-
- [switch]$Passthru
- )
-
- Begin
- {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}
-
- Process
- {
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing to file: $Filepath"
-
- if ($append) {$outfile = Get-Item $FilePath}
- else {$outFile = New-Item -ItemType file -Path $Filepath -Force:$Force}
- if (!($outFile)) {Throw "Could not create File"}
- foreach ($i in $InputObject.keys)
- {
- if (!($($InputObject[$i].GetType().Name) -eq "Hashtable"))
- {
- #No Sections
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $i"
- Add-Content -Path $outFile -Value "$i=$($InputObject[$i])" -Encoding $Encoding
- } else {
- #Sections
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing Section: [$i]"
- Add-Content -Path $outFile -Value "[$i]" -Encoding $Encoding
- Foreach ($j in $($InputObject[$i].keys | Sort-Object))
- {
- if ($j -match "^Comment[\d]+") {
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing comment: $j"
- Add-Content -Path $outFile -Value "$($InputObject[$i][$j])" -Encoding $Encoding
- } else {
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $j"
- Add-Content -Path $outFile -Value "$j=$($InputObject[$i][$j])" -Encoding $Encoding
- }
-
- }
- Add-Content -Path $outFile -Value "" -Encoding $Encoding
- }
- }
- Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Writing to file: $path"
- if ($PassThru) {Return $outFile}
- }
-
- End
- {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}
- }
复制代码
|