Easy Way to Read a Key Value From Ini With Powershell

Cloud Help Desk: Delays to incoming email:

I was told that an INI file could exist a simple text file with lawmaking in it. Instead of hardcoding, I would prefer to have PS read an INI file that has code that some other user can edit outside of my main PS script. This is my code:

# Check if cavalcade 5(E) is "0210" or "0227", if so write that row

Write-Output $COMPCODE $writeline

switch($COMPCODE)

{

'Company' { $writeline = $true } # Added to write header

'0210' { $writeline = $true }

'0227' { $writeline = $true }

}

Sad about inputting the lawmaking in like this. I remember I need to utilise another browser.

Is it but just pasting this code in a text file and change to extension to INI?

What cmdlet practice I use to read this external INI file?


M Boyle
Grand Boyle This person is a Verified Professional
This person is a verified professional.
Verify your account to enable IT peers to encounter that you are a professional.
November 30, 2017 at 21:09 UTC

Certain.

                                                    $dataLines                          =                          Get-Content                          'c:\data.txt'                          if                          (                          $dataLines                          -contains                          $COMPCODE                          )                          {                          $writeline                          =                          $true                          }                        

Starting time line loads the information in the file into $dataLines. This is created as an array (or drove).

The if statement uses the -contains operator to see if what is stored in $COMPCODE is an element in the collection.   If it is then $writeline is set to true.

I remember information technology as: $collection -contains $particular  or  $item -in $collection

It'southward worth saving the code in a modest file and playing with information technology so you understand it.

57 Replies

Gary M G
Gary K Grand This person is a Verified Professional
This person is a verified professional person.
Verify your account to enable IT peers to see that you are a professional person.
Nov 30, 2017 at 14:58 UTC

To read some file, use:

Go-content aka gc

So:

$fileContent = gc "\\SomeFQDN\C$\somePath\SomeFile.ini" # IF you have admin rights, you tin can read remotely

or

$fileContent = gc "C:\temp\myINIFile.ini"

trickyedq

What would the format of the INI file be, based on the code given higher up?

trickyedq

I was thinking I could only re-create n paste the lawmaking in the INI file and get PS to read it

trickyedq

COMPCODE has already been defined as such:

$COMPCODE = $canvass.Cells.Detail($i,five).text

trickyedq

I was thinking something in line like this:

                                                            # Cheque if column five(Eastward) is "0210" or "0227", if so write that row                              Write-Output                              "Reading .INI file"                              #Read INI file values                              Get-Content                              C                              :                              \                              TESTING                              \                              FULL_BRM_EXTRACT_TEST_ENV                              \                              Incoming                              \                              Extract_Filters                              .                              INI                              |                              Foreach                              -Object                              {                              $var                              =                              $_                              .                              Split                              (                              '='                              )                              New-Variable                              -Name                              $var                              [                              0                              ]                              -Value                              $var                              [                              1                              ]                              }                              #Write-Output $COMPCODE  $writeline                                                            #switch($COMPCODE)                              #{                              #    'Company'   { $writeline = $true }  # Added to write header                              #    '0210'      { $writeline = $truthful }                              #    '0227'      { $writeline = $truthful }                              #}                              Write-Output                              $COMPCODE                              $writeline                            

 Im not quite sure how this volition actually read an INI file, nor understand the format of the data in the INI file. I institute this lawmaking online and remember it's a start. Whatever and all help is highly appreciated!

M Boyle
M Boyle This person is a Verified Professional
This person is a verified professional person.
Verify your account to enable Information technology peers to see that y'all are a professional person.
Nov xxx, 2017 at eighteen:16 UTC

Perhaps you should start with what you lot are trying to do.

I was told that an INI file could be a elementary text file with lawmaking in information technology. Instead of hardcoding, I would adopt to have PS read an INI file that has code that another user can edit outside of my main PS script.

An ini file would await something similar this:

                                [PostgreSQL] Description	= ODBC for PostgreSQL Driver		= /usr/lib/psqlodbcw.so FileUsage	= ane  [MySQL] Clarification	= ODBC for MySQL Driver		= /usr/lib/libmyodbc5.and so Setup		= /usr/lib/libodbcmyS.so                              

It generally won't have lawmaking in it.

It is besides not a good idea to execute code from an external file every bit yous accept no idea what ti could do. This is fifty-fifty more of import if the original script is run with elevated permissions.

trickyedq

@Boyle........mayhap I used the incorrect words. I only desire the INI file to concord these values

                                                            'Visitor'                              {                              $writeline                              =                              $truthful                              }                              # Added to write header                              #    '0210'      { $writeline = $true }                              #    '0227'      { $writeline = $true }                            

 Are you proverb this is non possible?

M Boyle
M Boyle This person is a Verified Professional
This person is a verified professional person.
Verify your account to enable It peers to run into that you are a professional.
Nov xxx, 2017 at 19:09 UTC

Hmm, I'm still not entrely certain I understand what you lot are trying to practice. And then let's see:

  • Yous are reading data from an excel spreadsheet (a item cell)
  • if the data read is 1 of a number of items (that you'd like to supply in a file) then yous want to output something by setting: $writeline = $true

That it?

If so, why non just have a simple text file containing the values (one value per line) and read it in with Become-Content?

Something like:

                                                                $dataLines                                =                                Become-Content                                'c:\data.txt'                                if                                (                                $dataLines                                -contains                                $COMPCODE                                )                                {                                $writeline                                =                                $true                                }                              
trickyedq

That's pretty elementary! In the code I give, just if the Company is either "0210" or "0227", information technology will write that row of data. How will the INI file await, based on the lawmaking I've given in a higher place?

M Boyle
Chiliad Boyle This person is a Verified Professional
This person is a verified professional person.
Verify your account to enable IT peers to see that you are a professional.
Nov 30, 2017 at 20:26 UTC

This is where information technology tin get circuitous, will there be unlike choices or volition it only always be Company?

Having multiple numbers to search confronting is simple if they only ever relate to Company.

But I go the feeling information technology'southward not that simple.

trickyedq

It will e'er be COMPANY. The only purpose of 'COMPANY' is for header purposes. The whole purpose of the above script is to only pull in rows that have the visitor of 0210 and 0227. These number only chronicle to company.

M Boyle
M Boyle This person is a Verified Professional
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
November 30, 2017 at 20:50 UTC

Absurd.

And then the code I posted above will read from a uncomplicated text file that has each number on separates lines:

zippo more than complex than that.

trickyedq

Outstanding! Can you delight explain your code and how it works? I just want a full understanding for my development

M Boyle
K Boyle This person is a Verified Professional
This person is a verified professional.
Verify your business relationship to enable Information technology peers to see that you are a professional person.
Nov 30, 2017 at 21:09 UTC

Sure.

                                                                $dataLines                                =                                Become-Content                                'c:\data.txt'                                if                                (                                $dataLines                                -contains                                $COMPCODE                                )                                {                                $writeline                                =                                $truthful                                }                              

First line loads the data in the file into $dataLines. This is created as an array (or collection).

The if argument uses the -contains operator to meet if what is stored in $COMPCODE is an element in the collection.   If it is then $writeline is set to truthful.

I remember it equally: $collection -contains $detail  or  $item -in $drove

It's worth saving the code in a pocket-sized file and playing with it so you understand it.

trickyedq

Fantabulous!!! Thank yous very much. I'll add comments in the INI file ( which I recollect is ';' )

M Boyle
M Boyle This person is a Verified Professional
This person is a verified professional.
Verify your business relationship to enable IT peers to see that you are a professional person.
Nov thirty, 2017 at 22:36 UTC

Get-content won't intendance or fifty-fifty understand about comments. It reads the entire file and processes every line.

Basically I don't think you demand to bother with an ini file. A obviously text file will do the job just fine.

trickyedq

@Boyle.........now I am told that the INI file volition contain more than just ane filter, and that I demand the ini file to have a different filter on each row. Correct at present, I accept the file similar this:

0210
0227

but he wants information technology like this:

0210|0227

So that I can add together more than filters in the futurity

M Boyle
M Boyle This person is a Verified Professional
This person is a verified professional.
Verify your account to enable It peers to see that you are a professional person.
December 1, 2017 at 15:27 UTC

Hmm, hard to offering communication every bit I don't know what you re trying to practice.

Do this magic file need to be an ini file or is that simply one options?

You're better off sitting down with the person ofering yous this "communication" and asking them what the heck they are talking about.

If I wanted to store multiple data I may use a modified csv file:

                                term,values COMPANY,0210|0227 SEARCHTERM,2345|2356|2458 OTHERTERM,1111                              

but information technology would depend on the end goal.

trickyedq

Well I guess to start is there whatsoever fashion to go information technology to read in a row instead of different rows?

trickyedq

Really what yous are suggesting is basically the same thing. Do I need to modify my existing code other than changing the file extension? Here is my lawmaking:

                                                            Write-Output                              "Reading .INI file"                              #Read INI file values                              $CoCodes                              =                              Get-Content                              'C:\TESTING\test\Incoming\Filters.ini'                              if                              (                              $CoCodes                              -contains                              $COMPCODE                              )                              {                              $writeline                              =                              $true                              }                            

Also when I add other filters, how is it going to know which line is for a certain filter?

trickyedq

The terminate goal is to have several different filters that other users can alter to their taste instead of me having to alter it hardcoded.

For starters, how can I get information technology to read by row instead of it reading vertically?

M Boyle
Thousand Boyle This person is a Verified Professional
This person is a verified professional.
Verify your account to enable It peers to see that you are a professional.
Dec 1, 2017 at 18:x UTC

                                                                    Write-Output                                  "Reading .INI file"                                  #Read INI file values                                  $CoCodes                                  =                                  Get-Content                                  'C:\TESTING\examination\Incoming\Filters.ini'                                  if                                  (                                  $CoCodes                                  -contains                                  $COMPCODE                                  )                                  {                                  $writeline                                  =                                  $true                                  }                                

Also when I add other filters, how is it going to know which line is for a certain filter?

This code won't work because of multiple filters.

Information technology also won't be an ini file.

trickyedq

How tin I get it to work to read by the row?

I'thou thinking it works by reading a certain word assigned to each filter, but Im lost. I empathise the way it is at present.......barely. This INI stuff is new to me, but if yu thinking a csv is easier Ill gyre with that

trickyedq

What you lot gave above:

                                                            COMPANY                              ,                              0210                              |                              0227                              SEARCHTERM                              ,                              2345                              |                              2356                              |                              2458                              OTHERTERM                              ,                              1111                            

is exactly how I would need it but how exercise I convert my current code to work with this. This is what I have and so far:

                                                            Write-Output                              "Reading .INI file"                              $COMPCODE                              =                              $sheet                              .                              Cells                              .                              Particular                              (                              $i                              ,                              5                              ).                              text                              #Read INI file values                              $CoCodes                              =                              Get-Content                              'C:\TESTING\test\Incoming\Filters.ini'                              if                              (                              $CoCodes                              -contains                              $COMPCODE                              )                              {                              $writeline                              =                              $true                              }                            

This topic has been locked by an administrator and is no longer open for commenting.

To go on this word, please ask a new question.

williamslabon1937.blogspot.com

Source: https://community.spiceworks.com/topic/2093602-using-an-ini-file-in-powershell

0 Response to "Easy Way to Read a Key Value From Ini With Powershell"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel