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.
$argument = "-settingsFile=`"$configFile`""