Powershell 创建运行批处理文件的快捷方式

Powershell 创建运行批处理文件的快捷方式,powershell,batch-file,Powershell,Batch File,我想创建一个powershell脚本,在windows 7任务栏中创建一个快捷方式,从cmd.exe运行批处理文件 试着按照这两篇文章的要求去做: 基本上,我希望将快捷方式文件目标属性设置为类似以下内容: C:\Windows\System32\cmd.exe /C "C:\Dev\Batch files\cmake-guiMSVC1064bit.bat" 到目前为止,我在powershell脚本中得到的是: $batchPath = "C:\Dev\my_batchfile.bat" $

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

试着按照这两篇文章的要求去做:

  • 基本上,我希望将快捷方式文件目标属性设置为类似以下内容:

    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
    
    异常设置“TargetPath”:“参数不正确。(来自HRESULT的异常:0x80070057(E_INVALIDARG))”位于C:\Dev\Powershell\GetTools.ps1:220字符:18
    
    +$objShortCut 通过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()
    

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