Page 2 of 2

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

Posted: Tue Dec 12, 2017 12:51 pm
by jvierra
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

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

Posted: Tue Dec 12, 2017 12:56 pm
by jvierra
sekou2331 wrote: Tue Dec 12, 2017 12: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.

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

Posted: Tue Dec 12, 2017 4:49 pm
by jvierra
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.

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

Posted: Tue Dec 12, 2017 4:58 pm
by jvierra
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 '' }

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

Posted: Wed Dec 13, 2017 4:24 am
by sekou2331
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