-
Notifications
You must be signed in to change notification settings - Fork 14
/
Invoke-EC2.ps1
127 lines (105 loc) · 3.72 KB
/
Invoke-EC2.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
function Invoke-EC2 {
<#
.Synopsis
Invokes commands on EC2 instances
.Description
Invokes PowerShell commands on EC2 instances
.Example
Get-EC2 |
Invoke-EC2 -ScriptBlock { Get-Process }
.Link
Get-EC2
#>
[CmdletBinding(DefaultParameterSetName='InProcess')]
param(
# The EC2 instance ID
[Parameter(ParameterSetName='ComputerName',Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[string]
$InstanceId,
# An existing PS Session
[Parameter(ParameterSetName='Session', Position=0, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Runspaces.PSSession[]]
${Session},
# The port used to invoke the command
[Parameter(ParameterSetName='ComputerName')]
[ValidateRange(1, 65535)]
[System.Int32]
${Port},
# If set, will use SSL
[Parameter(ParameterSetName='ComputerName')]
[Switch]
${UseSSL},
# The configuration name
[Parameter(ParameterSetName='ComputerName', ValueFromPipelineByPropertyName=$true)]
[System.String]
${ConfigurationName},
# The application name
[Parameter(ParameterSetName='ComputerName', ValueFromPipelineByPropertyName=$true)]
[System.String]
${ApplicationName},
# The throttle limit
[Parameter(ParameterSetName='ComputerName')]
[System.Int32]
${ThrottleLimit},
# If set, will run the command as a job
[Parameter(ParameterSetName='ComputerName')]
[Parameter(ParameterSetName='Session')]
[Switch]
${AsJob},
# If set, will hide the computername property from returned objects
[Parameter(ParameterSetName='ComputerName')]
[Parameter(ParameterSetName='Session')]
[Alias('HCN')]
[Switch]
${HideComputerName},
# The name of the job
[Parameter(ParameterSetName='Session')]
[Parameter(ParameterSetName='ComputerName')]
[System.String]
${JobName},
# The command to run on the EC2 instance
[Parameter(ParameterSetName='ComputerName', Mandatory=$true, Position=1)]
[Parameter(ParameterSetName='Session', Mandatory=$true, Position=1)]
[Alias('Command')]
[ValidateNotNull()]
[System.Management.Automation.ScriptBlock]
${ScriptBlock},
# Remoting session options
[Parameter(ParameterSetName='ComputerName')]
[System.Management.Automation.Remoting.PSSessionOption]
${SessionOption},
# Remoting authentication options
[Parameter(ParameterSetName='ComputerName')]
[System.Management.Automation.Runspaces.AuthenticationMechanism]
${Authentication},
# An input object
[Parameter(ValueFromPipeline=$true)]
[System.Management.Automation.PSObject]
${InputObject},
# Any arguments to the remote script
[Alias('Args')]
[System.Object[]]
${ArgumentList},
# The certificate thumbprint
[Parameter(ParameterSetName='ComputerName')]
[System.String]
${CertificateThumbprint}
)
begin {
}
process {
$ec2 = Get-EC2 -InstanceId $InstanceID
if ($psCmdlet.ParameterSetNAme -eq 'ComputerName') {
$ec2Cred = $ec2 | Get-EC2InstancePassword -AsCredential
$ec2 | Enable-EC2Remoting -PowerShell -ErrorAction SilentlyContinue
$icmParams = @{} + $psBoundParameters
$icmParams.Remove('InstanceId')
Invoke-Command -ComputerName $ec2.PublicDnsName -Credential $ec2Cred @icmParams
} else {
Invoke-Command @psboundParameters
}
}
end {
}
}