【问题标题】:Create shortcut that runs a batch file创建运行批处理文件的快捷方式
【发布时间】:2012-06-19 07:29:11
【问题描述】:

我想创建一个 powershell 脚本,它在 windows 7 任务栏中创建一个快捷方式,从 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. How to create a shortcut using 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

有人有什么建议吗?

【问题讨论】:

  • 我已经有了批处理文件,只需要制作一个powershell脚本来创建一个快捷方式,其中目标属性类似于:C:\Windows\System32\cmd.exe /C "C:\开发\批处理文件\cmake-guiMSVC1064bit.bat"
  • 为什么不在批处理文件上右键->创建快捷方式...?
  • 因为这是一个更大的脚本的一部分,它应该在多台机器上运行,并且需要最少的人工交互。 :)
  • 啊,好吧。无论如何,$batchPath 是在哪里定义的?也许这就是你的问题。
  • 刚刚在复制/粘贴中发现了那个变量,现在在帖子中修复它,仍然遇到同样的问题。

标签: powershell batch-file


【解决方案1】:

通过 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()

【讨论】:

    猜你喜欢
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多