Powershell:
line1
line2
line3
line4
Excel VBA:
Set objShell = CreateObject("Wscript.Shell")
objShell.Run ("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -executionpolicy bypass -command ""line1; line2; lin3; line4"")
line1
将运行并打开Powershell窗口,但其余命令将不运行。我可以分别将line2
,line3
和line4
分别复制并粘贴到Powershell窗口中,然后分别运行。
答案 0 :(得分:0)
我建议使用powershell的-EncodedCommand
参数。
从他们的帮助文档(powershell -?
):
# To use the -EncodedCommand parameter:
$command = 'dir "c:\program files" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand
要为参数生成base64(缩短):
[System.Convert]::ToBase64String(
[System.Text.Encoding]::Unicode.GetBytes(@'
line1
line2
line3
line4
'@)
)
实际情况:
powershell -NoExit -NoLogo -NoProfile -ExecutionPolicy Bypass -EncodedCommand "bABpAG4AZQAxAA0ACgBsAGkAbgBlADIADQAKAGwAaQBuAGUAMwANAAoAbABpAG4AZQA0AA=="
注意:由于我从字面上理解了您的示例,因此'line1' is not recognized
会失败。