Powershell 在初始引导时,由于用户数据文件无法加载而传递给AWS EC2实例的Bash脚本

Powershell 在初始引导时,由于用户数据文件无法加载而传递给AWS EC2实例的Bash脚本,powershell,amazon-web-services,amazon-ec2,user-data,cloud-init,Powershell,Amazon Web Services,Amazon Ec2,User Data,Cloud Init,我有一个简单的bash脚本,我正试图通过使用AWS库的Powershell脚本传递给AWS EC2 ubuntu实例 #!/bin/bash apt-get update apt-get upgrade 下面是powershell脚本,该脚本对base64中的文件内容进行编码,并调用启动EC2实例的cmdlet: $fileContent = Get-Content $UserDataTarget $fileContentBytes = [System.Text.Encoding]::UTF

我有一个简单的bash脚本,我正试图通过使用AWS库的Powershell脚本传递给AWS EC2 ubuntu实例

#!/bin/bash

apt-get update
apt-get upgrade
下面是powershell脚本,该脚本对base64中的文件内容进行编码,并调用启动EC2实例的cmdlet:

$fileContent = Get-Content $UserDataTarget
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$MasterInstance = New-EC2Instance -ImageId ami-4c7a3924 -MinCount 1 -MaxCount 1 -KeyName AWSKey -SecurityGroups $SecurityGroup -InstanceType "t1.micro" -UserData $fileContentEncoded
这是cloud init日志中的一个片段:

Cloud-init v. 0.7.5 running 'modules:final' at Fri, 02 Oct 2015 21:05:24 +0000. Up 33.88 seconds.
/bin/bash: apt-get update apt-get upgrade: No such file or directory
2015-10-02 21:05:24,294 - util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [127]
2015-10-02 21:05:24,298 - cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
2015-10-02 21:05:24,298 - util.py[WARNING]: Running scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
我曾尝试使用010编辑器和Cygwin将windows文件转换为linux。我试着用LF字节替换CRLF字节。结果是一样的:整个bash脚本压缩为一行,所有换行符都被删除,并且用户数据脚本在初始引导时无法加载

更新:我已经包括了两个代码片段,我用它们来进行换行转换。两者都是从同行来源(SO)审查的。出于某种原因,该脚本仍然显示在linux实例中,没有换行符

片段1

function ConvertTo-LinuxLineEndings($path) {
        $oldBytes = [io.file]::ReadAllBytes($path)
        if (!$oldBytes.Length) {
            return;
        }
        [byte[]]$newBytes = @()
        [byte[]]::Resize([ref]$newBytes, $oldBytes.Length)
        $newLength = 0
        for ($i = 0; $i -lt $oldBytes.Length - 1; $i++) {
            if (($oldBytes[$i] -eq [byte][char]"`r") -and ($oldBytes[$i + 1] -eq [byte][char]"`n")) {
                continue;
            }
            $newBytes[$newLength++] = $oldBytes[$i]
        }
        $newBytes[$newLength++] = $oldBytes[$oldBytes.Length - 1]
        [byte[]]::Resize([ref]$newBytes, $newLength)
        [io.file]::WriteAllBytes($path, $newBytes)
    }

    ConvertTo-LinuxLineEndings($UserDataTarget)
片段2

try{

    # get the contents and replace line breaks by U+000A
    $contents = [IO.File]::ReadAllText($UserDataSource) -replace "`r`n", "`n"
    # create UTF-8 encoding without signature
    $utf8 = New-Object System.Text.UTF8Encoding $false
    # write the text back
    [IO.File]::WriteAllText($UserDataTarget, $contents, $utf8)
}
catch [Exception]
{
    echo $_.Exception|format-list -force
}

我无法回答您的Powershell代码需要什么样的外观,但如果有帮助,我可以告诉您Powershell脚本的输出需要什么样的外观,因此您需要确认,当它进入模板时,这就是您在UserData中得到的结果:

"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
            "#!/bin/bash\n\n",
            "apt-get update\n",
            "apt-get upgrade\n"
        ]]}}

也许试着用
\n
来表示换行符?我试过了。在如何通过AWS powershell库将源于windows机器的用户数据文件复制到EC2上的linux实例上,似乎有些奇怪。请查看您的/var/lib/cloud/instance/user-data.txt或/var/lib/cloud/cloud-config.txt(ubuntu)文件,以查看是否传递了有效的脚本。您将看到系统所看到的脚本。
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
            "#!/bin/bash\n\n",
            "apt-get update\n",
            "apt-get upgrade\n"
        ]]}}