Powershell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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"
Categories: Powershell

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.