如何在 PowerShell 脚本中使用配置文件(ini、conf、...)?

Xav*_*r C 25 scripting powershell

是否可以将配置文件与 PowerShell 脚本一起使用?

例如配置文件:

#links
link1=http://www.google.com
link2=http://www.apple.com
link3=http://www.microsoft.com
Run Code Online (Sandbox Code Playgroud)

然后在 PS1 脚本中调用此信息:

start-process iexplore.exe $Link1
Run Code Online (Sandbox Code Playgroud)

Xav*_*r C 25

非常感谢丹尼斯和蒂姆的帮助!你的回答让我走上正轨,我发现了这个

设置.TXT

#from http://tlingenf.spaces.live.com/blog/cns!B1B09F516B5BAEBF!213.entry
#
[General]
MySetting1=value

[Locations]
InputFile="C:\Users.txt"
OutputFile="C:\output.log"

[Other]
WaitForTime=20
VerboseLogging=True
Run Code Online (Sandbox Code Playgroud)

PowerShell 命令

#from http://tlingenf.spaces.live.com/blog/cns!B1B09F516B5BAEBF!213.entry
#
Get-Content "C:\settings.txt" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }
Run Code Online (Sandbox Code Playgroud)

然后

执行代码片段后,变量 ($h) 将包含 HashTable 中的值。

Name                           Value
----                           -----
MySetting1                     value
VerboseLogging                 True
WaitForTime                    20
OutputFile                     "C:\output.log"
InputFile                      "C:\Users.txt"
Run Code Online (Sandbox Code Playgroud)

*要从表中检索项目,请使用命令 $h.Get_Item("MySetting1").*

  • 您还可以通过更友好的 $h.MySetting1 获取设置 (6认同)

小智 6

使用ConvertFrom-JsonPowerShell 中的Cmdlet 读取 JSON 格式的文件,有一种更方便的方法可以从 JSON 文件中读取设置:

$SettingsObject = Get-Content -Path \path\to\settings.json | ConvertFrom-Json
Run Code Online (Sandbox Code Playgroud)

在您的情况下,JSON 中的设置如下settings.json

{
    "link1": "http://www.google.com",
    "link2": "http://www.apple.com",
    "link3": "http://www.microsoft.com"
}
Run Code Online (Sandbox Code Playgroud)

读:

PS C:\> $SettingsObject = Get-Content -Path \path\to\settings.json | ConvertFrom-Json

PS C:\> $SettingsObject

link1                 link2                link3                   
-----                 -----                -----                   
http://www.google.com http://www.apple.com http://www.microsoft.com



PS C:\> $SettingsObject.link1
http://www.google.com
Run Code Online (Sandbox Code Playgroud)

来源:PowerTip:将 JSON 配置文件作为 PowerShell 对象读取


Den*_*son 5

这里有一个很好的线程它显示了这段代码(引用自链接的线程):

# from http://www.eggheadcafe.com/software/aspnet/30358576/powershell-and-ini-files.aspx
param ($file)

$ini = @{}
switch -regex -file $file
{
    "^\[(.+)\]$" {
        $section = $matches[1]
        $ini[$section] = @{}
    }
    "(.+)=(.+)" {
        $name,$value = $matches[1..2]
        $ini[$section][$name] = $value
    }
}
$ini
Run Code Online (Sandbox Code Playgroud)

然后你可以这样做:

PS> $links = import-ini links.ini
PS> $links["search-engines"]["link1"]
http://www.google.com
PS> $links["vendors"]["link1"]
http://www.apple.com
Run Code Online (Sandbox Code Playgroud)

假设一个 INI 文件如下所示:

[vendors]
link1=http://www.apple.com
[search-engines]
link1=http://www.google.com
Run Code Online (Sandbox Code Playgroud)

不幸的是,链接处的代码中缺少正则表达式,因此您必须重新生成它们,但是有一个版本可以处理没有节标题和注释行的文件。


Tim*_*Tim 3

是的,您要查找的 cmdlet 是 get-content 和 select-string。

$content=get-content C:\links.txt
start-process iexplore.exe $content[0]
Run Code Online (Sandbox Code Playgroud)