小编Jan*_*eng的帖子

PowerShell命令多行

我有一个如下所示的批处理脚本:

TEST.BAT

@echo off

:: Starts a PowerShell session that starts a PowerShell process with administrator privileges
powershell -noprofile -command "&{$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;$process.WaitForExit();}"
Run Code Online (Sandbox Code Playgroud)

为了便于阅读,我想将"&{...}"内的代码拆分为多行.

这篇文章说使用尾随反引用应该可以解决问题,但是当我写下:

powershell -noprofile -command "&{`
    $process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;`
    $process.WaitForExit();`
}"
Run Code Online (Sandbox Code Playgroud)

...也就是说,我用尾随反引号结束每一行,然后我收到以下错误:

Incomplete string token.
At line:1 char:4
+ &{` <<<<
    + CategoryInfo          : ParserError: (`:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : IncompleteString

'$process' is not recognized as an …
Run Code Online (Sandbox Code Playgroud)

powershell batch-file

9
推荐指数
1
解决办法
1万
查看次数

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

我想创建一个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. 如何使用Powershell创建快捷方式

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

C:\Windows\System32\cmd.exe /C "C:\Dev\Batch files\cmake-guiMSVC1064bit.bat"
Run Code Online (Sandbox Code Playgroud)

到目前为止我在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()
Run Code Online (Sandbox Code Playgroud)

这会导致以下错误:

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 …
Run Code Online (Sandbox Code Playgroud)

powershell batch-file

3
推荐指数
1
解决办法
6718
查看次数

标签 统计

batch-file ×2

powershell ×2