如何在PowerShell中阅读特定的INI文件? [英] How to read specific section INI file in powershell?

查看:370
本文介绍了如何在PowerShell中阅读特定的INI文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个INI文件,如下所示:



I have an INI file, look like this :

[Setting]
Level=5
Name=x:\process.log
Log=1
Type=2

[State]
stage=10
step=Capture
status=BEGIN
Step=
Status=





我想阅读INI文件的特定部分。我想要阅读该元素的方式是这样的:

我用CMD运行它:



Script.ps1 Get-Infile -pathofINIfile设置名称Setting_Name.cmd



我想阅读[设置]部分和名称的值,然后将其写入文件Setting_Name.cmd



我的尝试:





I want to read specific section of the INI file. The way I want to read that element is like this :
I run it with CMD :

Script.ps1 Get-Infile -pathofINIfile Setting Name Setting_Name.cmd

I want to read section [Setting] and value of "Name" , then write it in to file "Setting_Name.cmd"

What I have tried:

Function Get-IniFile
{

Param(
  [parameter(mandatory=$true)][string]$path
)
$inifile = $path
$ini = @{}

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

$read = $ini.Setting.Name
$read

}

推荐答案

true)] [string]
true)][string]


path
path )


inifile =
inifile =


这篇关于如何在PowerShell中阅读特定的INI文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆