powershell PowerShell中的INI文件功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell中的INI文件功能相关的知识,希望对你有一定的参考价值。

function Get-IniSection
{
     param
     (
         [Object]
         $inifile,

         [Object]
         $section
     )

   $sections = select-string '^\[.*\]' $inifile
   if(!$section) {
      return $sections | %{$_.Line.Trim('[]')}
   }
   $start = 0
   switch($sections){
      {$_.Line.Trim() -eq "[$section]"}{
         $start = $_.LineNumber -1
         
      }
      default {
         if($start){ 
            return (Get-Content $inifile)[($start)..($start + ($_.LineNumber-2 - $start))]
         }
      }
   }
   $lines = Get-Content $inifile
   return $lines[$start..($lines.length-1)]
}


function Get-IniValue
{
     param
     (
         [Object]
         $inifile,

         [Object]
         $section,

         [Object]
         $name
     )

   $section = Get-IniSection $inifile $section
   ($section | Select-String "^\s*$name\s*=").Line.Split('=',2)[1]
}

function Set-IniValue
{
     param
     (
         [Object]
         $inifile,

         [Object]
         $section,

         [Object]
         $name,

         [Object]
         $value
     )

   $lines = Get-Content $inifile
   $sections = select-string '^\[.*\]' $inifile
   $start,$end = 0,0
   for($l=0; $l -lt $sections.Count; ++$l){
      if($sections[$l].Line.Trim() -eq "[$section]") {
         $start = $sections[$l].LineNumber
         if($l+1 -ge $sections.Count) {
            $end = $lines.length-1;
         } else {
            $end = $sections[$l+1].LineNumber -2
         }
      }
   }
   
   if($start -and $end) {
      $done = $false
      for($l=$start;$l -le $end;++$l){
         if( $lines[$l] -match "^\s*$name\s*=" ) {
            $lines[$l] = '{0} = {1}' -f $name, $value
            $done = $true
            break;
         }
      }
      if(!$done) {
         $output = $lines[0..$start]
         $output += '{0} = {1}' -f $name, $value
         $output += $lines[($start+1)..($lines.Length-1)]
         $lines = $output
      }
   }
   
   Set-Content $inifile $lines
}



##
## This is a ... different way of doing it, 
## which will be faster if you need to read lots of values
#### HOWEVER ####
## I don't recommend using Set-IniFile, because it will loose any comments etc
## 
function Get-IniFile {
param([string]$inifile=$(Throw 'You must specify the name of an ini file!'))
   $INI = @{}
   $s,$k,$v = $null
   foreach($line in (Get-Content $inifile | Where-Object {$_[0] -ne ';' -and $_.Trim().Length -gt 0})) {
      $k,$v = $line.Split('=',2)
      if($v -eq $null) {
         $s = $k.Trim('[]')
         $INI[$s] = @{}
      } else {
         $INI[$s][$k.Trim()] = $v.Trim()
      }
   }
   return $INI
}

function Set-IniFile {
param([HashTable]$ini,[string]$inifile=$(Throw 'You must specify the name of an ini file!'))
   [string[]]$inistring = @()
   foreach($section in $ini.Keys) {
      #Add-Content $inifile ("[{0}]" -f $section)
      $inistring += ("`n[{0}]" -f $section)
      foreach($key in $ini[$section].Keys) {
         $inistring += ('{0} = {1}' -f $key, $ini[$section][$key])
      }
   }
   # make the write be atomic ... 
   Set-Content $inifile $inistring
}

以上是关于powershell PowerShell中的INI文件功能的主要内容,如果未能解决你的问题,请参考以下文章

如何像 IN 语句一样使用 Powershell Where-Object

PowerShell初级篇●Powershell使用对象

Powershell foreach

Powershell Create DataTable(创建数据表)

Join EC2 into AD with SSM and remote powershell in AWS

保存凭据以供 powershell 重用和错误 ConvertTo-SecureString : Key not valid for use in specified state