【问题标题】:How is it possible to run this PowerShell script inside a docker container?如何在 docker 容器中运行这个 PowerShell 脚本?
【发布时间】:2020-12-26 14:20:59
【问题描述】:

我正在尝试在 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

首先,我在网上搜索了答案并尝试了here.的解决方案,但是当我尝试给定的解决方案时,我得到了以下错误。

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-string 部分没有正确输入,@" 之后存在另一个字符,但之后没有字符。我猜这与换行符或回车符有关,但我不确定。你能帮我么?非常感谢!

【问题讨论】:

    标签: c# powershell docker scripting


    【解决方案1】:

    为此,我建议利用powershell.exe-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
    

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

    【讨论】:

    • 感谢@Mathias 的快速回复,我已经尝试了您的解决方案,但仍然遇到同样的错误。我用更新的错误编辑了帖子。也许这个错误与将脚本保存到变量然后通过变量调用脚本有关。这可能吗?
    • @grcrtrkm 看起来 PowerShell 对 @""@ 此处字符串中的换行符存在问题 LF 而不是 CRLF,请在 GetBytes() 之前尝试 $script1 = $script1 -replace '\r?\n',"`r`n"
    • 好的,现在我明白了,但我必须使用 -raw 标志调用 get-content 以确保脚本正确保存到变量中。您能否像下面一样编辑您的答案。感谢您的帮助,不胜感激! $script1 = Get-Content -raw ('C:\Users\abc\Desktop\PowerShellScripts\myscript.ps1')
    猜你喜欢
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    相关资源
    最近更新 更多