I have the following Powershell code:
function readConfigData
{
$workingDir = (Get-Location).Path
$file = ""
if ($Global:USE_LOCAL_SERVER)
{
$file = $workingDir + '\Configs\Localhost.ini'
}
else
{
$file = $workingDir + '\Configs\' + $env:COMPUTERNAME + '.ini'
}
Write-Host 'INIFILE: ' $file
if (!$file -or ($file = ""))
{
throw [System.Exception] "Ini fil är inte satt."
}
if (!(Test-Path -Path $file))
{
throw [System.Exception] "Kan inte hitta ini fil."
}
}
readConfigData
How should I declare the local variable $file
that can be passed to the function Test-Path
.
My local variable $file get populated but then when I place it as argument to other function it's like it is out of scope.
I read the about scopes article but wasn't able to figure it out.
Currently I get the error:
INIFILE: D:\Projects\scripts\Configs\HBOX.ini Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string. At D:\Projects\freelancer.com\nero2000\cmd script to powershell\script.ps1:141 char:27 + if (!(Test-Path -Path $file)) + ~~~~~ + CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand