Powershell NSIS CreateShortCut在快捷方式的目标字段中未显示正确的字符串

Powershell NSIS CreateShortCut在快捷方式的目标字段中未显示正确的字符串,powershell,nsis,Powershell,Nsis,以下是NSIS安装程序的摘录 我的问题是当我执行: CreateShortCut "$DESKTOP\PRESS_Survey_PS.lnk" $shortcutPath "" $INSTDIR\images\appIcons\Survey.ico 它使用变量$shortcutPath在快捷方式的目标字段中生成: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command &.\PRESS_Survey.ps1

以下是NSIS安装程序的摘录

我的问题是当我执行:

CreateShortCut "$DESKTOP\PRESS_Survey_PS.lnk" $shortcutPath "" $INSTDIR\images\appIcons\Survey.ico
它使用变量$shortcutPath在快捷方式的目标字段中生成:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command &.\PRESS_Survey.ps1"
$shortcutPath变量是使用以下strcpy构建的:

StrCpy $powerShellPath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "
StrCpy $shortcutPath $powerShellPath$\"&.\PRESS_Survey.ps1$\"
当我使用以下命令将$shortcutPath变量写入我的ps1文件时:

FileWrite $0 $powershellComment$shortcutPath$NewLine
它产生了以下结果:

#C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "&.\PRESS_Survey.ps1"
当我尝试使用以下命令执行快捷方式时:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command &.\PRESS_Survey.ps1"
它失败了,询问PRESS_Survey.ps1位于何处

在目标字段中将CreateShortCut版本替换为FileWrite版本时:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "&.\PRESS_Survey.ps1"
它运行

为什么$shortcutPath的值在两种情况下都不相同? 这是我做错了什么,还是这是windows的快捷方式问题? 在这两种情况下,有没有办法解决这个问题


提前谢谢。

这里可能有两件事

首先,快捷方式目标和参数是两个不同的参数,因此需要两个变量:

StrCpy $powerShellPath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
StrCpy $parameters '-command "&.\PRESS_Survey.ps1"'
CreateShortcut "$DESKTOP\PRESS_Survey_PS.lnk" $powerShellPath $parameters "$INSTDIR\images\appIcons\Survey.ico"
另一个问题是路径中的
\
表示当前目录,我不建议在快捷方式中使用它。如果您坚持,那么您需要记住,存储在快捷方式中的当前/工作目录来自NSIS中用
SetOutPath
设置的路径,因此您可能需要执行以下操作:

Push $OUTDIR ; Save the old path
StrCpy $powerShellPath "$SysDir\WindowsPowerShell\v1.0\powershell.exe"
StrCpy $parameters '-command "&.\PRESS_Survey.ps1"'
SetOutPath "c:\path\where\PRESS_Survey\is\located" ; For .\ paths
CreateShortcut "$DESKTOP\PRESS_Survey_PS.lnk" $powerShellPath $parameters "$INSTDIR\images\appIcons\Survey.ico"
Pop $OUTDIR 
SetOutPath $OUTDIR ; Restore

非常感谢。这就成功了。关于:“另一个问题是。\in The path,。表示当前目录,我不建议在快捷方式中使用它。”我们将快捷方式的“Start in”字段设置为安装到的目录……但您知道安装程序中的完整路径,因此您也可以使用完整路径。