简体   繁体   中英

PowerShell to Copy To Clipboard Using -File

I have an application that is able to call/execute Powershell and send commands while doing so.

Unfortunately, part of those commands that I need it to send, contains multi-line text with quotes.

Because of this, my solution needs to store the that data into a variable/file, that can be referenced via powershell when the application sends the command arguments.

I'm able to store this as Copy.PS1

$command = 'Set-Clipboard -Value @"
Final text
Text also here
"@'

$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)

Then I try calling Powershell, while sending the arguments:

powershell -ExecutionPolicy bypass -EncodedCommand $encodedCommand -File "Copy.PS1"

This process doesn't work. Nor are there any errors that display. It simply doesn't update the clipboard.

However, when I open Powershell ISE, and run it without referencing a File, everything works:

$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

Can someone explain why this behavior is so different?

I'd recommend keeping it as simple as possible. If you want to have a string running accross multiple lines copied to the clipboard do exactly this ... not more:

$HereString = @'
Some arbitrary
String running
accross multiple
lines
'@
Set-Clipboard -Value $HereString

Save this to a file "Copy.PS1" and run it with this command line:

powershell -ExecutionPolicy bypass -File Copy.PS1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM