SLS context

Ask your 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.
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

SLS context

Post by sekou2331 »

I have file that looks like the below. I am trying to get everything after a certain line. Now the file can have 3 things or 20 after the line. I tried to use the context option but I would have to know how much is under what i am selecting. How do I get everything under the selected string. I trying to get all the data under data2 and make key value pair. Please see the code that i have. It will work if i select off of [info] because i am breaking off of [info2].

  1. $line = Get-Section -Filename .\Settings.txt | sls -Pattern "[Info2]"
  2. $KeyPairData =@()
  3. foreach ($element in $line.line )
  4. {
  5.     if ($element -eq "[info2]") { break }
  6.    
  7.     $a = $element.Replace("[info2]", "") | ConvertFrom-StringData
  8.     $KeyPairData += $a
  9.    
  10. }
  11.  
  12. $KeyPairData



  1. [Info]
  2. 3= data1
  3. 2= data2
  4. 4 =data3
  5.  
  6. [info2]
  7. 1 =data1
  8. 2 = data2
  9. 3 =data3
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SLS context

Post by jvierra »

It is an INI file. You can use INI file calls to extract anything.

Here is a module that can read a whole section by name:

Get-ProfileSection -iniFile $filename -Section info2
Attachments
INIProfileManager.zip
(4.79 KiB) Downloaded 132 times
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: SLS context

Post by sekou2331 »

Ok I am a little confused so bare with me. Are you saying I should create a ini file and have the attached read it. If so what should be in the ini file and what is the format. I tried to look this up because it is foreign to me. I am basically trying to create a config file.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SLS context

Post by jvierra »

The file you are trying to read appears to be an INI file even if it does not have that extension. "config" files are usually XML.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: SLS context

Post by sekou2331 »

I created it like that. I am trying find the best way to make a config file that my script can read and use the data.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SLS context

Post by jvierra »

The easiest way is to use CliXML.

Help Export-CliXml -full
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: SLS context

Post by sekou2331 »

Can we go back to my original question. I just want to select everything after selected string.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SLS context

Post by jvierra »

Just detect the string in each line and output everything after it. [

if($found){$line}
if($line -match 'info2'){$found = $true}
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: SLS context

Post by sekou2331 »

I am sorry but how would this work. Wouldn't I to do a foreach? Also how would I get everything after the [info2]?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SLS context

Post by jvierra »

The code outputs nothing until after the required line is found.
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked