简体   繁体   中英

Calling one powershell script from another with named parameters and whitespaces in path

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.

The problem is the way you are entering the parmeter:

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

settingsFile is aa 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"""

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"""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM