Fr. Vandecan
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.MyCom mand.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
$ScriptDir = Split-Path $script:MyInvocation.MyCom
$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
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.Tr im()] = $value.Trim('" ')}
}
Function ReadParam
{
write-host $global:Parameters.strScan Folder
}
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.Tr
}
Function ReadParam
{
write-host $global:Parameters.strScan
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
suppose that $Global:Variables.Exchange Server = "Exchange-01.Farm.be"
and this sentence
Write-Host "Let's run. Exchange server $Global:Variables.Exchange Server is ready"
Why this sentence is displayed :
Let's run. Exchange server System.Collections.Hashtab le.Exchang eServer is ready
and not
Let's run. Exchange server "Exchange-01.Farm.be is ready"
and this sentence
Write-Host "Let's run. Exchange server $Global:Variables.Exchange
Why this sentence is displayed :
Let's run. Exchange server System.Collections.Hashtab
and not
Let's run. Exchange server "Exchange-01.Farm.be is ready"
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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('" ') }
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
}
ASKER
It's a a pleasure to share knowledge. I have some experience with PowerShell. With Experts Exchange I'm saving precious time!
Open in new window
Or you can just split manually:Open in new window