【问题标题】:How to read a specific item of INI file using PowerShell?如何使用 PowerShell 读取 INI 文件的特定项?
【发布时间】:2019-06-06 06:50:33
【问题描述】:

我想读取我的 INI 文件的特定项目,我的 INI 文件部分中有 5 个项目,我想读取 4 个项目,除了第 3 个项目。

我已经尝试读取所有项目,但是我找不到如何指定我要读取的项目的方法,我读取的文件格式是这样的:

名称 值 AA 12 BB 13 抄送 14 15 天 EE 16

我使用这个命令来执行它。

File1.ps1 Read-File -FilePath C:\Users\Data.ini -a_section Code -store C:\Users\
function Read-File {
    Param(
        [Parameter(Mandatory=$true)]$FilePath,
        [Parameter(Mandatory=$true)]$a_section,
        [Parameter(Mandatory=$true)]$store
    )

    $input_file = $FilePath
    $ini_file = @{}

    Get-Content $input_file | ForEach-Object {
        $_.Trim()
    } | Where-Object {
        $_ -notmatch '^(;|$)'
    } | ForEach-Object {
        if ($_ -match '^\[.*\]$') {
            $section = $_ -replace '\[|\]'
            $ini_file[$section] = @{}
        } else {
            $key, $value = $_ -split '\s*=\s*', 2
            $ini_file[$section][$key] = $value
        }
    }

     $Path_Store = $store
     $Get_Reg = $ini_file.($a_section)

     $Output = $Get_Reg | Out-File $Path_Store\Out_Test
}

$cmd, $params = $args
& $cmd @params

我的预期结果,我有一个这样的输出文件

AA=12 BB=13 DD=15 EE=16

我的 INI 文件如下所示:

[姓名] 1=乔 2=恩典 [代码] AA=12 BB=13 抄送=14 DD=15 EE=16

【问题讨论】:

  • 请不要移动目标。如果您有后续问题:发布新问题。

标签: powershell parameters arguments


【解决方案1】:

试试这个:

function Get-IniSection {
    Param(
        [Parameter(Mandatory=$true)]$Path,
        [Parameter(Mandatory=$true)]$SectionName
    )

    $ini_file = @{}

    Get-Content $Path | ForEach-Object {
        $_.Trim()
    } | Where-Object {
        $_ -notmatch '^(;|$)'
    } | ForEach-Object {
        if ($_ -match '^\[.*\]$') {
            $section = $_ -replace '\[|\]'
            $ini_file += @{ $section = @{} }
        } else {
            $key, $value = $_ -split '\s*=\s*', 2
            $ini_file[$section] += @{ $key = $value }
        }
    }

    return $ini_file[$SectionName]
}

$section = Get-IniSection -Path "C:\temp\test.ini" -SectionName "code"

$section.GetEnumerator() | Where-Object { $_.Name -ne "EE" }

$section.GetEnumerator() | ForEach-Object { "$($_.Name)=$($_.Value)" }

$section.GetEnumerator() | 
    Where-Object { $_.Name -in @("A1","AE","AP","AS","E1","E2","JP","M1","M2","N1","N2","P1","P2","P3","P4","PR","RU","S1","S2","W1","W2","W3","W4","ZH") } | 
    Select-Object -ExpandProperty "Value"

$section.GetEnumerator() | 
    Where-Object { $_.Name -in @("A1","AE","AP","AS","E1","E2","JP","M1","M2","N1","N2","P1","P2","P3","P4","PR","RU","S1","S2","W1","W2","W3","W4","ZH") } | 
    Foreach-Object { ($_.Value -split ",")[0] }

【讨论】:

  • 感谢您的回答,非常感谢。抱歉,我修改了我的问题。
  • 添加了另一个用例,当您只需要值列表中的第一项时
  • 我修改了这部分 $Get_Reg = $ini_file.($SectionName) $Get_Reg = Get-IniSection $Path -SectionName $SectionName $Get_Reg.GetEnumerator() | Where-Object { $_.Name -ne "N3" } $Get_Reg.GetEnumerator() | ForEach-Object { "$($_.Name)=$($_.Value)" } $Get_Reg.GetEnumerator() | Where-Object { $_.Name -in @("A1","AE","AP","AS","E1","E2","JP","M1","M2","N1","N2","P1","P2","P3","P4","PR","RU","S1","S2","W1","W2","W3","W4","ZH") } | Foreach-Object { ($_.Value -split ",")[0] } $Get_Reg 但它不起作用
  • 您是否删除了return?您应该从函数返回值
  • 不,我不删除它` return $ini_file[$SectionName] } $section = Get-IniSection -Path $Path -SectionName $Section_Name $section.GetEnumerator() | Where-Object { $_.Name -ne "NE" } $section.GetEnumerator() | ForEach-Object { "$($_.Name)=$($_.Value)" } $section.GetEnumerator() | Where-Object { $_.Name -in @("A1","AE","AP","AS","E1","E2","JP","M1","M2","N1 ","N2","P1","P2","P3","P4","PR","RU","S1","S2","W1","W2","W3", "W4","ZH") } | Foreach-Object { ($_.Value -split ",")[0] }` 我用这个
猜你喜欢
  • 2019-08-17
  • 2013-09-13
  • 2011-03-30
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 2015-10-01
  • 1970-01-01
  • 2011-07-24
相关资源
最近更新 更多