EC2 Windows Brugerdata: Powershell kører ikke som forventet

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg forsøger at bootstrap EC2 Windows-instanser via Powershell-kommandoer inde i brugerdata. De trin, jeg forsøger at udføre via brugerdata, er:



  • Installer chocolatey

  • Brug chokolade til at installere Python

  • Brug chocolatey til at installere AWS CLI

  • Brug AWS CLI til at downloade et Powershell-script fra S3

  • Kør det Powershell script



Brugerdataene er ret ligefrem:


<powershell>
Set-ExecutionPolicy Bypass -Force;
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'));
choco install python3 -y;
choco install awscli -y

refreshenv

$BootstrapScript = (Join-Path $env:TEMP "NewBootstrap.ps1")
& aws s3api get-object --bucket my-bucket-name --key bootstrap/WindowsBootstrap.ps1 "$BootstrapScript"

iex "$BootstrapScript"
</powershell>


Skriptet til bootstrap downloades aldrig eller udføres. Hvis jeg logger på forekomsten og ser logfilerne, tyder udgangen på, at noget mærkeligt sker:



  • På toppen af ​​logfilerne er der fejl, der klager over, at 'aws' is not recognized as the name of a cmdlet. Hvorfor skal denne fejl før den selv forsøgte at installere aws cli?

  • Efter det the term 'C:UsersAdministratorAppDataLocalTempNewBootstrap.ps1' is not recognized as the name of a cmdlet, function, script file. Igen - hvorfor er denne fejltagelse, før vi har forsøgt at få den fil?

  • Så loggerne viser, at choco, python og awscli faktisk var installeret korrekt.



Jeg forstår ikke hvad der sker med udførelsesrækkefølgen. Hvis jeg logger ind i boksen og udfører det nøjagtige samme brugerdatascript, der er indeholdt i C:WindowsTemp, kører det helt som forventet.


Enhver hjælp til forståelse eller debugging dette ville være mest værdsat.


Output fra C:ProgramDataAmazonEC2-WindowsLaunchLogUserdataExecution.log med noget choco output elid for korthed


2017/11/06 12:11:49Z: Userdata execution begins
2017/11/06 12:11:49Z: Zero or more than one <persist> tag was not provided
2017/11/06 12:11:49Z: Unregistering the persist scheduled task
2017/11/06 12:11:54Z: Zero or more than one <runAsLocalSystem> tag was not provided
2017/11/06 12:11:54Z: Zero or more than one <script> tag was not provided
2017/11/06 12:11:54Z: Zero or more than one <powershellArguments> tag was not provided
2017/11/06 12:11:54Z: <powershell> tag was provided.. running powershell content
2017/11/06 15:13:42Z: Userdata execution begins
2017/11/06 15:13:42Z: Zero or more than one <persist> tag was not provided
2017/11/06 15:13:42Z: Unregistering the persist scheduled task
2017/11/06 15:13:54Z: Zero or more than one <runAsLocalSystem> tag was not provided
2017/11/06 15:13:54Z: Zero or more than one <script> tag was not provided
2017/11/06 15:13:54Z: Zero or more than one <powershellArguments> tag was not provided
2017/11/06 15:13:55Z: <powershell> tag was provided.. running powershell content
2017/11/06 15:16:11Z: Userdata:  is currently executing. To end it kill the process with id: 2828
2017/11/06 15:17:40Z: Message: The errors from user scripts: & : The term 'aws' 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 C:WindowsTEMPUserScript.ps1:15 char:3
+ & aws s3api get-object --bucket my-bucket-name --key bootstra ...
+   ~~~
    + CategoryInfo          : ObjectNotFound: (aws:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

C:UsersAdministratorAppDataLocalTempNewBootstrap.ps1 : The term 
'C:UsersAdministratorAppDataLocalTempNewBootstrap.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:UsersAdministratorAppDataLocalTempNewBootstrap.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:UsersAdmini...ewBootstrap.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


2017/11/06 15:17:40Z: Message: The output from user scripts: This is the new bootstrap

Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
d-----        11/6/2017   3:14 PM                chocInstall                                                           
Getting latest version of the Chocolatey package for download.
Getting Chocolatey from https://chocolatey.org/api/v2/package/chocolatey/0.10.8.

... Chocolatey Install output ...

Chocolatey (choco.exe) is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
 first prior to using choco.
Ensuring chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder
Installing awscli
Chocolatey v0.10.8
Installing the following packages:
python3


 .. Python Download / Install output ...

Download of python-3.6.3-amd64.exe (30.16 MB) completed.
Hashes match.
Installing python3...
python3 has been installed.
Installed to 'C:Python36'
  python3 can be automatically uninstalled.
Environment Vars (like PATH) have changed. Close/reopen your shell to
 see the changes (or in powershell/cmd.exe just type `refreshenv`).
 The install of python3 was successful.
  Software installed as 'EXE', install location is likely default.

Chocolatey installed 1/1 packages. 
 See the log for details (C:ProgramDatachocolateylogschocolatey.log).
Chocolatey v0.10.8
Installing the following packages:
awscli

 .. AWS CLI Download / Install output ...

 The install of awscli was successful.
  Software installed as 'msi', install location is likely default.

Chocolatey installed 1/1 packages. 
 See the log for details (C:ProgramDatachocolateylogschocolatey.log).
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..

2017/11/06 15:17:40Z: Userdata execution done

Bedste reference


Problemet var, at Powershell-profilen ikke installerede Chocolatey, da den blev installeret som en del af cloud init-processen. Dette betyder, at pakker vil installere fint via choco install, men er ikke tilgængelige i miljøet, selvom du ringer til refreshenv (derfor har mit opkald til aws mislykkedes, selvom det blev installeret.


For at løse dette problem kan du manuelt tvinge Powershell til at importere Chocolatey-modulet ved at redigere din Powershell-profil.


# Updated profile content to explicitly import Choco
$ChocoProfileValue = @'
$ChocolateyProfile = "$env:ChocolateyInstallhelperschocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
  Import-Module "$ChocolateyProfile"
}
'@

# Write it to the $profile location
Set-Content -Path "$profile" -Value $ChocoProfileValue -Force

# Source it
. $profile


Du kan læse mere om dette i chocolatey fejlfindingsvejledningen [11]