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
François VDCIcT service DeskAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
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

0
François VDCIcT service DeskAuthor Commented:
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
}
0
oBdACommented:
You may do with it whatever works for you ...
But to properly encapsulate that into a function, you should do something like below.
And function names should follow the <Verb>-<Noun> convention, with <verb> coming from the list obtained by the cmdlet "Get-Verb".
Function Get-ParamFromIniFile {
Param(
	[string]$Path
)
	$ht = @{}
	Get-Content -Path $Path | ForEach-Object {
		$key, $value = $_.Split('=', 2)
		$ht[$key.Trim()] = $value.Trim('" ')
	}
	Return $ht
}

$Global:parameters = Get-ParamFromIniFile -Path .\parameters.ini

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

François VDCIcT service DeskAuthor Commented:
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"
0
oBdACommented:
Because the variable is part of a string, and only "plain" variables can be evaluated immediately inside a string. You're accessing properties of an object here, so you need to use a subexpression $()
Write-Host "Let's run. Exchange server $($Global:Variables.ExchangeServer) is ready"

Open in new window

0
François VDCIcT service DeskAuthor Commented:
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('" ') }
0
oBdACommented:
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

0
François VDCIcT service DeskAuthor Commented:
It's a a pleasure to share knowledge. I have some experience with PowerShell. With Experts Exchange I'm saving precious time!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.