· 6 years ago · Jul 08, 2019, 08:56 AM
1$lblDevX="Dev2"
2$lblTestX="Test2"
3$lblIntX="Int"
4$DevX="i-0a7738a4620b4c2bf"
5$TestX="i-0b9872cd15c6101eb"
6$IntX="i-0e3a6a5c107048a18"
7$AccessKey="AKIAWADI6GREPLH7BCSK"
8$SecretKey="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"
9Set-DefaultAWSRegion -Region eu-west-1
10
11
12Function ButtonStartDev_Click()
13{
14 Start-EC2Instance -InstanceId $DevX -AccessKey $AccessKey -SecretKey $SecretKey
15}
16
17Function ButtonStopDev_Click()
18{
19 Stop-EC2Instance -InstanceId $DevX -AccessKey $AccessKey -SecretKey $SecretKey
20}
21
22Function ButtonStartTest_Click()
23{
24 Start-EC2Instance -InstanceId $TestX -AccessKey $AccessKey -SecretKey $SecretKey
25}
26
27Function ButtonStopTest_Click()
28{
29 Stop-EC2Instance -InstanceId $TestX -AccessKey $AccessKey -SecretKey $SecretKey
30}
31
32Function ButtonStartInt_Click()
33{
34 Start-EC2Instance -InstanceId $IntX -AccessKey $AccessKey -SecretKey $SecretKey
35}
36
37Function ButtonStopInt_Click()
38{
39 Stop-EC2Instance -InstanceId $IntX -AccessKey $AccessKey -SecretKey $SecretKey
40}
41
42
43Function Generate-Form {
44
45 Add-Type -AssemblyName System.Windows.Forms
46 Add-Type -AssemblyName System.Drawing
47
48 # Build Form
49 $Form = New-Object System.Windows.Forms.Form
50 $Form.Text = "AWS Serwery"
51 $Form.Size = New-Object System.Drawing.Size(550,200)
52 $Form.StartPosition = "CenterScreen"
53 $Form.Topmost = $True
54
55 # Add Label
56 $LabelDev = New-Object System.Windows.Forms.Label
57 $LabelDev.Location = New-Object System.Drawing.Size(35,10)
58 $LabelDev.Size = New-Object System.Drawing.Size(120,50)
59 $LabelDev.Text = $lblDevX
60
61 # Add Label
62 $LabelTest = New-Object System.Windows.Forms.Label
63 $LabelTest.Location = New-Object System.Drawing.Size(200,10)
64 $LabelTest.Size = New-Object System.Drawing.Size(120,50)
65 $LabelTest.Text = $lblTestX
66
67 # Add Label
68 $LabelInt = New-Object System.Windows.Forms.Label
69 $LabelInt.Location = New-Object System.Drawing.Size(365,10)
70 $LabelInt.Size = New-Object System.Drawing.Size(120,50)
71 $LabelInt.Text = $lblIntX
72
73 # Add Button
74 $ButtonStartDev = New-Object System.Windows.Forms.Button
75 $ButtonStartDev.Location = New-Object System.Drawing.Size(35,35)
76 $ButtonStartDev.Size = New-Object System.Drawing.Size(120,50)
77 $ButtonStartDev.Text = "Start Dev"
78
79 # Add Button
80 $ButtonStopDev = New-Object System.Windows.Forms.Button
81 $ButtonStopDev.Location = New-Object System.Drawing.Size(35,80)
82 $ButtonStopDev.Size = New-Object System.Drawing.Size(120,50)
83 $ButtonStopDev.Text = "Stop Dev"
84
85 # Add Button
86 $ButtonStartTest = New-Object System.Windows.Forms.Button
87 $ButtonStartTest.Location = New-Object System.Drawing.Size(200,35)
88 $ButtonStartTest.Size = New-Object System.Drawing.Size(120,50)
89 $ButtonStartTest.Text = "Start Test"
90
91 # Add Button
92 $ButtonStopTest = New-Object System.Windows.Forms.Button
93 $ButtonStopTest.Location = New-Object System.Drawing.Size(200,80)
94 $ButtonStopTest.Size = New-Object System.Drawing.Size(120,50)
95 $ButtonStopTest.Text = "Stop Test"
96
97 # Add Button
98 $ButtonStartInt = New-Object System.Windows.Forms.Button
99 $ButtonStartInt.Location = New-Object System.Drawing.Size(365,35)
100 $ButtonStartInt.Size = New-Object System.Drawing.Size(120,50)
101 $ButtonStartInt.Text = "Start Int"
102
103 # Add Button
104 $ButtonStopInt = New-Object System.Windows.Forms.Button
105 $ButtonStopInt.Location = New-Object System.Drawing.Size(365,80)
106 $ButtonStopInt.Size = New-Object System.Drawing.Size(120,50)
107 $ButtonStopInt.Text = "Stop Int"
108
109 $Form.Controls.Add($ButtonStartTest)
110 $Form.Controls.Add($ButtonStopTest)
111 $Form.Controls.Add($ButtonStartDev)
112 $Form.Controls.Add($ButtonStopDev)
113 $Form.Controls.Add($ButtonStartInt)
114 $Form.Controls.Add($ButtonStopInt)
115 $Form.Controls.Add($LabelDev)
116 $Form.Controls.Add($LabelTest)
117 $Form.Controls.Add($LabelInt)
118
119 #Add Button event
120 $ButtonStartTest.Add_Click({ButtonStartTest_Click})
121 $ButtonStopTest.Add_Click({ButtonStopTest_Click})
122 $ButtonStartDev.Add_Click({ButtonStartDev_Click})
123 $ButtonStopDev.Add_Click({ButtonStopDev_Click})
124 $ButtonStartInt.Add_Click({ButtonStartInt_Click})
125 $ButtonStopInt.Add_Click({ButtonStopInt_Click})
126
127
128 #Show the Form
129 $form.ShowDialog()| Out-Null
130
131} #End Function
132
133#Call the Function
134
135$IsAwsPowershell = Get-InstalledModule | Where-Object {$_.Name -EQ "AWSPowerShell"} | select -ExpandProperty name
136if ($IsAwsPowershell -ne "AWSPowerShell") {
137 Add-Type -AssemblyName System.Windows.Forms
138 $result = [System.Windows.Forms.MessageBox]::Show('Brak zainstalowanego modułu AWSPowerShell. Czy zainstalować?', 'Error', 'YESNO', 'Error')
139 if ($result -eq "Yes") {
140 If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
141 # launch as an elevated process:
142 Start-Process powershell.exe 'Set-PSRepository -Name PSGallery -InstallationPolicy Trusted; Install-Module -Name AWSPowerShell -Force' -Verb RunAs -Wait
143 [System.Windows.Forms.MessageBox]::Show('Moduł zainstalowany. Uruchom ponownie aplikację', 'Info', 'OK', 'Info')
144 }
145 }
146}
147else {
148 Generate-Form
149}