0

I have already checked the possible duplicates of this question but I was not able to fix my problem with the given answers.

I have a script install.ps1 with one named parameter -settingsFile

    param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$settingsFile
    )
    Write-Host "calling $PSCommandPath with settings file $settingsFile"
    If (!(Test-Path $settingsFile)){
        Write-Host "cannot load settings[ $settingsFile ], aborting "
        exit
    }
...
...

From another powershell script I want to call this, but the given $settingsFile will not be found. Here is the caller script

    Write-Host "BEGIN-------------------------------------------------------------"

    $configFile = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "..\install-data\settings.json"))
    Write-Host ""$configFile""
    If (Test-Path $configFile){
        Write-Host "config found"
    }
    else{
        Write-Host "config not found"
    }

    $argument = "-settingsFile=""$configFile"""
    Write-Host "$argument"
    $installFps = $($PSScriptRoot + "\scripts\install-fps\install-fps.ps1")
    Write-Host """$installFps""" $argument
    Write-Host "case 1: $installFps $argument"
    & $installFps $argument
    Write-Host "END-------------------------------------------------------------"

And here the output

    BEGIN-------------------------------------------------------------
    C:\Program Files\foo\install-data\settings.json
    config found
    -settingsFile="C:\Program Files\foo\install-data\settings.json"
    "C:\Program Files\foo\install-data\scripts\install-fps\install-fps.ps1" -settingsFile="C:\Program Files\foo\install-data\settings.json"
    case 1: C:\Program Files\foo\install-data\scripts\install-fps\install-fps.ps1 -settingsFile="C:\Program Files\foo\install-data\settings.json"
    calling C:\Program Files\foo\install-data\scripts\install-fps\install-fps.ps1 with settings file -settingsFile="C:\Program Files\foo\install-data\settings.json"
    cannot load settings[ -settingsFile="C:\Program Files\foo\install-data\settings.json" ], aborting
    END-------------------------------------------------------------

When I call the script manually from the powershell command line it works, but when I am using it with this script it does not work. The settingsFile does exist, this I can ensure. It seems that my parameter is not correctly transported into the install.ps1 script, because in my log out I can see the the complete string "-settingsFile="C:\Program Files\foo\install-data\settings.json" will be transported, not only the filepath "C:\Program Files\foo\install-data\settings.json" I need to add always the double quotes because of the possible whitespace in the path.

I have tried several approaches but does not work.

2
  • You could try escaped quotes nstead of triple quotes: $argument = "-settingsFile=`"$configFile`"" Commented Nov 28, 2018 at 14:17
  • Tried it but resolved in the same result Commented Nov 28, 2018 at 14:20

2 Answers 2

2

The problem is the way you are entering the parmeter:

$argument = "-settingsFile=""$configFile"""

settingsFile is a a Named parameter but you are including an = between the param name and value, which is incorrect syntax.

This is causing powershell to interpret it as a single string of -settingsFile="C:\Program Files\foo\install-data\settings.json" and sending this to the parameter at position 1.

Which is like calling this:

-settingsFile '-settingsFile="C:\Program Files\foo\install-data\settings.json"'

This is shown by the param name appearing in the error message:

cannot load settings[ -settingsFile="C:\Program Files\foo\install-data\settings.json" ], aborting

It's easy to fix, remove the =:

$argument = "-settingsFile ""$configFile"""
Sign up to request clarification or add additional context in comments.

2 Comments

I changed it like you said but its still not working. The log output is: cannot load settings[ -settingsFile "C:\Program Files\PaperDynamix\install-data\settings.json" ], aborting So its still interpreted as one parameter but now without the equal sign. Any idea?
Without knowing what's going on in install-fps.ps1 I can't diagnose any further, can you update your question with this please.
1

PowerShell has decided that your settingsFile variable has the value 'settingsFile="C:\Program Files\foo\install-data\settings.json"'. This will be because of the equals sign. It's treating it as a positional parameter instead of a named one.

Try replacing the equals sign with a colon or space.

$argument = "-settingsFile:""$configFile"""

$argument = "-settingsFile ""$configFile"""

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.