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!