问题
I would like to parse an ini file and capture data from a Section. Basically I am trying to capture the version of an AV installed on 1000+ Servers. the ini file contains "Program_Version". This variable contains the version no. If I search using Program_Version=, I directly find the phrase after the "=" sign it contains the version no. Like 8.0, 10.6 etc
Can some one please guide me on how to achieve this?
Thanks
回答1:
Here is the function I use :
function Parse-IniFile
{
[CmdletBinding()]
Param
(
[Parameter(mandatory=$true,ValueFromPipeline=$true)]
[Alias("Fichier")]
[string]$fic
)
begin {}
Process
{
$ini = @{}
switch -regex -file $fic
{
"^\[(.+)\]$"
{
$section = $matches[1]
$ini[$section] = @{}
}
"(.+)=(.+)"
{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
end {}
}
given an INI file from "C:\Windows\System32\DriverStore\FileRepository" directory tree.
$p = Parse-IniFile "C:\Windows\System32\DriverStore\FileRepository\adihdaud.inf_amd64_neutral_66552f06054bc4ee\Mixer.ini"
$p["FRENCH"]["KSPINNAME_ADI_ALT_PCBEEP_SOURCE"]
gives
"Bip PC"
回答2:
.ini files are normally key = value pairs, that are easily convertible to a hash table using ConvertFrom-StringData
Read a properties file in powershell
From there it's just a matter of reading the value of the hash Program_Version key from the hash table.
来源:https://stackoverflow.com/questions/20534845/capture-data-from-ini-file-powershell