Easy Way to Read a Key Value From Ini With Powershell
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?
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
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"
What would the format of the INI file be, based on the code given higher up?
I was thinking I could only re-create n paste the lawmaking in the INI file and get PS to read it
COMPCODE has already been defined as such:
$COMPCODE = $canvass.Cells.Detail($i,five).text
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!
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.
@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?
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 } 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?
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.
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.
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.
Outstanding! Can you delight explain your code and how it works? I just want a full understanding for my development
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.
Fantabulous!!! Thank yous very much. I'll add comments in the INI file ( which I recollect is ';' )
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.
@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
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.
Well I guess to start is there whatsoever fashion to go information technology to read in a row instead of different rows?
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?
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?
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.
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
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 } 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