Как запустить многострочный скрипт powershell из Excel vba в одну строку? - PullRequest
0 голосов
/ 02 июля 2018

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 в командной строке, и каждый из них запустится.

1 Ответ

0 голосов
/ 02 июля 2018

Я предлагаю использовать параметр -EncodedCommand powershell.

Из справочной документации (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, так как я взял ваш пример буквально.

...