我有一个如下所示的批处理脚本:
@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脚本,在Windows 7任务栏中创建一个快捷方式,从cmd.exe运行批处理文件.
试着像这两篇文章中所说的那样做:
基本上我想将快捷方式文件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)