2

I'm trying to run the below script (myscript.ps1) inside a Windows Docker container without actually copying the script file to the container.

$Source =  @"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public static class Imagehlp
{
    [DllImport("imagehlp.dll", CharSet = CharSet.Auto)]
    public static extern int MapFileAndCheckSum(string Filename, out int HeaderSum, out int CheckSum);
}
"@
Add-Type -TypeDefinition $Source

[int] $headerSum = 0;
[int] $checkSum = 0;
$result = [Imagehlp]::MapFileAndCheckSum(
    "C:\Program Files\Internet Explorer\iexplore.exe",
    [ref] $headerSum,
    [ref] $checkSum
    )

 if ($result -ne 0) {
     Write-Error "Error: $result"
    }
 $headerSum, $checkSum

First, I have searched online for the answer and tried the solution in here. However, when I tried the given solution, I got the error below.

docker exec my-windows powershell -command "C:\Users\abc\Desktop\PowerShellScripts\myscript.ps1"

C:\Users\abc\Desktop\PowerShellScripts\myscript.ps1 : The term 
'C:\Users\abc\Desktop\PowerShellScripts\myscript.ps1' is not
recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
At line:1 char:1
+ C:\Users\abc\Desktop\PowerShellScripts\myscript.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\abc...yscript.p
   s1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The reason for this error is probably because the script is in the host not in the container. Therefore, I have tried to save the script content into a variable in PowerShell and then tried to run the command with the variable.

$script1 = Get-Content ('C:\Users\abc\Desktop\PowerShellScripts\myscript.ps1')
docker exec my-windows powershell -command $script1

This time I get the following error.

At line:1 char:15
+ $Source =  @" using System; using System.Diagnostics; using System.Ru ...
+               ~
No characters are allowed after a here-string header but before the end of the
line.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
   ception
    + FullyQualifiedErrorId : UnexpectedCharactersAfterHereStringHeader

It says that the here-string part of my script is not properly typed and there exists another character after @" but there is no character after that. I'm guessing this is something related to newline or carriage return characters but I'm not sure. Can you please help me? Thanks a bunch!

1 Answer 1

2

For this, I'd suggest taking advantage of powershell.exe's -EncodedCommand switch

# Load script contents from disk
$script1 = Get-Content 'C:\Users\abc\Desktop\PowerShellScripts\myscript.ps1' -Raw
# UTF16LE-encode the script
$bytes = [System.Text.Encoding]::Unicode.GetBytes($script1)
# Convert encoded byte string to b64
$encodedCommand = [Convert]::ToBase64String($bytes)

docker exec my-windows powershell -encodedcommand $encodedCommand

Beware that Windows' process API's limit the length of command line arguments to 8191 characters, so it only works for scripts of less than ~3050 characters (including whitespace)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Mathias for the quick reply, I have tried your solution but I still get the same error. I have edited the post with the newer error. Maybe this error is related to saving a script to a variable and then calling the script via the variable. Is this possible?
@grcrtrkm Looks like PowerShell takes issue with the line breaks in the @""@ here-string being LF and not CRLF, try $script1 = $script1 -replace '\r?\n',"`r`n" before GetBytes()
OK, now I got it but I had to call get-content with -raw flag to ensure that the script is saved into a variable properly. Can you please edit your answer like below. Thanks for the help, appreciated! $script1 = Get-Content -raw ('C:\Users\abc\Desktop\PowerShellScripts\myscript.ps1')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.