sty*_*ets 18 windows upload automation amazon-ec2 amazon-web-services
我想启动EC2 Windows实例,上传EXEecutable并执行它(所有这些都是自动化的,这很重要)
到目前为止,我能够以编程方式启动EC2 Windows实例并获取其参数(密码/ IP),现在我想找到一种方法来上传这个可执行文件(从我的Windows机器或我的其他EC2 linux实例)并运行它.
我考虑过启动RDP连接并使用宏软件上传和执行文件,但根据以前的经验,这是一个糟糕/脆弱的方法,至少可以说.
我还想过将这个EXE上传到服务器,然后在Windows上做这样的事情:
wget http://www.domain.com/my-file.exe
Run Code Online (Sandbox Code Playgroud)
除了Windows 没有 wget!
所以我的问题是:有没有办法在EC2 Windows实例中以编程方式上传和执行EXEcutable?
Bar*_*rak 12
另一种方法是使用Windows powershell和WinRM - 它允许远程执行,有点像Linux上的ssh.
以下是您可以在客户端上运行以远程执行脚本的powershell脚本示例(摘自:https://github.com/CloudifySource/cloudify/blob/master/esc/src/main/resources/clouds/ec2 -win/upload/bootstrap-client.ps1):
param ([string]$target, [string]$username, [string]$password, [string]$command)
$ErrorActionPreference="Stop"
# Set up the password
$securePassword = ConvertTo-SecureString -AsPlainText -Force $password
$cred = New-Object System.Management.Automation.PSCredential $username, $securePassword
Write-Host "Connecting to management service of $target"
Connect-WSMan -Credential $cred $target
set-item WSMan:\$target\Client\TrustedHosts -Value * -Force
set-item WSMan:\$target\Shell\MaxMemoryPerShellMB -Value 0 -Force
Write-Host Invoking command on Remote host $target
Invoke-Command -ComputerName $target -Credential $cred -ScriptBlock {
Invoke-Expression $args[0]
} -ArgumentList $command
Write-Host "Command finished"
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令从您自己的脚本运行此命令:
powershell.exe -inputformat none -File PATH_TO_SCRIPT -target TARGET_IP -password PASSWORD -username USERNAME -command COMMAND_TO_EXECUTE
Run Code Online (Sandbox Code Playgroud)
你可能应该引用你的字符串,尤其是密码和命令,因为这些字符通常会包含powershell可以解释为其他内容的特殊字符.
默认情况下,Win2M服务在EC2 Amazon Windows AMI上启用.您需要做的就是在安全组中打开端口5985(WinRM端口).
最后,如果您以前从未在客户端计算机上使用过PowerShell远程处理,则应该执行一些命令来设置它(您只需要执行一次):
set-item WSMan:\localhost\Client\TrustedHosts -Value * -Force
set-item WSMan:\localhost\Shell\MaxMemoryPerShellMB -Value 0 -Force
Enable-PSRemoting
Set-ExecutionPolicy unrestricted
Run Code Online (Sandbox Code Playgroud)
确保以管理员身份运行它们.
该命令ec2-run-instances
有两个额外的参数,可以在运行命令时使用.该user-data
命令和user-data-file
这两个命令执行相同的任务只是他们从不同的输入读取.使用此参数时,用户数据的内容将上载到亚马逊托管的URI http://169.254.169.254/1.0/user-data
,该URI 仅对启动的实例可用.
在linux环境中执行此操作的常规方法是将shell脚本上载到实例以下载exe,您的用户数据文件可能看起来像这样......
#! /bin/bash
wget http://www.domain.com/my-file.exe
Run Code Online (Sandbox Code Playgroud)
在Windows中,没有安装默认服务来在引导实例时执行用户数据文件,但是有一个开源项目CloudInit.NET模拟相同的进程,但有一个PowerShell脚本.唯一的要求是.NET 4.0和CloudInit.NET.安装后,它将在引导实例时执行用户数据文件.下载文件并使用PowerShell脚本执行它非常容易.
!# /powershell/
$wc = New-Object System.Net.WebClient
$wc.DownloadFile("http://www.domain.com/my-file.exe", "C:\my-file.exe");
& 'C:\my-file.exe'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16596 次 |
最近记录: |