如何处理powershell中$ PSScriptRoot路径中的Spaces

时间:2015-04-14 15:17:01

标签: java powershell path spaces

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

以下开关中存在错误: " INSTALLCFG =' C:\程序&#34 ;;"文件&#34 ;;"(86 \ DesktopCentral_Agent \ swrepository \ 1 \ swuploades \爪哇&#34 ;;&#34 8&#34 ;;"更新&#34 ;;" 25 \ InstallCFG.cfg'&#34 ;;

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

我错过了什么?

1 个答案:

答案 0 :(得分:3)

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

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

用于args参数。