是否有可能使用PowerShell脚本configuration文件?
例如configuration文件:
#links link1=http://www.google.com link2=http://www.apple.com link3=http://www.microsoft.com
然后在PS1脚本中调用这个信息:
start-process iexplore.exe $Link1
在此先感谢您的帮助!!
非常感谢您的帮助Dennis和Tim! 你的回答使我走上了良好的轨道,我发现了这一点
SETTINGS.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
鲍威尔指挥
#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]) } }
然后
执行代码片段之后,variables($ h)将包含HashTable中的值。
Name Value ---- ----- MySetting1 value VerboseLogging True WaitForTime 20 OutputFile "C:\output.log" InputFile "C:\Users.txt"
*要从表中检索项目,请使用$h.Get_Item("MySetting1").*
这里有一个很好的线程显示了这个代码(引用链接的线程):
# 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
那你可以这样做:
PS> $links = import-ini links.ini PS> $links["search-engines"]["link1"] http://www.google.com PS> $links["vendors"]["link1"] http://www.apple.com
假设一个INI文件如下所示:
[vendors] link1=http://www.apple.com [search-engines] link1=http://www.google.com
不幸的是,链接代码中缺less正则expression式,因此您必须重新生成这些正则expression式,但是有一个版本可以处理不带节标题和注释行的文件。
是的,你要找的cmdlet是get-content和select-string。
$content=get-content C:\links.txt start-process iexplore.exe $content[0]