• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 52
  • Last Modified:

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
0
François VDC
Asked:
François VDC
  • 4
  • 4
2 Solutions
 
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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now