Parsing ini file and adding it to a CSV file

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
jvierra
Posts: 12096
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Parsing ini file and adding it to a CSV file

Post by jvierra » Tue Dec 12, 2017 1:51 pm

With an INI file you have two choices. You can use the INI API to read the INI sections into a data structure and then manipulate them or you can read the filter strings and convert them into data. If you want to invent some other method then you are on your own. The methods I posted work with all INI files.

This:
[Section_1]
not need =1
Primary_need1=something1
Primary_need2=something2
Primary_need3=something3
Primary_Recovery_needed4=something4
Primary_Recovery_needed5=something5
Primary_Recovery_needed6=something6
not need =2
not need =3
Can be input and filtered with one line.

Code: Select all

$data = Get-Content inifile.ini | Where-Object{ $_ -match 'Primary_' } | Out-String | ConvertFrom-StringData
$data

Name                           Value
----                           -----
Primary_Recovery_needed6       something6
Primary_Recovery_needed4       something4
Primary_Recovery_needed5       something5
Primary_need3                  something3
Primary_need2                  something2
Primary_need1                  something1
Now you have objects that can be manipulated as needed.

You can also convert the hash to an object:
[pscustomobject]$data

User avatar
jvierra
Posts: 12096
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Parsing ini file and adding it to a CSV file

Post by jvierra » Tue Dec 12, 2017 1:56 pm

sekou2331 wrote:
Tue Dec 12, 2017 1:46 pm
Sorry but I dont know how ConvertFrom-StringData is going to help me. If I am confusing you I am sorry. If you look at the code I am parsing an ini file by selecting on two strings Primary and Secondary. Then from there I am removing everything that is not needed. Then I am storing it in $collected_parsed_items which is now a System.Object[]. Now for every three items in the array I want to look like the below.

before:
a
b
c
e
f
d

after:
abc
efd

I think you gave me an idea and I will figure it out and share my results. Once again thank you and I am sorry for the confusion. Still trying to get better at scripting.
I do not see what this has to do with reading an INI file.

User avatar
jvierra
Posts: 12096
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Parsing ini file and adding it to a CSV file

Post by jvierra » Tue Dec 12, 2017 5:49 pm

It sounds like you are asking a new question about how to work with arrays. It would have been best if you had opened a new question.

With arrays you can index through the array in sectiosn.

for($I = 0;$I -lt $count;$I+=3){

This will move you through the array 3 elements at a time.

User avatar
jvierra
Posts: 12096
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Parsing ini file and adding it to a CSV file

Post by jvierra » Tue Dec 12, 2017 5:58 pm

The following is the same as what I posted much earlier. Here it is with a simple example.

Code: Select all

# make an array of characters - an alphabet will do
$a = [char]'A' .. [char]'Z' | ForEach-Object{ [char]$_ }

# collect three at a time into strings
for ($i = 0; $i -lt $a.Count; $i += 3) { $a[$i .. ($i + 2)] -join '' }

User avatar
sekou2331
Posts: 258
Joined: Wed Aug 15, 2012 6:07 am

Re: Parsing ini file and adding it to a CSV file

Post by sekou2331 » Wed Dec 13, 2017 5:24 am

First let me say thanks jvierra for helping i am sorry it was a bit confusing. With that being said I didn't use ConvertFrom-StringData to parse or get the data because there were keys with the same name from my understanding and the errors from my console so it forced me to go another route as you can see below. I came up with the code below. I don't know if it is the best way to write this but it is working and giving me what is needed. Once again thanks jvierra for your input.

Code: Select all

#run select string to only grab what is needed from ini file 
$ConfigFile = select-string -path $env:HOMEDRIVE\file.ini -Pattern 'Primary', 'Secondary' -SimpleMatch
$collected_parsed_items = @()
#go through whats left and take out what is not needed 
foreach ($smd_PS in $ConfigFile.line) {
	if (!(
		$a -match "remove1" -or
		$a -match "remove2" -or
		$a -match "remove3" -or
		$a -match "remove4" -or
		$a -match "remove5" -or
		$a -match "remove6" -or
		$a -match "remove7" -or
		$a -match "remove8"))
	{
		#take all data left and remove everything before the equal sign as well as the equal sign
		$parseOut_all = $smd_PS -replace '.*='
		#added to array
		$collected_parsed_items += $parseOut_all
	}
	
}

# for all data in array count three and create a row with comma between each
$collect_data = @()
for ($i = 0; $i -lt $collected_parsed_items.count; $i += 3) {
	$collect_data += $collected_parsed_items[$i .. ($i + 2)] -join ','
}



#output to a created csv. 
$collect_data | Out-File -FilePath $env:HOMEDRIVE\Test.csv


#Import newly created csv file with created headers
$Header = '1', '2', '3'
$imported_csvdata = import-csv -Path $env:HOMEDRIVE\Test.csv -Header $Header

Locked