Link to home
Start Free TrialLog in
Avatar of Fr. Vandecan
Fr. VandecanFlag for Belgium

asked on

How to read UNC path on Ini file without having a evaluation of the string (and avoid run time error)

Unable to read inifile if content is a UNC path

$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$ScriptDir += "\parameters.ini"

# We use the "raw" parameter here in Get-Content so that when we get the contents
# of the file so that our hashtable is not converted to an object
$program = Get-Content -raw -Path $ScriptDir | ConvertFrom-StringData
write-host "`nType of the variable `$program`n"
$program.GetType()
write-host "`nPrinting `$program"
$program

-->

>> Running (Test.ps1) Script...
>> Platform: V5 64Bit (STA)
C:\Projects\parameters.ini
ERROR: ConvertFrom-StringData : parsing "\\Server\Scan\Folder" - Unrecognized escape sequence \S.

WHY ???
How to solve this ?

Tks
Parameters.ini
Avatar of oBdA
oBdA

The backslash is the 'escape' character for ConvertFrom-StringData; you'll need to escape every backslash with another backslash:
Company="CompanyCorp"
DefaultDerive="e:\\"
strScanFolder=\\\\Server\\Scan\\Folder

Open in new window

Or you can just split manually:
$parameters = @{}
Get-Content -Path .\parameters.ini | ForEach-Object {$key, $value = $_.Split('=', 2); $parameters[$key.Trim()] = $value.Trim('" ')}
$parameters

Open in new window

Avatar of Fr. Vandecan

ASKER

Great. working.
small addition question.

May I change this with something like this (I would like to have parameters global !

$global:parameters = @{}

Function ReadParamFromIniFile
{
Get-Content -Path .\parameters.ini | ForEach-Object {$key, $value = $_.Split('=', 2); $global:parameters[$key.Trim()] = $value.Trim('" ')}
}

Function ReadParam
{
write-host $global:Parameters.strScanFolder
}
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
suppose that $Global:Variables.ExchangeServer = "Exchange-01.Farm.be"

and this sentence
Write-Host "Let's run. Exchange server $Global:Variables.ExchangeServer is ready"

Why this sentence is displayed :
Let's run. Exchange server System.Collections.Hashtable.ExchangeServer is ready

and not
Let's run. Exchange server "Exchange-01.Farm.be is ready"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
May I ask a last question (not linked to the original question)?
I would like to avoid wrong content on the ini file.
This is my idea (but not OK) to take only lines containing a =

            Get-Content -Path $strFile | ForEach-Object {
                        $key, {
                              If ($_.IndexOf("=") -ne 0) { $value = $_.Split('=', 2); $Variables[$key.Trim()] = $value.Trim('" ') } Else {}
                        }
                  }

instead of
            Get-Content -Path $strFile | ForEach-Object {
                  $key,  { $value = $_.Split('=', 2); $Variables[$key.Trim()] = $value.Trim('" ') }
You just filter while reading:
Function Get-ParamFromIniFile {
Param(
	[string]$Path
)
	$ht = @{}
	Get-Content -Path $Path | Where-Object {$_.Contains('=')} | ForEach-Object {
		$key, $value = $_.Split('=', 2)
		$ht[$key.Trim()] = $value.Trim('" ')
	}
	Return $ht
}

Open in new window

It's a a pleasure to share knowledge. I have some experience with PowerShell. With Experts Exchange I'm saving precious time!