[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 ivor 于 2019-1-9 20:06 编辑

转载自:https://gallery.technet.microsof ... 0-bf32-eca99b4e42f4
  1. $path = "C:\windows\SysWOW64\eapagent.ini"
  2. $ini=Get-IniContent $path
  3. $ini["HTTP_SERVER"]["default.Host"]="128.36.10.115"  
  4. Out-IniFile -InputObject $ini -FilePath $path -Force  
  5. Function Get-IniContent {  
  6.       
  7.     [CmdletBinding()]  
  8.     Param(  
  9.         [ValidateNotNullOrEmpty()]  
  10.         [ValidateScript({(Test-Path $_) -and ((Get-Item $_).Extension -eq ".ini")})]  
  11.         [Parameter(ValueFromPipeline=$True,Mandatory=$True)]  
  12.         [string]$FilePath  
  13.     )  
  14.       
  15.     Begin  
  16.         {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}  
  17.          
  18.     Process  
  19.     {  
  20.         Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath"  
  21.               
  22.         $ini = @{}  
  23.         switch -regex -file $FilePath  
  24.         {  
  25.             "^\[(.+)\]$" # Section  
  26.             {  
  27.                 $section = $matches[1]  
  28.                 $ini[$section] = @{}  
  29.                 $CommentCount = 0  
  30.             }  
  31.             "^(;.*)$" # Comment  
  32.             {  
  33.                 if (!($section))  
  34.                 {  
  35.                     $section = "No-Section"  
  36.                     $ini[$section] = @{}  
  37.                 }  
  38.                 $value = $matches[1]  
  39.                 $CommentCount = $CommentCount + 1  
  40.                 $name = "Comment" + $CommentCount  
  41.                 $ini[$section][$name] = $value  
  42.             }   
  43.             "(.+?)\s*=\s*(.*)" # Key  
  44.             {  
  45.                 if (!($section))  
  46.                 {  
  47.                     $section = "No-Section"  
  48.                     $ini[$section] = @{}  
  49.                 }  
  50.                 $name,$value = $matches[1..2]  
  51.                 $ini[$section][$name] = $value  
  52.             }  
  53.         }  
  54.         Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath"  
  55.         Return $ini  
  56.     }  
  57.          
  58.     End  
  59.         {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}  
  60. }
  61. Function Out-IniFile {  
  62.       
  63.     [CmdletBinding()]  
  64.     Param(  
  65.         [switch]$Append,  
  66.          
  67.         [ValidateSet("Unicode","UTF7","UTF8","UTF32","ASCII","BigEndianUnicode","Default","OEM")]  
  68.         [Parameter()]  
  69.         [string]$Encoding = "Unicode",  
  70.          
  71.         [ValidateNotNullOrEmpty()]  
  72.         [ValidatePattern('^([a-zA-Z]\:)?.+\.ini$')]  
  73.         [Parameter(Mandatory=$True)]  
  74.         [string]$FilePath,  
  75.          
  76.         [switch]$Force,  
  77.          
  78.         [ValidateNotNullOrEmpty()]  
  79.         [Parameter(ValueFromPipeline=$True,Mandatory=$True)]  
  80.         [Hashtable]$InputObject,  
  81.          
  82.         [switch]$Passthru  
  83.     )  
  84.       
  85.     Begin  
  86.         {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}  
  87.          
  88.     Process  
  89.     {  
  90.         Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing to file: $Filepath"  
  91.          
  92.         if ($append) {$outfile = Get-Item $FilePath}  
  93.         else {$outFile = New-Item -ItemType file -Path $Filepath -Force:$Force}  
  94.         if (!($outFile)) {Throw "Could not create File"}  
  95.         foreach ($i in $InputObject.keys)  
  96.         {  
  97.             if (!($($InputObject[$i].GetType().Name) -eq "Hashtable"))  
  98.             {  
  99.                 #No Sections  
  100.                 Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $i"  
  101.                 Add-Content -Path $outFile -Value "$i=$($InputObject[$i])" -Encoding $Encoding  
  102.             } else {  
  103.                 #Sections  
  104.                 Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing Section: [$i]"  
  105.                 Add-Content -Path $outFile -Value "[$i]" -Encoding $Encoding  
  106.                 Foreach ($j in $($InputObject[$i].keys | Sort-Object))  
  107.                 {  
  108.                     if ($j -match "^Comment[\d]+") {  
  109.                         Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing comment: $j"  
  110.                         Add-Content -Path $outFile -Value "$($InputObject[$i][$j])" -Encoding $Encoding  
  111.                     } else {  
  112.                         Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $j"  
  113.                         Add-Content -Path $outFile -Value "$j=$($InputObject[$i][$j])" -Encoding $Encoding  
  114.                     }  
  115.                      
  116.                 }  
  117.                 Add-Content -Path $outFile -Value "" -Encoding $Encoding  
  118.             }  
  119.         }  
  120.         Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Writing to file: $path"  
  121.         if ($PassThru) {Return $outFile}  
  122.     }  
  123.          
  124.     End  
  125.         {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}  
  126. }
复制代码

TOP

返回列表