Comments (5)
If you are using the lastest version, the value should be available at $out["_"]["Server"]
(the default NoSection is _
)
Please let me know or close this issue if this fixed the problem
Also, you are missing a closing "
in your sample code
from psini.
Thank you for the quick response and my apologies for missing the closing double quote.
If i import the PsIni module i am able to read the INI.
Sample Code:
Import-Module -Name PsIni
$Svr = $null
$Port = $null
$Val = Get-IniContent "C:\Temp\test\test\Server.ini" -Verbose -ErrorAction SilentlyContinue
$Svr = $Val[""]["SERVER"]
$Port = $Val[""]["Port"]
Write-Host "Server: $Svr"
Write-Host "Port: $Port"
Results:
VERBOSE: Get-IniContent:: Function started
VERBOSE: Get-IniContent:: Processing file: C:\Temp\test\test\Server.ini
VERBOSE: Get-IniContent:: Adding key SERVER with value: "10.10.10.10"
VERBOSE: Get-IniContent:: Adding key Port with value: 8080
VERBOSE: Get-IniContent:: Finished Processing file: C:\Temp\test\test\Server.ini
VERBOSE: Get-IniContent:: Function ended
Server: "10.10.10.10"
Port: 8080
If i try to just the Get-IniContent function without the module, it fails to read the INI.
Sample Code:
`Set-StrictMode -Version Latest
Function Get-IniContent {
<#
.Synopsis
Gets the content of an INI file
.Description
Gets the content of an INI file and returns it as a hashtable
.Notes
Author : Oliver Lipkau <[email protected]>
Source : https://github.com/lipkau/PsIni
http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91
Version : 1.0.0 - 2010/03/12 - OL - Initial release
1.0.1 - 2014/12/11 - OL - Typo (Thx SLDR)
Typo (Thx Dave Stiff)
1.0.2 - 2015/06/06 - OL - Improvment to switch (Thx Tallandtree)
1.0.3 - 2015/06/18 - OL - Migrate to semantic versioning (GitHub issue#4)
1.0.4 - 2015/06/18 - OL - Remove check for .ini extension (GitHub Issue#6)
1.1.0 - 2015/07/14 - CB - Improve round-tripping and be a bit more liberal (GitHub Pull #7)
OL - Small Improvments and cleanup
1.1.1 - 2015/07/14 - CB - changed .outputs section to be OrderedDictionary
1.1.2 - 2016/08/18 - SS - Add some more verbose outputs as the ini is parsed,
allow non-existent paths for new ini handling,
test for variable existence using local scope,
added additional debug output.
#Requires -Version 2.0
.Inputs
System.String
.Outputs
System.Collections.Specialized.OrderedDictionary
.Parameter FilePath
Specifies the path to the input file.
.Parameter CommentChar
Specify what characters should be describe a comment.
Lines starting with the characters provided will be rendered as comments.
Default: ";"
.Parameter IgnoreComments
Remove lines determined to be comments from the resulting dictionary.
.Example
$FileContent = Get-IniContent "C:\myinifile.ini"
-----------
Description
Saves the content of the c:\myinifile.ini in a hashtable called $FileContent
.Example
$inifilepath | $FileContent = Get-IniContent
-----------
Description
Gets the content of the ini file passed through the pipe into a hashtable called $FileContent
.Example
C:\PS>$FileContent = Get-IniContent "c:\settings.ini"
C:\PS>$FileContent["Section"]["Key"]
-----------
Description
Returns the key "Key" of the section "Section" from the C:\settings.ini file
.Link
Out-IniFile
#>
[CmdletBinding()]
[OutputType(
[System.Collections.Specialized.OrderedDictionary]
)]
Param(
[ValidateNotNullOrEmpty()]
[Parameter(ValueFromPipeline=$True,Mandatory=$True)]
[string]$FilePath,
[char[]]$CommentChar = @(";"),
[switch]$IgnoreComments
)
Begin
{
Write-Debug "PsBoundParameters:"
$PSBoundParameters.GetEnumerator() | ForEach-Object { Write-Debug $_ }
if ($PSBoundParameters['Debug']) { $DebugPreference = 'Continue' }
Write-Debug "DebugPreference: $DebugPreference"
Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"
$commentRegex = "^([$($CommentChar -join '')].*)$"
Write-Debug ("commentRegex is {0}." -f $commentRegex)
}
Process
{
Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath"
$ini = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
if (!(Test-Path $Filepath))
{
Write-Verbose ("Warning: `"{0}`" was not found." -f $Filepath)
return $ini
}
$commentCount = 0
switch -regex -file $FilePath
{
"^\s*\[(.+)\]\s*$" # Section
{
$section = $matches[1]
Write-Verbose "$($MyInvocation.MyCommand.Name):: Adding section : $section"
$ini[$section] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
$CommentCount = 0
continue
}
$commentRegex # Comment
{
if (!$IgnoreComments)
{
if (!(test-path "variable:local:section"))
{
$section = $script:NoSection
$ini[$section] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
}
$value = $matches[1]
$CommentCount++
Write-Debug ("Incremented CommentCount is now {0}." -f $CommentCount)
$name = "Comment" + $CommentCount
Write-Verbose "$($MyInvocation.MyCommand.Name):: Adding $name with value: $value"
$ini[$section][$name] = $value
}
else { Write-Debug ("Ignoring comment {0}." -f $matches[1]) }
continue
}
"(.+?)\s*=\s*(.*)" # Key
{
if (!(test-path "variable:local:section"))
{
$section = $script:NoSection
$ini[$section] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
}
$name,$value = $matches[1..2]
Write-Verbose "$($MyInvocation.MyCommand.Name):: Adding key $name with value: $value"
$ini[$section][$name] = $value
continue
}
}
Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath"
Return $ini
}
End
{Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}
}
Set-Alias gic Get-IniContent
$Svr = $null
$Port = $null
$Val = Get-IniContent "C:\Temp\test\test\Server.ini" -Verbose -ErrorAction SilentlyContinue
$Svr = $Val[""]["SERVER"]
$Port = $Val[""]["Port"]
Write-Host "Server: $Svr"
Write-Host "Port: $Port"
'
Results:
VERBOSE: Get-IniContent:: Function started
VERBOSE: Get-IniContent:: Processing file: C:\Temp\test\test\Server.ini
VERBOSE: Get-IniContent:: Adding key SERVER with value: "10.10.10.10"
VERBOSE: Get-IniContent:: Adding key Port with value: 8080
VERBOSE: Get-IniContent:: Finished Processing file: C:\Temp\test\test\Server.ini
VERBOSE: Get-IniContent:: Function ended
Cannot index into a null array.At line:166 char:1
- $Svr = $Val["_"]["SERVER"]
-
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray
Cannot index into a null array.At line:167 char:1
- $Port = $Val["_"]["Port"]
-
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray
Server:
Port:
Am i able to use the function without importing the module?
from psini.
INI content:
SERVER = "10.10.10.10"
Port=8080
from psini.
Disregard, i found out why. I see the $script:NoSection = "_" entry in the PsIni.psm1.
If i add that code just below the Get-IniContent function, I am able to get the desired results.
Thank you for creating this fantastic module. It is extremely useful to me.
-Kevin
from psini.
Glad to hear that. :-)
from psini.
Related Issues (20)
- Quotes stripped and trimming HOT 20
- If values are double in one section the Write operation add mistakes
- Installation of v3.1.2 fails HOT 2
- 30 seconds for processing 5MB ini file
- Files edited with PsIni can't be read with GetPrivateProfileString HOT 1
- File with [ ] in path doesn't seem to work as expected. HOT 1
- PsIni doesn't work with C:\ProgramData\Microsoft\Network\Connections\Pbk\rasphone.pbk HOT 5
- Can't read Vmware Workstation .vmx HOT 1
- Please allow duplicate keys HOT 2
- Latest tag v3.1.2 is pointing to version 3.1.0
- Submission: ConvertFrom-Ini and ConvertTo-Ini
- Writing new section and values? HOT 1
- Cant handle keys without value (e.g. winpeshl.ini) HOT 1
- -IgnoreComments do not work HOT 1
- Out-IniFile: UTF-8 without BOM HOT 5
- Can't save as ANSI or save characte — (dash em) HOT 1
- Wrong order in Out-IniFile HOT 1
- Importing the module HOT 1
- Quotation marks that encapsulate values are truncated
- Can't get all section names from big ini file HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from psini.