引用当前目录和子文件夹的Powershell

引用当前目录和子文件夹的Powershell,powershell,Powershell,我使用以下代码引用当前脚本目录中的文件 $PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition 然后我可以这样称呼它 try { WriteLog("Installing...") $installresult = (Start-Process msiexec.exe -ArgumentList "/i $PSScriptRoot\InstallPrism6.msi /qn /norestart"

我使用以下代码引用当前脚本目录中的文件

$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
然后我可以这样称呼它

try {
WriteLog("Installing...")
$installresult = (Start-Process msiexec.exe -ArgumentList "/i 
$PSScriptRoot\InstallPrism6.msi /qn /norestart" -Wait -PassThru).ExitCode
WriteLog("Installation finished with return code: $installresult")
}
catch {
    WriteLog($_.Exception.Message)
}
这个很好用。但是,如果我想引用子目录中的文件,比如

try {
WriteLog("Installing...")
$installresult = (Start-Process msiexec.exe -ArgumentList "/i $PSScriptRoot 
+ \test\InstallPrism6.msi /qn /norestart" -Wait -PassThru).ExitCode
WriteLog("Installation finished with return code: $installresult")
}
catch {
    WriteLog($_.Exception.Message)
}
它失败,错误代码为1639。
如果这不起作用,我如何在使用$PSScriptRoot时引用子目录?

请尝试不要在路径中留下空格。当您使用变量构建字符串时,不需要连接。这与引用子文件夹无关

try {
WriteLog("Installing...")
$installresult = (Start-Process msiexec.exe -ArgumentList "/i ${PSScriptRoot}\test\InstallPrism6.msi /qn /norestart" -Wait -PassThru).ExitCode
WriteLog("Installation finished with return code: $installresult")
}
catch {
    WriteLog($_.Exception.Message)
}

尽量不要在路径中留下空格。当您使用变量构建字符串时,不需要连接。这与引用子文件夹无关

try {
WriteLog("Installing...")
$installresult = (Start-Process msiexec.exe -ArgumentList "/i ${PSScriptRoot}\test\InstallPrism6.msi /qn /norestart" -Wait -PassThru).ExitCode
WriteLog("Installation finished with return code: $installresult")
}
catch {
    WriteLog($_.Exception.Message)
}

请注意,
$PSScriptRoot
变量是在PowerShell 3.0及更高版本上预定义的,因此只有在尚未定义该变量时才需要该变量。我相信您要执行的操作的正确语法应该如下所示:

if ( -not $PSScriptRoot ) {
  $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
}

try {
  WriteLog "Installing..."
  $installresult = (Start-Process msiexec.exe -ArgumentList "/i","`"$PSScriptRoot\test\InstallPrism6.msi`"","/qn","/norestart" -Wait -PassThru).ExitCode
  WriteLog "Installation finished with return code: $installresult"
}
catch {
  WriteLog $_.Exception.Message
}

-ArgumentList
技术上是一个数组,我嵌入了
以防路径包含任何空格。

请注意,
$PSScriptRoot
变量是在PowerShell 3.0及更高版本上预定义的,因此您只需要尚未定义该变量。我相信您要执行的操作的正确语法应该如下所示:

if ( -not $PSScriptRoot ) {
  $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
}

try {
  WriteLog "Installing..."
  $installresult = (Start-Process msiexec.exe -ArgumentList "/i","`"$PSScriptRoot\test\InstallPrism6.msi`"","/qn","/norestart" -Wait -PassThru).ExitCode
  WriteLog "Installation finished with return code: $installresult"
}
catch {
  WriteLog $_.Exception.Message
}

-ArgumentList
从技术上讲是一个数组,我嵌入了
,以防路径包含任何空格。

试着打印出
${PSScriptRoot}\test\InstallPrism6.msi
字符串…它是否包含空格?另外,“这不起作用”意味着你得到了相同的错误还是什么?它返回“\test\InstallPrism6.msi”“没有空间。错误代码1619。然后检查您正在使用哪个版本的PowerShell,并确保您也读取了,因此您的变量实际上保存了数据。请尝试打印出
${PSScriptRoot}\test\InstallPrism6.msi
字符串。。。它包含空格吗?另外,“这不起作用”意味着您遇到了相同的错误或什么?它返回“\test\InstallPrism6.msi”,不带空格。错误代码1619。然后检查您正在使用哪个版本的PowerShell,并确保您也读取了,因此您的变量实际上保存了数据。我们不使用括号调用PowerShell函数-您应该说
WriteLog“Installing…”
,而不使用
()
。我们不使用括号调用PowerShell函数-您应该在没有
()
的情况下说
写下“安装…”