-1

我有一个不会正确更新 $TargetFile 的 powershell 脚本。这是我的代码:

$TargetFile = "C:\Users\wjschan\AppData\Roaming\DAS Toolbox\DAS Toolbox.exe"

$ShortcutFile = "$StartMenuPath\$ApplicationName.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$NewTarget = $TargetFile
$Shortcut.TargetPath = $TargetFile
$Shortcut.IconLocation = $IconLocation
$Shortcut.Save()

但是当我右键单击快捷方式时,它会将“这台电脑”列为目标。但是,如果我把这段代码放进去,它工作正常:

$ShortcutFile = "$StartMenuPath\$ApplicationName.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$NewTarget = $TargetFile
$Shortcut.TargetPath = "C:\Users\wjschan\AppData\Roaming\DAS Toolbox\DAS Toolbox.exe"
$Shortcut.IconLocation = $IconLocation
$Shortcut.Save()

我不知道为什么硬编码文件路径有效但变量无效。如果 $TargetFile 指向不同的路径(比如网络共享),它之前可以工作。

Win10,Build 1607(当前业务分支)-不,我无法获得更新的版本。

4

1 回答 1

2

显然你$TargetFile不是一个String类型。
我怀疑它是一种IO.FileInfo类型,因为您可能会动态地构建路径。
要找出它实际关注的对象类型,请尝试:$TargetFile.PSTypeNames

要将其强制为 a String,请将其包装成引号,例如:

$Shortcut.TargetPath = "$TargetFile"

或使用:$TargetFile.ToString()[String]$TargetFile

于 2018-06-12T19:59:54.417 回答