如何处理 powershell 中 $PSScriptRoot 路径中的空格

How do I deal with Spaces in the $PSScriptRoot path in powershell

我正在尝试编写一个 powershell 脚本,它将使用 Installcfg.cfg 文件安装 Java。我希望它能够 运行 无论脚本在哪里执行。当任何文件路径中没有空格时它工作正常但是当我从另一个位置 运行 它时我收到以下错误:

以下开关存在错误: "INSTALLCFG='C:\Program";"Files";"(x86\DesktopCentral_Agent\swrepository\swuploades\Java";"8";"Update";"25\InstallCFG.cfg'";.

Powershell 似乎没有按照我的意愿传递文件路径。

脚本如下:

#Determine the Architecture
$Arch = (Get-WmiObject Win32_OperatingSystem).OSArchitecture

#Make sure no browsers are running
kill -processname iexplore, chrome, firefox

#Install based on Architecture
if ($Arch -eq "64-bit")
    {
    Start-Process jre-8u25-windows-x64.exe -wait -args "INSTALLCFG=$PSScriptRoot\InstallCFG.cfg /L c:\temp\java64.txt"
    Start-Process jre-8u25-windows-i586.exe -wait -args "INSTALLCFG=$PSScriptRoot\InstallCFG.cfg /L c:\temp\java32.txt"
    }

else 
    {
    Start-Process jre-8u25-windows-i586.exe -args "INSTALLCFG=$PSScriptRoot\InstallCFG.cfg /L c:\temp\java32.txt"
    }

我尝试了各种方法在参数中包含引号,甚至将参数创建为变量。我不想对位置进行硬编码,因为我希望文件从任何地方 运行。

我基本上是在尝试 运行 从 powershell 中的批处理文件中执行以下命令:

"%~dp0jre-8u25-windows-x64.exe" INSTALLCFG="%~dp0InstallCFG.cfg" /L c:\temp\log.txt

我错过了什么?

转义字符是反引号。你应该能够写出类似

的东西
"INSTALLCFG=`"$PSScriptRoot\InstallCFG.cfg`" /L c:\temp\java64.txt"

对于 args 参数。