schmidle
Goto Top

INI Datei über Batch Script verändern

Hallo zusammen,

ich muss in einer ini Datei einen Eintrag anpassen, der den ProxyServer hinterlegt. Wir haben einen neuen Proxy Server bekommen mit einer neuen Adresse und einem neuen Port.
INI Datei sieht wie folgt aus: (Adressen sind nur ein Beispiel)

[GENERAL]
SERVER1=https://test.de
USERNAME=test@test.com
ACCEPTSHUTDOWN=1
AGENTMODE=1
COMPANYTYPE=0
REBOOTIMMEDIATEPENDING=0
RUNASSETSCANIMMEDIATE=0
[PROXYSERVER]
ENABLED=1
HOST=4.4.4.4
PORT=5000
USERNAME=test\Muser
USERKEY=123456
CACHEPATH=
FAILING=1
VERBOSELOGGING=0

Ich muss den Eintrag Proxy Server nun wie folgt anpassen:
[PROXYSERVER]
ENABLED=1
HOST=8.8.8.8
PORT=8000
USERNAME=
USERKEY=

Es kann aber auch vorkommen, dass der Eintrag des Proxyservers gar nicht in der INI Datei vorhanden ist, dann müsste das Script den Eintrag erstellen.

Ich habe selbst mal versucht eine Batch anzulegen. Ich komme aber nicht mehr weiter, meine Batch kann nur eine Zeile verändern, und auch nur wenn der Eintrag vorhanden ist.
So sieht meine Batch bis jetzt aus:

@echo off & setlocal
set "ini=C:\Test\settings.ini"
set "bak=%ini%.bak"
set "Suche=[PROXYSERVER]"
set "Neu=HOST=8.8.8.8"

move "%ini%" "%bak%"
set Zeile=
for /f "tokens=1-2 delims=:" %%i in ('findstr /b /n /c:"%Suche%" "%bak%"') do set Zeile=%%i
if not defined Zeile echo "%Suche%" nicht gefunden! & pause & goto :eof

set /a Zeile+=1
for /f "tokens=1-2 delims=:" %%i in ('findstr /n "^" "%bak%"') do (
if %%i neq %Zeile% (echo\%%j) else (echo %Neu%)
)>>"%ini%"
del "%bak%"

Kann mir hier jemand helfen, sodass ich die INI Datei nach meinen Wünschen verändert ?

Grüße

Content-Key: 279546

Url: https://administrator.de/contentid/279546

Printed on: April 19, 2024 at 23:04 o'clock

Member: SauBaer2014
SauBaer2014 Aug 07, 2015 at 12:09:05 (UTC)
Goto Top
... Hallo ...


wenn alle Werte innerhalb der *.ini feststehen ... also statische Werte sind (also z.B. 3 Szenerien), warum erzeugst du nicht diese 3 möglichen INI-Dateien
und änderst dann nur per Batch den Namen des Datei, die du folglich mit dem bestimmten Inhalt benötigst...?Ist doch einfacher als ein Script der den benötigten Wert setzt?


MFG
Member: Schmidle
Schmidle Aug 07, 2015 at 12:17:40 (UTC)
Goto Top
in der Ini Datei stehen noch mehrere Einträge. Die bei jedem Rechner anderst sind.
Von dem her ist das leider nicht möglich.
Mitglied: 114757
114757 Aug 07, 2015 updated at 14:50:44 (UTC)
Goto Top
Moin zusammen,
mit Powershell sieht das so aus:

Quelle der Ini-Scripte
https://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8 ...
https://gallery.technet.microsoft.com/7d7c867f-026e-4620-bf32-eca99b4e42 ...


Pfad zur Ini in Zeile 1 eintragen, den Rest für die Anpassung findest du ganz unten im Skript.
$pfad = 'C:\temp\demo.ini'  

Function Get-IniContent {
    [CmdletBinding()]  
    Param(  
        [ValidateNotNullOrEmpty()]  
        [ValidateScript({(Test-Path $_) -and ((Get-Item $_).Extension -eq ".ini")})]    
        [Parameter(ValueFromPipeline=$True,Mandatory=$True)]  
        [string]$FilePath  
    )  
      
    Begin  
        {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}    
          
    Process  
    {  
        Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath"    
              
        $ini = @{}  
        switch -regex -file $FilePath  
        {  
            "^\[(.+)\]$" # Section    
            {  
                $section = $matches[1]  
                $ini[$section] = @{}  
                $CommentCount = 0  
            }  
            "^(;.*)$" # Comment    
            {  
                if (!($section))  
                {  
                    $section = "No-Section"    
                    $ini[$section] = @{}  
                }  
                $value = $matches[1]  
                $CommentCount = $CommentCount + 1  
                $name = "Comment" + $CommentCount    
                $ini[$section][$name] = $value  
            }   
            "(.+?)\s*=\s*(.*)" # Key    
            {  
                if (!($section))  
                {  
                    $section = "No-Section"    
                    $ini[$section] = @{}  
                }  
                $name,$value = $matches[1..2]  
                $ini[$section][$name] = $value  
            }  
        }  
        Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath"    
        Return $ini  
    }  
          
    End  
        {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}    
} 
Function Out-IniFile {
    [CmdletBinding()]  
    Param(  
        [switch]$Append,  
          
        [ValidateSet("Unicode","UTF7","UTF8","UTF32","ASCII","BigEndianUnicode","Default","OEM")]    
        [Parameter()]  
        [string]$Encoding = "Unicode",    
 
          
        [ValidateNotNullOrEmpty()]  
        [ValidatePattern('^([a-zA-Z]\:)?.+\.ini$')]    
        [Parameter(Mandatory=$True)]  
        [string]$FilePath,  
          
        [switch]$Force,  
          
        [ValidateNotNullOrEmpty()]  
        [Parameter(ValueFromPipeline=$True,Mandatory=$True)]  
        [Hashtable]$InputObject,  
          
        [switch]$Passthru  
    )  
      
    Begin  
        {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}    
          
    Process  
    {  
        Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing to file: $Filepath"    
          
        if ($append) {$outfile = Get-Item $FilePath}  
        else {$outFile = New-Item -ItemType file -Path $Filepath -Force:$Force}  
        if (!($outFile)) {Throw "Could not create File"}    
        foreach ($i in $InputObject.keys)  
        {  
            if (!($($InputObject[$i].GetType().Name) -eq "Hashtable"))    
            {  
                #No Sections  
                Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $i"    
                Add-Content -Path $outFile -Value "$i=$($InputObject[$i])" -Encoding $Encoding    
            } else {  
                #Sections  
                Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing Section: [$i]"    
                Add-Content -Path $outFile -Value "[$i]" -Encoding $Encoding    
                Foreach ($j in $($InputObject[$i].keys | Sort-Object))  
                {  
                    if ($j -match "^Comment[\d]+") {    
                        Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing comment: $j"    
                        Add-Content -Path $outFile -Value "$($InputObject[$i][$j])" -Encoding $Encoding    
                    } else {  
                        Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $j"    
                        Add-Content -Path $outFile -Value "$j=$($InputObject[$i][$j])" -Encoding $Encoding    
                    }  
                      
                }  
                Add-Content -Path $outFile -Value "" -Encoding $Encoding    
            }  
        }  
        Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Writing to file: $path"    
        if ($PassThru) {Return $outFile}  
    }  
          
    End  
        {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}    
} 

$ini = Get-IniContent -FilePath $pfad

if (!$ini.PROXYSERVER){
    $ini.PROXYSERVER = @{}   
}
$ini.PROXYSERVER.ENABLED = '1'  
$ini.PROXYSERVER.HOST = '8.8.8.8'  
$ini.PROXYSERVER.PORT = '8000'  
$ini.PROXYSERVER.USERNAME = ''  
$ini.PROXYSERVER.USERKEY = ''  

$ini | Out-IniFile $pfad -Force -Encoding UTF8
Gruß jodel32