Target Error Creating shortcut on 64-bit Windows

by cre8tivspirit at 2012-08-21 07:48:04

We are changing the developer workstations over to 64-bit Windows 7 operating systems and I’ve discovered an error in my install scripts that didn’t happen in the 32-bit OS. Below are the variables and the script being used to create the shortcut;

$ALL_DDT = ‘D:\Users\Public\Desktop\Developer Desktop Tools’
$PF = ‘C:\Program Files’
$TL_NM = ‘Mozilla FireFox’
$TL_EXE = ‘firefox.exe’
$TL_VER = ‘12.0’
$TL_Target = ’ -profile C:\DevDskTop\mozilla_firefox\12.0’
$TL = $PF + '' + $TL_NM + ‘_’ + $TL_VER
$TL_Pro = 'C:\DevDskTop\mozilla_firefox\12.0'
$TL_ICON = $TL + '' + $TL_EXE


$objShell = New-Object -com "Wscript.Shell"
$objShortCut = $objShell.CreateShortcut($ALL_DDT + "\Mozilla FireFox_12.0.LNK")
$objShortCut.TargetPath = $TL + '' + $TL_EXE + $TL_Target
$objShortCut.IconLocation = $TL_ICON
$objShortCut.Save()

on a 32-bit OS, the shortcut path looks like this;
"C:\Program Files\Mozilla FireFox_12.0\firefox.exe" -profile C:\DevDskTop\mozilla_firefox\12.0


On the 64-bit OS, I’m getting the following error;

Exception setting "TargetPath": "The parameter is incorrect. (Exception from HR
ESULT: 0x80070057 (E_INVALIDARG))"
At J:\PowerShell_Scripts\DevDskTopTools_Build.ps1:1603 char:15
+ $objShortCut. <<<< TargetPath = $TL + '' + $TL_EXE + $TL_Target
+ CategoryInfo : InvalidOperation: (:slight_smile: , RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
by DonJ at 2012-08-21 08:02:11
Have you checked to make sure the path is the same on a 64-bit system? It’s often something like "Program Files (x86)" or something, isn’t it?
by cre8tivspirit at 2012-08-21 08:15:44
Yes, there are two different "Program Files" directories on a 64-bit system but I double checked and the software is definitely sitting in the directory that the shortcut is pointing to.
by poshoholic at 2012-08-21 08:22:36
If it’s not an issue with Firefox being in the Program Files (x86) path, then use Test-Path in your script to validate your assumptions.

Once you have your paths configured, you should make sure that the path is valid like this:

[script=powershell]$firefoxExePath = $TL + '&#39; + $TL_EXE
if (-not (Test-Path -LiteralPath $firefoxExePath)) {
throw "Could not find firefox.exe at path $firefoxExePath".
}[/script]

That will throw an exception specifically if the firefox.exe file is not where you expect it to be.

Also, you can look up the Firefox install path from the Registry instead of assuming it’s installed on the C drive under Program Files. That would be a more robust approach. See this article to find out how you can get the path you need by looking up the version and then using the version in a path to look up the Install Directory value.
by cre8tivspirit at 2012-08-21 10:25:29
Paths are valid. It appears to be something with the "$TL_Target" parameter. When I remove that, the shortcut is created but without the -profile parameter. I’m not sure why this is a problem in the 64-bit Windows.
by cre8tivspirit at 2012-08-21 10:47:29
Not sure why, but adding;
$objShortCut.Arguments = $TL_Target
Solved the problem. Don’t know why it wasn’t needed in 32-bit but it all works now.
by poshoholic at 2012-08-21 10:59:34
Thanks for circling back to let us know you got it working.
by DonJ at 2012-08-21 11:16:27
If you’re able to mark this as "SOLVED," it’ll help future users a lot! Thanks!
by cre8tivspirit at 2012-08-22 03:26:26
I looked for an option to do that but didn’t see it. I try to do that whenever possible.