2

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

3 Answers 3

2
if (!$file -or ($file = ""))

should be replaced by

if (!$file -or ($file -eq ""))

You assign $file to an empty string in the first if clause and therefore your variable is empty in the Test-Path call.

Edit: Also there are some alternatives: How can I check if a string is null or empty in PowerShell?

you could either use

if([string]::IsNullOrEmpty($file))

or even just

if(!$file)
Sign up to request clarification or add additional context in comments.

3 Comments

Powershell uses -eq for the equals comparison. Not ==
Also, while it is true that initially an empty string is assigned to $file, $file is then immediately assigned one of two different values depending on the $Global:USE_LOCAL_SERVER variable. So it is not empty at the time that Test-Path is called.
But it is assigned to an empty string again in the first if-clause. But i can understand you confusion, my answer is not written clear enough
1

As others have mentioned, you are unintentionally assigning a blank string to $file in your first if (!$file ... statement. That is really the root of your problem.

However, instead of:

if (!$file -or ($file = ""))

You could use this forumula, which I find explains itself better:

if([String]::IsNullOrEmpty($file))

Comments

1

I would define a function Get-ConfigFile to retrieve the config and add a switch for local server:

function Get-ConfigFile
{
    Param(
        [switch]$UseLocalServer
    )

    $workingDir = (Get-Location).Path
    if ($UseLocalServer.IsPresent)
    {
         Join-Path $workingDir '\Configs\Localhost.ini'
    }
    else
    {
         Join-Path $workingDir ('\Configs\{0}.ini' -f $env:COMPUTERNAME)
    }
}

I would also use the Join-Path cmdlet to join a path instead of string concatenations.

Now you can retrive the config file path using:

$configFile = Get-ConfigFile -UseLocalServer:$Global:USE_LOCAL_SERVER

And if needed, ensure that the file exists:

if (-not(Test-Path -Path $configFile))
{
    throw [System.Exception] "Kan inte hitta ini fil."
}

Note: Get-Location will give you the current powershell path (working location), if you want to get the path where your script is located, use this instead:

$workingDir = split-path -parent $MyInvocation.MyCommand.Definitio

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.