4

我想创建一个在 Windows 7 任务栏中创建快捷方式的 powershell 脚本,该脚本从 cmd.exe 运行批处理文件。

尝试按照这两个帖子中的说明进行操作:

  1. https://superuser.com/questions/100249/how-to-pin-either-a-shortcut-or-a-batch-file-to-the-new-windows-7-taskbar
  2. 如何使用 Powershell 创建快捷方式

基本上我想将快捷方式文件的目标属性设置为:

C:\Windows\System32\cmd.exe /C "C:\Dev\Batch files\cmake-guiMSVC1064bit.bat"

到目前为止,我在我的 powershell 脚本中得到的是:

$batchPath = "C:\Dev\my_batchfile.bat"
$taskbarFolder = "$Home\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\Taskbar\"
$cmdPath = (Get-Command cmd | Select-Object Definition).Definition
$objShell = New-Object -ComObject WScript.Shell
$objShortCut = $objShell.CreateShortcut("$shortcutFolder\$batchName.lnk")

#TODO problem ... :(
$objShortCut.TargetPath = "$cmdPath /C $batchPath"

$objShortCut.Save()

这会导致以下错误:

Exception setting "TargetPath": "The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))" At C:\Dev\Powershell\GetTools.ps1:220 char:18
+     $objShortCut. <<<< TargetPath = "$cmdPath /C $batchPath"
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

有人有什么建议吗?

4

1 回答 1

10

通过 Arguments 属性设置参数:

$batchName = 'cmd'
$batchPath="D:\Temp\New folder\test.bat"
$taskbarFolder = "$env:APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\Taskbar\"
$objShell = New-Object -ComObject WScript.Shell
$objShortCut = $objShell.CreateShortcut("$taskbarFolder\$batchName.lnk")
$objShortCut.TargetPath = 'cmd'
$objShortCut.Arguments="/c ""$batchPath"""
$objShortCut.Save()
于 2012-06-19T07:52:04.737 回答