【发布时间】:2020-02-23 14:12:01
【问题描述】:
我有一个应用程序能够调用/执行 Powershell 并在执行此操作时发送命令。
不幸的是,我需要它发送的部分命令包含带引号的多行文本。
因此,我的解决方案需要将该数据存储到一个变量/文件中,当应用程序发送命令参数时可以通过 powershell 引用该变量/文件。
我可以将其存储为 Copy.PS1
$command = 'Set-Clipboard -Value @"
Final text
Text also here
"@'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
然后我尝试调用 Powershell,同时发送参数:
powershell -ExecutionPolicy bypass -EncodedCommand $encodedCommand -File "Copy.PS1"
此过程不起作用。也没有显示任何错误。它根本不会更新剪贴板。
但是,当我打开 Powershell ISE 并在不引用文件的情况下运行它时,一切正常:
$command = 'Set-Clipboard -Value @"
Final text
Text also here
"@'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell -ExecutionPolicy bypass -EncodedCommand $encodedCommand
谁能解释为什么这种行为如此不同?
【问题讨论】:
-
您启动 Powershell 并提供编码命令 AND 文件?为什么?我希望其中之一 - 不是两者。我建议使用 here 字符串并将其复制到剪贴板。
-
嗯...不确定我是否遵循。编码的命令是变量 $command 完全转义,因为它可以包含其他命令/HTML。
-
您从哪里调用 PowerShell CLI (
powershell.exe)?我以为$encodedCommand在Copy.ps1文件中,为什么还要通过CLI 传递它?请说明您的要求。
标签: powershell