Powershell
function Read-Ini {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$FilePath,
[Parameter(Mandatory=$true)]
[string]$Section,
[Parameter(Mandatory=$true)]
[string]$Key
)
# Check if the file exists
if (-Not (Test-Path $FilePath)) {
Write-Host "Error: The file $FilePath does not exist."
return $null
}
$iniContent = Get-Content -Path $FilePath
$sectionFound = $false
# Loop through each line of the INI file
foreach ($line in $iniContent) {
if ($line -eq "[$Section]") {
$sectionFound = $true
}
if ($sectionFound -and $line -match "^$Key=") {
return ($line -split "=")[1]
}
}
Write-Host "Error: Key not found."
return $null
}
# Example usage
$value = Read-Ini -FilePath "C:\path\to\your.ini" -Section "Settings" -Key "username"
Write-Host "Value: $value"
Powershell compatible V1 (Winpe)
function Read-Ini($FilePath, $Section, $Key) {
# Check if the file exists
if (-not (Test-Path $FilePath)) {
Write-Host "Error: The file $FilePath does not exist."
return $null
}
$iniContent = Get-Content $FilePath
$sectionFound = $false
# Loop through each line of the INI file
foreach ($line in $iniContent) {
if ($line -eq "[$Section]") {
$sectionFound = $true
}
if ($sectionFound -and $line -match "^$Key=") {
return ($line -split "=")[1]
}
}
Write-Host "Error: Key not found."
return $null
}
# Example usage
$value = Read-Ini "C:\path\to\your.ini" "Settings" "username"
Write-Host "Value: $value"
0 Comments