Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows inline ps1 when exec privilege restricted #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions res/pc.ps1

This file was deleted.

24 changes: 21 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,34 @@ class Paster {
if (!powershellExisted) {
command = "powershell"
}
// Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1
let script = `$imagePath = "${imagePath}"

Add-Type -Assembly PresentationCore
$img = [Windows.Clipboard]::GetImage()

if ($img -eq $null) {
"no image"
Exit 1
}

$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0)
$stream = [IO.File]::Open($imagePath, "OpenOrCreate")
$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder
$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null
$encoder.Save($stream) | out-null
$stream.Dispose() | out-null

$imagePath`
// TS refuses to compile Buffer.from
const encoded = (Buffer as any).from(script, 'utf16le').toString('base64');
const powershell = spawn(command, [
'-noprofile',
'-noninteractive',
'-nologo',
'-sta',
'-executionpolicy', 'unrestricted',
'-windowstyle', 'hidden',
'-file', scriptPath,
imagePath
'-EncodedCommand', encoded
]);
powershell.on('error', function (e) {
if (e.code == "ENOENT") {
Expand Down