1

I use the following code to reference files in the current script directory

$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition

Then I can call it like

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)
}

This works fine. However, if I want to reference a file in a subdirectory like so

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)
}

it fails with error code 1639. If this doesn't work, how can I reference subdirectories when using $PSScriptRoot?

Pickle
  • 1,064
  • 9
  • 33
  • 51
  • We don't call PowerShell functions with parentheses - you should be saying `WriteLog "Installing..."` without `( )`. – Bill_Stewart Oct 23 '17 at 17:41

2 Answers2

0

Try not leaving spaces in your path. When you are building a string using a variable, you don't need to concatenate. It's not about referencing subfolders.

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)
}
Alfabravo
  • 7,493
  • 6
  • 46
  • 82
  • Try printing out `${PSScriptRoot}\test\InstallPrism6.msi` string... does it contain spaces? Also, "this doesn't work" means you got the same error or what? – Alfabravo Oct 23 '17 at 17:13
  • It returns "\test\InstallPrism6.msi" with no spaces. Error code 1619. – Pickle Oct 23 '17 at 17:16
  • Then check which version of PowerShell are you using and make sure you read [this answer](https://stackoverflow.com/questions/3667238/how-can-i-get-the-file-system-location-of-a-powershell-script#3667376) too, so your variable actually holds data. – Alfabravo Oct 23 '17 at 18:36
0

Note that the $PSScriptRoot variable is predefined on PowerShell 3.0 and newer, so you only need that variable if it's not already defined. I believe the proper syntax for what you want to do should look like the following:

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 is technically an array, and I embedded the " in case the path contains any whitespace.

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62