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

Stanislav Stoyanov
  • 2,082
  • 2
  • 20
  • 22

3 Answers3

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)
Community
  • 1
  • 1
Sosian
  • 622
  • 11
  • 28
  • 1
    Powershell uses -eq for the equals comparison. Not == – EBGreen May 09 '16 at 13:37
  • 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. – EBGreen May 09 '16 at 13:42
  • 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 – Sosian May 09 '16 at 13:45
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))
Charlie Joynt
  • 4,411
  • 1
  • 24
  • 46
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
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172