kotb92

Ini file powershell parse

Hallo zusammen,
Ich habe ein paar solche 'ini' Dateien die ich mit Powershell in eine csv Datei parsen möchte.
Die gefillterte daten sind
Customer Name, Applikation Name, Datenbanken, StartModule , Anzahl der Benutzern, Anzahl der Rollen der Benutzern 'Functionality'.
Kann mir jm bitte dabei hilfen?
Viele Grüße
Kotb
; LICENSE.INI file
;
; Testmanager will only work 
; if this LICENSE.INI file and the corresponding LICENSE.KEY are found
; in Testmanager 's installation directory.  

; !! THIS FILE MUST NOT BE CHANGED OR EDITED IN ANY WAY !!

;----------------------------------------------------------
[General]
(Standard)=(undefined)

Customer        = SCRIPTUS
Application     = Testmanager
Expiration Date = none

; Functionality : Possible values are (one value only):
; Demo, View, Review, Compile

; Options : Possible values are (list of values allowed):
; Studies, Comfort, DocManager, Publish, EASY

; DMS (Document Management System): Possible values are
; none, ODMA, Documentum

; AMS (Archive Management System): Possible values are
; none (file system), Documentum, miniDMS

; DB (Database support): Possible values are
; ACCESS, Oracle, SQLserver 

; StartModule : Possible values are 
; Testmanager, Valmanager 


users = SCRIPT\laura, SCRIPT\lara         
LicenseDate=2015-07-02 11:46


[User SCRIPT\laura]
Functionality   = Compile
Options         = Comfort, Publish
DMS             = miniDMS
AMS             = miniDMS
DB              = SQLserver
StartModule     = Testmanager SerialNo= C6A3-FC4F-D332F-453B-E871-2850-A98D-88C8


[User SCRIPT\lara]
Functionality   = Compile
Options         = Comfort, Publish
DMS             = miniDMS
AMS             = miniDMS
DB              = SQLserver
StartModule     = Testmanager
SerialNo=C6A3-FC4F-D332F-453B-E871-2850-A98D-88C7
Auf Facebook teilen
Auf X (Twitter) teilen
Auf Reddit teilen
Auf Linkedin teilen

Content-ID: 285341

Url: https://administrator.de/forum/ini-file-powershell-parse-285341.html

Ausgedruckt am: 25.04.2025 um 09:04 Uhr

Snowman25
Lösung Snowman25 12.10.2015 aktualisiert um 22:09:55 Uhr
Goto Top
Hallo @kotb92,

Hol dir die CMDlets Get-IniContent und Out-IniFile.
Beschrieben sind sie beide hier: Technet ~ Use PowerShell to Work with Any INI File.

Damit kannst du Ini-Files in Hashtables lesen und Hashtables in Ini-Files speichern.

Gruß,
@Snowman25
kotb92
kotb92 20.10.2015 um 14:24:31 Uhr
Goto Top
Hallo Snowman25,
Danke für die Antwort. Das war hilfreich. nur bin ich stehen geblieben bei der versuch die Anzahl der users zu berechnen.
Hier ist mein code
$fileDirectory = "C:\Users\kotb\Desktop\companys";  
$CompanyWithoutIni = "C:\Users\kotb\Desktop\companys\CompanyWithoutIni.txt";  
$CompanyWithIni = "C:\Users\kotb\Desktop\companys\CompanyWithIni.txt";  
$parse_results = New-Object System.Collections.ArrayList;

# Delete the old out files if the exist.
If (Test-Path $CompanyWithoutIni){
	Remove-Item $CompanyWithoutIni, $CompanyWithIni
}
# Use a foreach to loop through all the files in a directory.
# This method allows us to easily track the file name so we can report 
# our findings by file.

foreach($file in Get-ChildItem $fileDirectory)
{
    # We will need to tell the Switch command exactly where to parse, so we'll put together 
    # the full file path.
	$iniPath = "03_Permanent_License\License.INI"  
	$filePath = $fileDirectory + "\" + $file + "\" + $iniPath;  
	if (Test-Path $filePath ){
	$content = Get-content -Path $filePath
	# Process each line
	for($i = 0; $i -lt $content.Count; $i++){
           $line = $content[$i]
	   $us = (Where-Object {$line.StartsWith("[User")}| Measure-Object).count  
       if ($line.StartsWith("[User")) {  
		 Add-content "Number of users = ", $us -path $CompanyWithIni  
		 Add-content $line -path $CompanyWithIni
       } else{} 
    }
	Add-content $file.name  -path $CompanyWithIni
	Add-content "==================" -path $CompanyWithIni  
}Else{
  Add-content $file.name -path $CompanyWithoutIni
}	   
}
Snowman25
Snowman25 20.10.2015 um 16:01:14 Uhr
Goto Top
Hallo kotb,

Ich glaube, in Zeile 27 möchtest du das lieber so schreiben:
Add-Content "Number of Users = $us" -path $CompanyWithIni  
Output:
Number of Users = <Zahl>
Anstelle von:
Number of Users = 
<Zahl>

Abgesehen davon kann ich jetzt aber keine großen Schnitzer sehen. Allerdings finde ich PowerShell auch ziemlich unleserlich.
Benutz doch mal die Powershell ISE und Debugge das Skript bei dir indem du in Einzelschritten (F11) hindurchspringst.
Ich vermute den Fehler am ehesten in Zeile 25.

Gruß,
@Snowman25