-
Notifications
You must be signed in to change notification settings - Fork 64
/
Computer_Services_With_WPF_GUI.ps1
182 lines (154 loc) · 13.4 KB
/
Computer_Services_With_WPF_GUI.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#
#
#
#
# Created By Kaido Järvemets 05.05.2011
# DepSharee.Blogspot.com
# Configuration Manager MVP
#
#
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Computer Services by Kaido Järvemets" Height="493" Width="651">
<Grid>
<Label Content="Computer:" Height="28" HorizontalAlignment="Left" Margin="12,41,0,0" Name="Label1" VerticalAlignment="Top" FontWeight="Bold" Width="81" FontSize="13" />
<TextBox Height="23" HorizontalAlignment="Right" Margin="0,46,384,0" Name="TxtBox_Computer" VerticalAlignment="Top" Width="146" />
<ListView Height="310" HorizontalAlignment="Left" Margin="90,94,0,0" Name="ListView1_Services" VerticalAlignment="Top" Width="491">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked = "{Binding IsChecked}"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header = "Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Name = "Name" Content = "{Binding Name}"></Label>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header = "Status">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Name = "Status" Content = "{Binding Status}"></Label>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header = "Display Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Name = "DisplayName" Content = "{Binding DisplayName}"></Label>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Content="Connect" Height="23" HorizontalAlignment="Left" Margin="270,46,0,0" Name="Btn_Connect" VerticalAlignment="Top" Width="75" />
<Button Content="Restart" Height="23" HorizontalAlignment="Left" Margin="335,419,0,0" Name="Btn_Restart" VerticalAlignment="Top" Width="75" />
<Button Content="Stop" Height="23" HorizontalAlignment="Left" Margin="416,419,0,0" Name="Btn_Stop" VerticalAlignment="Top" Width="75" />
<Button Content="Start" Height="23" HorizontalAlignment="Left" Margin="506,419,0,0" Name="Btn_Start" VerticalAlignment="Top" Width="75" />
<Label Height="28" HorizontalAlignment="Left" Margin="26,418,0,0" Name="Label_Messages" VerticalAlignment="Top" Width="265" />
<Label Content="DepSharee.BlogSpot.com" HorizontalAlignment="Left" Margin="434,12,0,414" Name="Label3" Width="147" />
</Grid>
</Window>
'@
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
#Find objects
$Connect = $form.FindName('Btn_Connect')
$Restart = $form.FindName('Btn_Restart')
$Stop = $form.FindName('Btn_Stop')
$Start = $form.FindName('Btn_Start')
$ListView1_Services = $form.FindName('ListView1_Services')
$Computer = $form.FindName('TxtBox_Computer')
$Messages = $form.FindName('Label_Messages')
# Fill ListView
Function FillListView($Computer)
{
$script:emptyarray = New-Object System.Collections.ArrayList # Empty Array
$Services = Get-Service -ComputerName $Computer # Get Services
foreach ($item in $Services)
{
$tmpObject = Select-Object -InputObject "" IsChecked, Name, Status, DisplayName
$tmpObject.IsChecked = $false
$tmpObject.Name = $item.Name
$tmpObject.Status = $item.Status
$tmpObject.DisplayName = $item.DisplayName
$script:emptyarray += $tmpObject
}
# ListView item source
$ListView1_Services.ItemsSource = $script:emptyarray
}# End of FillListviewFunction
# Function Ping Computer
Function Ping-Computer($Computer)
{
Get-WmiObject -Class Win32_PingStatus -Filter "Address = '$Computer'"
}# End of Function Ping-Computer
# Connect button event
$Connect.Add_Click({
If((Ping-Computer -Computer $Computer.Text).StatusCode -eq 0){
$Messages.Content = ""
FillListView -Computer $Computer.Text
}
Else{
$Messages.Content = "Computer is not responding to Ping"
}
})
# Restart button event
$Restart.Add_Click({
foreach($AddedItem in $script:emptyarray)
{
if($AddedItem.IsChecked)
{
$Service = $AddedItem.Name
#Invoke command
Invoke-Command -ComputerName $Computer.Text -ArgumentList $Service -ScriptBlock {param ($Service) Restart-Service $Service}
#Sleep three seconds
Start-Sleep 3
#Fill again ListView
FillListView -Computer $Computer.Text
}
}
})
# Stop button Event
$Stop.Add_Click({
foreach($AddedItem in $script:emptyarray)
{
if($AddedItem.IsChecked)
{
$Service = $AddedItem.Name
#Invoke Command
Invoke-Command -ComputerName $Computer.Text -ArgumentList $Service -ScriptBlock {param ($Service) Stop-Service $Service -force}
#Sleep three seconds
Start-Sleep 3
#Fill again ListView
FillListView -Computer $Computer.Text
}
}
})
# Start button event
$Start.Add_Click({
foreach($AddedItem in $script:emptyarray)
{
if($AddedItem.IsChecked)
{
$Service = $AddedItem.Name
#Invoke command
Invoke-Command -ComputerName $Computer.Text -ArgumentList $Service -ScriptBlock {param ($Service) Start-Service $Service}
#Sleep three seconds
Start-Sleep 3
#Fill listview again
FillListView -Computer $Computer.Text
}
}
})
#Show Form
$Form.ShowDialog() | out-null