C# 如何在docker容器中运行此PowerShell脚本?

C# 如何在docker容器中运行此PowerShell脚本?,c#,powershell,docker,scripting,C#,Powershell,Docker,Scripting,我试图在Windows Docker容器中运行下面的脚本(myscript.ps1),而不实际将脚本文件复制到容器中 $Source = @" using System; using System.Diagnostics; using System.Runtime.InteropServices; public static class Imagehlp { [DllImport("imagehlp.dll", CharSet = CharSet.Aut

我试图在Windows Docker容器中运行下面的脚本(myscript.ps1),而不实际将脚本文件复制到容器中

$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
首先,我在网上搜索了答案并尝试了中的解决方案,但是,当我尝试给定的解决方案时,我得到了下面的错误

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
此错误的原因可能是脚本位于主机中,而不是容器中。因此,我尝试将脚本内容保存到PowerShell中的变量中,然后尝试使用该变量运行命令

$script1 = Get-Content ('C:\Users\abc\Desktop\PowerShellScripts\myscript.ps1')
docker exec my-windows powershell -command $script1
这次我得到了以下错误

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

它说我的脚本的here字符串部分没有正确键入,并且
@“
但之后没有字符。我猜这与换行符或回车符有关,但我不确定。你能帮帮我吗?非常感谢

为此,我建议利用
powershell.exe


请注意,Windows的process API将命令行参数的长度限制为8191个字符,因此它仅适用于少于3050个字符(包括空格)的脚本。

感谢@Mathias的快速回复,我已经尝试了您的解决方案,但仍然收到相同的错误。我已经用更新的错误编辑了这篇文章。此错误可能与将脚本保存到变量,然后通过变量调用脚本有关。这可能吗?@grcrtrkm看起来PowerShell对
@@
中的换行符有问题,这里的字符串是
LF
而不是
CRLF
,请尝试
$script1=$script1-在
GetBytes()之前替换“\r?\n”,“r`n”
,确定,现在我得到了它,但我必须使用
-raw
标志调用
get content
,以确保脚本正确保存到变量中。你能像下面那样编辑你的答案吗。谢谢你的帮助,谢谢<代码>$script1=获取内容-原始('C:\Users\abc\Desktop\PowerShellScripts\myscript.ps1')
# 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