-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinSvceFunctions.psm1
1624 lines (1193 loc) · 79.6 KB
/
WinSvceFunctions.psm1
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###########Get-WinSvce Function#####################
Function Get-WinSvce {
<#
.SYNOPSIS
This function help you to get needed details about any service, no matter how many computers, services or apply it on local machine or even remotely.
.DESCRIPTION
Use this function to bring any require informantion before execute service operations such as find stop service is applicable before stop it.
.PARAMETER ComputerName
Indicates the property to provide one computername or many .
.PARAMETER ServiceName
Indicates the property to provide one service name or many .
.PARAMETER DisplayOptoins
Indicates the property to call another function under name Get-WinSvceDisplayOptions to display service details based of your concern information.
.PARAMETER Credential
Indicates the property to call another function under name Get-WinSvceCredential and be able connect on computer with different credentials.
.EXAMPLE
Get-WinSvce -ComputerName Server1 -ServiceName W32Time
.EXAMPLE
Get-WinSvce -ComputerName Server1,Server2 -ServiceName W32Time,Browser -DisplayOptoins
.EXAMPLE
Get-WinSvce -ComputerName Server1 -ServiceName W32Time,Browser -DisplayOptoins -Credential
#>
param ($ComputerName,
$ServiceName,
[Switch]$DisplayOptoins,
[Switch]$Credential)
#Set trusted hosts
Set-Item WSMan:\localhost\Client\TrustedHosts –Value “*” -ea 0 -Force
#Get number of computers
$WinSrvNumber=($ComputerName).count
#Get service on one computer with credential
if (($WinSrvNumber -eq 1) -and ($Credential -eq $true)){
#Call Get-WinSvceCredential function
$WinCheckCred=1
Get-WinSvceCredential
#Get services based on function Get-WinSvceDisplayOptions
if ($DisplayOptoins){
#Call Function Get-WinSvceDisplayOptions
$WinSvceProperty = Get-WinSvceDisplayOptions
#Get services
Write-Host ("Display services on computername: " + $ComputerName) -ForegroundColor Green
#Foreach to include server type into service details if checked
$WinGetSvc=@()
foreach ($WinSvceTypeTemp in $ServiceName){
$WinGetSvc+=Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvceTypeTemp} | Select-Object $WinSvceProperty
} #end foreach ($WinSvceTypeTemp in $ServiceName)
#Display get services after choose concern display properties
$WinGetSvc | Format-Table -Wrap -AutoSize
} #end if ($DisplayOptoins)
else{
#get service wtih default display options
Write-Host ("Display services on computername: " + $ComputerName) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:ServiceName} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end else
} #end if (($WinSrvNumber -eq 1) -and ($Credential -eq $true))
#Get service on one computer without credential
if (($WinSrvNumber -eq 1) -and ($Credential -eq $false)){
#Referance value to Get-Global:WinCredential function
$WinCheckCred=0
#Get services based on function Get-WinSvceDisplayOptions
if ($DisplayOptoins){
#Call Function Get-WinSvceDisplayOptions
$WinSvceProperty = Get-WinSvceDisplayOptions
#Get services
Write-Host ("Display services on computername: " + $ComputerName) -ForegroundColor Green
#Foreach to include server type into service details if checked
$WinGetSvc=@()
foreach ($WinSvceTypeTemp in $ServiceName){
$WinGetSvc+=Invoke-Command -ComputerName $ComputerName -ScriptBlock{Get-Service -Name $using:WinSvceTypeTemp} | Select-Object $WinSvceProperty
} #end foreach ($WinSvceTypeTemp in $ServiceName)
#Display get services after choose concern display properties
$WinGetSvc | Format-Table -Wrap -AutoSize
} #end if ($DisplayOptoins)
else{
#get service wtih default display options
Write-Host ("Display services on computername: " + $ComputerName) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName -ScriptBlock{Get-Service -Name $using:ServiceName} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end else
} #end if (($WinSrvNumber -eq 1) -and ($Credential -eq $false))
#Get service on multi computer with credentail
if (($WinSrvNumber -gt 1) -and ($Credential -eq $true)){
#Call Get-WinSvceCredential function
$WinCheckCred=1
Get-WinSvceCredential
#Get services based on function Get-WinSvceDisplayOptions
if ($DisplayOptoins){
#Call Function Get-WinSvceDisplayOptions
$WinSvceProperty = Get-WinSvceDisplayOptions
#Connect to each computer
for ($counter=0; $counter -lt $WinSrvNumber; $counter++){
#Get services
Write-Host ("Display services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
#Foreach to include server type into service details if checked
$WinGetSvc=@()
foreach ($WinSvceTypeTemp in $ServiceName){
$WinGetSvc+=Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvceTypeTemp} | Select-Object $WinSvceProperty
} #end foreach ($WinSvceTypeTemp in $ServiceName)
#Display get services after choose concern display properties
$WinGetSvc | Format-Table -Wrap -AutoSize
} #end for ($counter=0; $counter -lt $WinSrvNumber; $counter)
} #end if ($DisplayOptoins)
else{
#Connect to each computer
for ($counter=0; $counter -lt $WinSrvNumber; $counter++){
#Get services
Write-Host ("Display services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:ServiceName} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end for ($counter=0; $counter -lt $WinSrvNumber; $counter)
} #end else
} #end if (($WinSrvNumber -gt 1) -and ($Credential -eq $true))
#Get service on multi computer without credentail
if (($WinSrvNumber -gt 1) -and ($Credential -eq $false)){
#Referance value to Get-Global:WinCredential function
$WinCheckCred=0
#Get services based on function Get-WinSvceDisplayOptions
if ($DisplayOptoins){
#Call Function Get-WinSvceDisplayOptions
$WinSvceProperty = Get-WinSvceDisplayOptions
#Connect to each computer
for ($counter=0; $counter -lt $WinSrvNumber; $counter++){
#Get services
Write-Host ("Display services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
#Foreach to include server type into service details if checked
$WinGetSvc=@()
foreach ($WinSvceTypeTemp in $ServiceName){
$WinGetSvc+=Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Get-Service -Name $using:WinSvceTypeTemp} | Select-Object $WinSvceProperty
} #end foreach ($WinSvceTypeTemp in $ServiceName)
#Display get services after choose concern display properties
$WinGetSvc | Format-Table -Wrap -AutoSize
} #end for ($counter=0; $counter -lt $WinSrvNumber; $counter)
} #end if ($DisplayOptoins)
else{
#Connect to each computer
for ($counter=0; $counter -lt $WinSrvNumber; $counter++){
#Get services
Write-Host ("Display services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Get-Service -Name $using:ServiceName} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end for ($counter=0; $counter -lt $WinSrvNumber; $counter)
} #end else
} #end if (($WinSrvNumber -gt 1) -and ($Credential -eq $false))
#Remove trusted hosts
Clear-Item WSMan:\localhost\Client\TrustedHosts -Force
} #end Function Get-WinSvce
###########Get-WinSvceCredential Function###########
Function Get-WinSvceCredential {
<#
.SYNOPSIS
This function used by other functions to get crednetial and be able conenct to computer with different authentication.
#>
$Global:WinCredential=Get-Credential -Credential 'Administrator'
} #end Function Get-WinSvceCredential
##########Get-WinSvceDisplayOptions Function########
Function Get-WinSvceDisplayOptions{
<#
.SYNOPSIS
This function used by another function to get service display options.
#>
#region Import the Assemblies
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
#Get service form
$formServiceDisplayOption = New-Object 'System.Windows.Forms.Form'
$formServiceDisplayOption.AutoScaleDimensions = '6, 13'
$formServiceDisplayOption.AutoScaleMode = 'Font'
$formServiceDisplayOption.ClientSize = '371, 220'
$formServiceDisplayOption.Name = 'formServiceDisplayOption'
$formServiceDisplayOption.Text = 'Service Display Options'
#Create require service option label
$labelChooseRequireService = New-Object 'System.Windows.Forms.Label'
$labelChooseRequireService.AutoSize = $True
$labelChooseRequireService.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
$labelChooseRequireService.Location = '9, 16'
$labelChooseRequireService.Name = 'labelChooseRequireService'
$labelChooseRequireService.Size = '352, 13'
$labelChooseRequireService.TabIndex = 11
$labelChooseRequireService.Text = 'Choose require service informations would you like to display'
#Create ok button
$buttonOk = New-Object 'System.Windows.Forms.Button'
$buttonOk.DialogResult = '1'
$buttonOk.Location = '232, 45'
$buttonOk.Name = 'buttonOk'
$buttonOk.Size = '129, 34'
$buttonOk.TabIndex = 9
$buttonOk.Text = 'Ok'
$buttonOk.UseVisualStyleBackColor = $True
#Create cancel button
$buttonCancel = New-Object 'System.Windows.Forms.Button'
$buttonCancel.DialogResult = 'Cancel'
$buttonCancel.Location = '232, 89'
$buttonCancel.Name = 'buttonCancel'
$buttonCancel.Size = '129, 33'
$buttonCancel.TabIndex = 10
$buttonCancel.Text = 'Cancel'
$buttonCancel.UseVisualStyleBackColor = $True
#Create check all button
$buttonCheckAll = New-Object 'System.Windows.Forms.Button'
$buttonCheckAll.DialogResult = 'None'
$buttonCheckAll.Location = '232, 134'
$buttonCheckAll.Name = 'buttonCheckAll'
$buttonCheckAll.Size = '129, 33'
$buttonCheckAll.TabIndex = 11
$buttonCheckAll.Text = 'Check all'
$buttonCheckAll.UseVisualStyleBackColor = $True
$buttonCheckAll.Add_Click({CheckAll})
#Create uncheck all button
$buttonUncheckAll = New-Object 'System.Windows.Forms.Button'
$buttonUncheckAll.DialogResult = 'None'
$buttonUncheckAll.Location = '232, 178'
$buttonUncheckAll.Name = 'buttonUncheckAll'
$buttonUncheckAll.Size = '129, 33'
$buttonUncheckAll.TabIndex = 12
$buttonUncheckAll.Text = 'Uncheck all'
$buttonUncheckAll.UseVisualStyleBackColor = $True
$buttonUncheckAll.Add_Click({UnCheckAll})
#Create status checkbox
$checkboxSvceStatus = New-Object 'System.Windows.Forms.CheckBox'
$checkboxSvceStatus.Font = 'Microsoft Tai Le, 8.25pt, style=Bold'
$checkboxSvceStatus.Location = '12, 34'
$checkboxSvceStatus.Name = 'checkboxSvceStatus'
$checkboxSvceStatus.Size = '126, 24'
$checkboxSvceStatus.TabIndex = 1
$checkboxSvceStatus.Text = 'Service Status'
$checkboxSvceStatus.UseVisualStyleBackColor = $True
$checkboxSvceStatus.add_CheckedChanged($CheckboxSvceStatus_CheckedChanged)
$formServiceDisplayOption.ResumeLayout()
#Create require services checkbox
$checkboxSvceRequiredServices = New-Object 'System.Windows.Forms.CheckBox'
$checkboxSvceRequiredServices.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
$checkboxSvceRequiredServices.Location = '12, 55'
$checkboxSvceRequiredServices.Name = 'checkboxSvceRequiredServices'
$checkboxSvceRequiredServices.Size = '137, 24'
$checkboxSvceRequiredServices.TabIndex = 2
$checkboxSvceRequiredServices.Text = 'Required Services'
$checkboxSvceRequiredServices.UseVisualStyleBackColor = $True
#Create dependent services checkbox
$checkboxSvceDependentServices = New-Object 'System.Windows.Forms.CheckBox'
$checkboxSvceDependentServices.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
$checkboxSvceDependentServices.Location = '12, 75'
$checkboxSvceDependentServices.Name = 'checkboxSvceDependentServices'
$checkboxSvceDependentServices.Size = '148, 24'
$checkboxSvceDependentServices.TabIndex = 3
$checkboxSvceDependentServices.Text = 'Dependent Services'
$checkboxSvceDependentServices.UseVisualStyleBackColor = $True
#Create can pause and continue checkbox
$checkboxSvceCanPauseAndContinue = New-Object 'System.Windows.Forms.CheckBox'
$checkboxSvceCanPauseAndContinue.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
$checkboxSvceCanPauseAndContinue.Location = '12, 96'
$checkboxSvceCanPauseAndContinue.Name = 'checkboxSvceCanPauseAndContinue'
$checkboxSvceCanPauseAndContinue.Size = '181, 24'
$checkboxSvceCanPauseAndContinue.TabIndex = 4
$checkboxSvceCanPauseAndContinue.Text = 'Can Pause And Continue'
$checkboxSvceCanPauseAndContinue.UseVisualStyleBackColor = $True
#Create can stop checkbox
$checkboxSvceCanStop = New-Object 'System.Windows.Forms.CheckBox'
$checkboxSvceCanStop.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
$checkboxSvceCanStop.Location = '12, 117'
$checkboxSvceCanStop.Name = 'checkboxSvceCanStop'
$checkboxSvceCanStop.Size = '181, 24'
$checkboxSvceCanStop.TabIndex = 5
$checkboxSvceCanStop.Text = 'Can Stop'
$checkboxSvceCanStop.UseVisualStyleBackColor = $True
#Create start type checkbox
$checkboxSvceStartType = New-Object 'System.Windows.Forms.CheckBox'
$checkboxSvceStartType.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
$checkboxSvceStartType.Location = '12, 138'
$checkboxSvceStartType.Name = 'checkboxSvceStartType'
$checkboxSvceStartType.Size = '181, 24'
$checkboxSvceStartType.TabIndex = 6
$checkboxSvceStartType.Text = 'Start Type'
$checkboxSvceStartType.UseVisualStyleBackColor = $True
#Create name checkbox
$checkboxSvceName = New-Object 'System.Windows.Forms.CheckBox'
$checkboxSvceName.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
$checkboxSvceName.Location = '12, 159'
$checkboxSvceName.Name = 'checkboxSvceName'
$checkboxSvceName.Size = '181, 24'
$checkboxSvceName.TabIndex = 7
$checkboxSvceName.Text = 'Name'
$checkboxSvceName.UseVisualStyleBackColor = $True
#Create display name checkbox
$checkboxSvceDisplayName = New-Object 'System.Windows.Forms.CheckBox'
$checkboxSvceDisplayName.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
$checkboxSvceDisplayName.Location = '12, 180'
$checkboxSvceDisplayName.Name = 'checkboxSvceDisplayName'
$checkboxSvceDisplayName.Size = '181, 24'
$checkboxSvceDisplayName.TabIndex = 8
$checkboxSvceDisplayName.Text = 'Display Name'
$checkboxSvceDisplayName.UseVisualStyleBackColor = $True
#Add objects to form
$formServiceDisplayOption.Controls.Add($labelChooseRequireService)
$formServiceDisplayOption.Controls.Add($buttonOk)
$formServiceDisplayOption.Controls.Add($buttonCancel)
$formServiceDisplayOption.Controls.Add($buttonCheckAll)
$formServiceDisplayOption.Controls.Add($buttonUncheckAll)
$formServiceDisplayOption.Controls.Add($checkboxSvceStatus)
$formServiceDisplayOption.Controls.Add($checkboxSvceRequiredServices)
$formServiceDisplayOption.Controls.Add($checkboxSvceDependentServices)
$formServiceDisplayOption.Controls.Add($checkboxSvceCanPauseAndContinue)
$formServiceDisplayOption.Controls.Add($checkboxSvceCanStop)
$formServiceDisplayOption.Controls.Add($checkboxSvceStartType)
$formServiceDisplayOption.Controls.Add($checkboxSvceName)
$formServiceDisplayOption.Controls.Add($checkboxSvceDisplayName)
#Functoin to checll all service display options
Function CheckAll{
if ($buttonCheckAll.DialogResult -eq 'None'){
$checkboxSvceStatus.Checked = $True
$checkboxSvceRequiredServices.Checked = $True
$checkboxSvceDependentServices.Checked = $True
$checkboxSvceCanPauseAndContinue.Checked = $True
$checkboxSvceCanStop.Checked = $True
$checkboxSvceStartType.Checked = $True
$checkboxSvceName.Checked = $True
$checkboxSvceDisplayName.Checked = $True
} #end if ($buttonCheckAll.DialogResult -eq 'None')
} #end Function CheckAll
#Functoin to unchecll all service display options
Function UnCheckAll{
if ($buttonCheckAll.DialogResult -eq 'None'){
$checkboxSvceStatus.Checked = $False
$checkboxSvceRequiredServices.Checked = $False
$checkboxSvceDependentServices.Checked = $False
$checkboxSvceCanPauseAndContinue.Checked = $False
$checkboxSvceCanStop.Checked = $False
$checkboxSvceStartType.Checked = $False
$checkboxSvceName.Checked = $False
$checkboxSvceDisplayName.Checked = $False
} #end if ($buttonCheckAll.DialogResult -eq 'None')
} #end Function UnCheckAll
#Show the Form
$formServiceDisplayOption.ShowDialog() | Out-Null
#Add service display options to array
$WinSvceOptions=@()
if ($checkboxSvceStatus.Checked){
$WinSvceOptions+='Status'
} #end if ($checkboxSvceStatus.Checked)
if ($checkboxSvceRequiredServices.Checked){
$WinSvceOptions+=@{Label='RequiredServices'; Expression={($_.RequiredServices) -join "`n"}}
} #end if ($checkboxSvceRequiredServices.Checked)
if ($checkboxSvceDependentServices.Checked){
$WinSvceOptions+=@{Label='DependentServices'; Expression={($_.DependentServices) -join "`n"}}
} #end if ($checkboxSvceDependentServices.Checked)
if ($checkboxSvceCanPauseAndContinue.Checked){
$WinSvceOptions+='CanPauseAndContinue'
} #end if ($checkboxSvceCanPauseAndContinue.Checked)
if ($checkboxSvceCanStop.Checked){
$WinSvceOptions+='CanStop'
} #end if ($checkboxSvceCanStop.Checked)
#StartType checkbox for one computer
if (($WinSrvNumber -eq 1) -and ($WinCheckCred -eq 1)){
if ($checkboxSvceStartType.Checked){
$WinSvceOptions+=@{Label='StartType'; Expression={Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{(Get-CimInstance Win32_Service | ?{$_.name -match $using:WinSvceTypeTemp}).StartMode}}}
} #end if ($checkboxSvceStartType.Checked)
} #end if (($WinSrvNumber -eq 1) -and ($WinCheckCred -eq 1))
elseif (($WinSrvNumber -eq 1) -and ($WinCheckCred -eq 0)){
if ($checkboxSvceStartType.Checked){
$WinSvceOptions+=@{Label='StartType'; Expression={Invoke-Command -ComputerName $ComputerName -ScriptBlock{(Get-CimInstance Win32_Service | ?{$_.name -match $using:WinSvceTypeTemp}).StartMode}}}
} #end if ($checkboxSvceStartType.Checked)
} #end elseif (($WinSrvNumber -eq 1) -and ($WinCheckCred -eq 0))
#StartType checkbox for multi computer
if (($WinSrvNumber -gt 1) -and ($WinCheckCred -eq 1)){
if ($checkboxSvceStartType.Checked){
$WinSvceOptions+=@{Label='StartType'; Expression={Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{(Get-CimInstance Win32_Service | ?{$_.name -match $using:WinSvceTypeTemp}).StartMode}}}
} #end if ($checkboxSvceStartType.Checked)
} #end if (($WinSrvNumber -gt 1) -and ($WinCheckCred -eq 1))
elseif (($WinSrvNumber -gt 1) -and ($WinCheckCred -eq 0)){
if ($checkboxSvceStartType.Checked){
$WinSvceOptions+=@{Label='StartType'; Expression={Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{(Get-CimInstance Win32_Service | ?{$_.name -match $using:WinSvceTypeTemp}).StartMode}}}
} #end if ($checkboxSvceStartType.Checked)
} #end elseif (($WinSrvNumber -gt 1) -and ($WinCheckCred -eq 0))
if ($checkboxSvceName.Checked){
$WinSvceOptions+='Name'
} #end if ($checkboxSvceName.Checked)
if ($checkboxSvceDisplayName.Checked){
$WinSvceOptions+='DisplayName'
} #end if ($checkboxSvceDisplayName.Checked)
#Output service display options
$WinSvceOptions
} #end Functoin Get-WinSvceDisplayOptions
###############Start-WinSvce Function################
Function Start-WinSvce {
<#
.SYNOPSIS
This function help you to start needed service, no matter how many computers, services or apply it on local machine or even remotely.
.DESCRIPTION
Use this function to control start services, also it can bring up all required services as prerequisite before run mentioned service.
.PARAMETER ComputerName
Indicates the property to provide one computername or many .
.PARAMETER ServiceName
Indicates the property to provide one service name or many .
.PARAMETER RequiredService
Indicates the property to get and run required services where mentioned service depend on them before start .
.PARAMETER Credential
Indicates the property to call another function under name Get-WinSvceCredential and be able connect on computer with different credentials.
.EXAMPLE
Start-WinSvce -ComputerName Server1 -ServiceName W32Time
.EXAMPLE
Get-WinSvce -ComputerName Server1,Server2 -ServiceName W32Time,iphlpsvc -RequiredService
.EXAMPLE
Get-WinSvce -ComputerName Server1 -ServiceName W32Time,iphlpsvc -RequiredService -Credential
#>
param ($ComputerName,
$ServiceName,
[Switch]$RequiredService,
[Switch]$Credential
)
#Set trusted hosts
Set-Item WSMan:\localhost\Client\TrustedHosts –Value “*” -ea 0 -Force
#Get number of computers
$WinSrvNumber=($ComputerName).count
#Run service on one computer with credential
if (($WinSrvNumber -eq 1) -and ($Credential -eq $true)){
#Call Get-WinSvceCredential function
Get-WinSvceCredential
#At begin run required services
if ($RequiredService){
#Get required services
$WinGetSvcDepedning=Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:ServiceName}
$WinArraySvcDepnding=$WinGetSvcDepedning.RequiredServices | Sort-Object -Unique
Write-Host ("At begin it must run required services on computername: " + $ComputerName) -ForegroundColor Green
#Run required services
$WinGetSvceList=@()
Foreach ($WinSvceDependTemp in $WinArraySvcDepnding) {
Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Start-Service -Name $using:WinSvceDependTemp}
$WinGetSvceList+=Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvceDependTemp}
} #end foreach ($WinSvceDependTemp in $WinArraySvcDepnding)
$WinGetSvceList | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
Start-Sleep -Milliseconds 3000
} #end if ($RequiredService)
#Run mentioned service
Write-Host ("Run needed services on computername: " + $ComputerName) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Start-Service -Name $using:ServiceName}
Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:ServiceName} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end if (($WinSrvNumber -eq 1) -and ($Credential -eq $true))
#Run service on one computer without credential
if (($WinSrvNumber -eq 1) -and ($Credential -eq $false)){
#At begin run required services
if ($RequiredService){
#Get required services
$WinGetSvcDepedning=Invoke-Command -ComputerName $ComputerName -ScriptBlock{Get-Service -Name $using:ServiceName}
$WinArraySvcDepnding=$WinGetSvcDepedning.RequiredServices | Sort-Object -Unique
Write-Host ("At begin it must run required services on computername: " + $ComputerName) -ForegroundColor Green
#Run required services
$WinGetSvceList=@()
Foreach ($WinSvceDependTemp in $WinArraySvcDepnding) {
Invoke-Command -ComputerName $ComputerName -ScriptBlock{Start-Service -Name $using:WinSvceDependTemp}
$WinGetSvceList+=Invoke-Command -ComputerName $ComputerName -ScriptBlock{Get-Service -Name $using:WinSvceDependTemp}
} #end foreach ($WinSvceDependTemp in $WinArraySvcDepnding)
$WinGetSvceList | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
Start-Sleep -Milliseconds 3000
} #end if ($RequiredService)
#Run mentioned service
Write-Host ("Run needed services on computername: " + $ComputerName) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName -ScriptBlock{Start-Service -Name $using:ServiceName}
Invoke-Command -ComputerName $ComputerName -ScriptBlock{Get-Service -Name $using:ServiceName} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end if (($WinSrvNumber -eq 1) -and ($Credential -eq $false))
# Run service on multi computer with credential
if (($WinSrvNumber -gt 1) -and ($Credential -eq $true)){
#Call Get-WinSvceCredential function
Get-WinSvceCredential
#Connect to each computer
for ($counter=0; $counter -lt $WinSrvNumber; $counter++){
#At begin run required services
if ($RequiredService){
#get required services
$WinArraySvcDepnding=$null
$WinGetSvcDepedingSrv=Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:ServiceName}
$WinArraySvcDepnding=$WinGetSvcDepedingSrv.RequiredServices | Sort-Object -Unique
Write-Host ("At begin it must run required services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
#Run required services
$WinGetSvceList=@()
Foreach ($WinSvceDependTemp in $WinArraySvcDepnding) {
Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Start-Service -Name $using:WinSvceDependTemp}
$WinGetSvceList+=Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvceDependTemp}
} #end foreach ($WinSvceDependTemp in $WinArraySvcDepnding)
$WinArraySvcDepnding=$null
$WinGetSvceList | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
Start-Sleep -Milliseconds 3000
} #end if ($RequiredService)
#Run mentioned service
Write-Host ("Run needed services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Start-Service -Name $using:ServiceName}
Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:ServiceName} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end for ($counter=0; $counter -lt $WinSrvNumber; $counter)
} #end if (($WinSrvNumber -gt 1) -and ($Credential -eq $true))
# Run service on multi computer without credential
if (($WinSrvNumber -gt 1) -and ($Credential -eq $false)){
#Connect to each computer
for ($counter=0; $counter -lt $WinSrvNumber; $counter++){
#At begin run required services
if ($RequiredService){
#get required services
$WinArraySvcDepnding=$null
$WinGetSvcDepedingSrv=Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Get-Service -Name $using:ServiceName}
$WinArraySvcDepnding=$WinGetSvcDepedingSrv.RequiredServices | Sort-Object -Unique
Write-Host ("At begin it must run required services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
#Run required services
$WinGetSvceList=@()
Foreach ($WinSvceDependTemp in $WinArraySvcDepnding) {
Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Start-Service -Name $using:WinSvceDependTemp}
$WinGetSvceList+=Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Get-Service -Name $using:WinSvceDependTemp}
} #end foreach ($WinSvceDependTemp in $WinArraySvcDepnding)
$WinArraySvcDepnding=$null
$WinGetSvceList | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
Start-Sleep -Milliseconds 3000
} #end if ($RequiredService)
#Run mentioned service
Write-Host ("Run needed services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Start-Service -Name $using:ServiceName}
Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Get-Service -Name $using:ServiceName} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end for ($counter=0; $counter -lt $WinSrvNumber; $counter)
} #end if (($WinSrvNumber -gt 1) -and ($Credential -eq $false))
#Remove trusted hosts
Clear-Item WSMan:\localhost\Client\TrustedHosts -Force
} #end Function Start-WinSvce
###############Stop-WinSvce Function################
Function Stop-WinSvce {
<#
.SYNOPSIS
This function help you to stop needed service, no matter how many computers, services or apply it on local machine or even remotely.
.DESCRIPTION
Use this function to manage stop services, also it can bring and stop dependent services before shutdown mentioned service.
.PARAMETER ComputerName
Indicates the property to provide one computername or many .
.PARAMETER ServiceName
Indicates the property to provide one service name or many .
.PARAMETER DependentServices
Indicates the property to find dependent services to be stopped before shutdown mentioned service by call another function name Stop-WinSvceCanStop to collect only services can stop into array.
.PARAMETER Credential
Indicates the property to call another function under name Get-WinSvceCredential and be able connect on computer with different credentials.
.EXAMPLE
Stop-WinSvce -ComputerName Server1 -ServiceName W32Time
.EXAMPLE
Stop-WinSvce -ComputerName Server1,Server2 -ServiceName W32Time,KeyIso -DependentServices
.EXAMPLE
Stop-WinSvce -ComputerName Server1 -ServiceName W32Time,KeyIso -DependentServices -Credential
#>
param ($ComputerName,
$ServiceName,
[Switch]$DependentServices,
[Switch]$Credential
)
#Set trusted hosts
Set-Item WSMan:\localhost\Client\TrustedHosts –Value “*” -ea 0 -Force
#Get number of computers
$WinSrvNumber=($ComputerName).count
#Stop service on one computer with credential
if (($WinSrvNumber -eq 1) -and ($Credential -eq $true)){
#Call Get-WinSvceCredential function
Get-WinSvceCredential
$Global:WinSvceCredential=1
#Call Function Stop-WinSvceCanStop
Stop-WinSvceCanStop -ComputerName $ComputerName -ServiceName $ServiceName -Credential $Global:WinCredential
$WinSvcFinal=@($Global:ServiceNameCanStop)
#At begin stop depending services
if ($DependentServices){
#Get depending services
$WinGetSvcDepedning=Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvcFinal}
$WinArraySvcDepnding=$WinGetSvcDepedning.DependentServices | Sort-Object -Unique
Write-Host ("`n" + "At begin it must stop dependent services on computername: " + $ComputerName) -ForegroundColor Green
#stop depending services
$WinGetSvceList=@()
Foreach ($WinSvceDependTemp in $WinArraySvcDepnding) {
Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Stop-Service -Name $using:WinSvceDependTemp}
$WinGetSvceList+=Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvceDependTemp}
} #end foreach ($WinSvceDependTemp in $WinArraySvcDepnding)
$WinGetSvceList | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
Start-Sleep -Milliseconds 3000
} #end if ($DependentServices)
#If WinSvc include stop services
if (($WinSvcFinal).count -gt 0){
#Stop mentioned service
Write-Host ("`n" + "Stop needed services on computername: " + $ComputerName) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Stop-Service -Name $using:WinSvcFinal}
Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvcFinal} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end if (($WinSvcFinal).count -eq $null)
else{
Write-Host ("There isn't any services can stop on computername: " + $ComputerName) -ForegroundColor Red
} #end else
} #end if (($WinSrvNumber -eq 1) -and ($Credential -eq $true))
#Stop service on one computer without credential
if (($WinSrvNumber -eq 1) -and ($Credential -eq $false)){
#Call Function Stop-WinSvceCanStop
$Global:WinSvceCredential=0
Stop-WinSvceCanStop -ComputerName $ComputerName -ServiceName $ServiceName
$WinSvcFinal=@($Global:ServiceNameCanStop)
#At begin stop depending services
if ($DependentServices){
#Get depending services
$WinGetSvcDepedning=Invoke-Command -ComputerName $ComputerName -ScriptBlock{Get-Service -Name $using:WinSvcFinal}
$WinArraySvcDepnding=$WinGetSvcDepedning.DependentServices | Sort-Object -Unique
Write-Host ("`n" + "At begin it must stop dependent services on computername: " + $ComputerName) -ForegroundColor Green
#stop depending services
$WinGetSvceList=@()
Foreach ($WinSvceDependTemp in $WinArraySvcDepnding) {
Invoke-Command -ComputerName $ComputerName -ScriptBlock{Stop-Service -Name $using:WinSvceDependTemp}
$WinGetSvceList+=Invoke-Command -ComputerName $ComputerName -ScriptBlock{Get-Service -Name $using:WinSvceDependTemp}
} #end foreach ($WinSvceDependTemp in $WinArraySvcDepnding)
$WinGetSvceList | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
Start-Sleep -Milliseconds 3000
} #end if ($DependentServices)
#If WinSvc include stop services
if (($WinSvcFinal).count -gt 0){
#Stop mentioned service
Write-Host ("`n" + "Stop needed services on computername: " + $ComputerName) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName -ScriptBlock{Stop-Service -Name $using:WinSvcFinal}
Invoke-Command -ComputerName $ComputerName -ScriptBlock{Get-Service -Name $using:WinSvcFinal} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end if (($WinSvcFinal).count -eq $null)
else{
Write-Host ("There isn't any services can stop on computername: " + $ComputerName) -ForegroundColor Red
} #end else
} #end if (($WinSrvNumber -eq 1) -and ($Credential -eq $false))
#Stop service on multi computer with credential
if (($WinSrvNumber -gt 1) -and ($Credential -eq $true)){
#Call Get-WinSvceCredential function
Get-WinSvceCredential
$Global:WinSvceCredential=1
#Connect to each computer
for ($counter=0; $counter -lt $WinSrvNumber; $counter++){
#Call Function Stop-WinSvceCanStop
Stop-WinSvceCanStop -ComputerName $ComputerName[$counter] -ServiceName $ServiceName -Credential $Global:WinCredential
$WinSvcFinal=@($Global:ServiceNameCanStop)
#At begin stop depending services
if ($DependentServices){
#get depending services
$WinArraySvcDepnding=$null
$WinGetSvcDepedingSrv=Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvcFinal}
$WinArraySvcDepnding=$WinGetSvcDepedingSrv.DependentServices | Sort-Object -Unique
Write-Host ("At begin it must stop dependent services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
#Stop depending services
$WinGetSvceList=@()
Foreach ($WinSvceDependTemp in $WinArraySvcDepnding) {
Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Stop-Service -Name $using:WinSvceDependTemp}
$WinGetSvceList+=Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvceDependTemp}
} #end foreach ($WinSvceDependTemp in $WinArraySvcDepnding)
$WinArraySvcDepnding=$null
$WinGetSvceList | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
Start-Sleep -Milliseconds 3000
} #end if ($DependentServices)
#If WinSvc include services
if (($WinSvcFinal).count -gt 0){
#Stop mentioned service
Write-Host ("Stop needed services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Stop-Service -Name $using:WinSvcFinal}
Invoke-Command -ComputerName $ComputerName[$counter] -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvcFinal} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end if (($WinSvcFinal).count -eq $null)
else{
Write-Host ("There isn't any services can stop on computername: " + $ComputerName[$counter]) -ForegroundColor Red
} #end else
} #end for ($counter=0; $counter -lt $WinSrvNumber; $counter)
} #end if (($WinSrvNumber -gt 1) -and ($Credential -eq $true))
#Stop service on multi computer without credential
if (($WinSrvNumber -gt 1) -and ($Credential -eq $false)){
#Connect to each computer
for ($counter=0; $counter -lt $WinSrvNumber; $counter++){
#Call Function Stop-WinSvceCanStop
$Global:WinSvceCredential=0
Stop-WinSvceCanStop -ComputerName $ComputerName[$counter] -ServiceName $ServiceName
$WinSvcFinal=@($Global:ServiceNameCanStop)
#At begin stop depending services
if ($DependentServices){
#get depending services
$WinArraySvcDepnding=$null
$WinGetSvcDepedingSrv=Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Get-Service -Name $using:WinSvcFinal}
$WinArraySvcDepnding=$WinGetSvcDepedingSrv.DependentServices | Sort-Object -Unique
Write-Host ("At begin it must stop dependent services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
#Stop depending services
$WinGetSvceList=@()
Foreach ($WinSvceDependTemp in $WinArraySvcDepnding) {
Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Stop-Service -Name $using:WinSvceDependTemp}
$WinGetSvceList+=Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Get-Service -Name $using:WinSvceDependTemp}
} #end foreach ($WinSvceDependTemp in $WinArraySvcDepnding)
$WinArraySvcDepnding=$null
$WinGetSvceList | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
Start-Sleep -Milliseconds 3000
} #end if ($DependentServices)
#If WinSvc include services
if (($WinSvcFinal).count -gt 0){
#Stop mentioned service
Write-Host ("Stop needed services on computername: " + $ComputerName[$counter]) -ForegroundColor Green
Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Stop-Service -Name $using:WinSvcFinal}
Invoke-Command -ComputerName $ComputerName[$counter] -ScriptBlock{Get-Service -Name $using:WinSvcFinal} | Format-Table -Property Status, Name, DisplayName -Wrap -AutoSize
} #end if (($WinSvcFinal).count -eq $null)
else{
Write-Host ("There isn't any services can stop on computername: " + $ComputerName[$counter]) -ForegroundColor Red
} #end else
} #end for ($counter=0; $counter -lt $WinSrvNumber; $counter)
} #end if (($WinSrvNumber -gt 1) -and ($Credential -eq $false))
#Remove trusted hosts
Clear-Item WSMan:\localhost\Client\TrustedHosts -Force
} #end Function Stop-WinSvce
###############Stop-WinSvceCanStop Function################
Function Stop-WinSvceCanStop {
<#
.SYNOPSIS
This function used by another function to include stoppable services into global array.
#>
param ($ComputerName,
$ServiceName,
[Switch]$Credential)
#Set trusted hosts
Set-Item WSMan:\localhost\Client\TrustedHosts –Value “*” -ea 0 -Force
#Force array to accept one value or more
[System.Collections.ArrayList]$WinSvceArray=@($ServiceName)
#Verfiy invoke command with credential or not
if ($Global:WinSvceCredential -eq 1){
$WinGetSvce=Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvceArray}
} #end if ($Global:WinSvceCredential -eq 1)
elseif ($Global:WinSvceCredential -eq 0){
$WinGetSvce=Invoke-Command -ComputerName $ComputerName -ScriptBlock{Get-Service -Name $using:WinSvceArray}
} #end elseif ($Global:WinSvceCredential -eq 0)
$WinSvceNumber=($ServiceName).Count
#Remove unable service to stop
for ($counter=0; $counter -lt $WinSvceNumber; $counter++){
if ($WinGetSvce[$counter].CanStop -eq $false){
Write-Host ("The service name ") -ForegroundColor Yellow -NoNewline
Write-Host (($WinGetSvce[$counter]).DisplayName) -ForegroundColor Green -NoNewline
Write-Host (" is unble to stop or already stopped before on computername: ") -ForegroundColor Yellow -NoNewline
Write-Host ($ComputerName) -ForegroundColor Green
$WinSvceArray.Remove(($WinGetSvce[$counter]).Name)
} #end if ($WinGetSvce[$counter].CanStop -eq $false)
} #end for ($counter=0; $counter -lt $WinSvceNumber; $counter++)
#Service name output
$Global:ServiceNameCanStop=$WinSvceArray
} #end Function Stop-WinSvceCanStop
###############Resume-WinSvce Function################
Function Resume-WinSvce {
<#
.SYNOPSIS
This function help you to resume needed service, no matter how many computers, services or apply it on local machine or even remotely.
.DESCRIPTION
Use this function to control resume services, no need to consider if a service resumable or not because it has ability to find out resumable services from many provided services through command typed.
.PARAMETER ComputerName
Indicates the property to provide one computername or many .
.PARAMETER ServiceName
Indicates the property to provide one service name or many .
.PARAMETER Credential
Indicates the property to call another function under name Get-WinSvceCredential and be able connect on computer with different credentials.
.EXAMPLE
Resume-WinSvce -ComputerName Server1 -ServiceName W32Time
.EXAMPLE
Resume-WinSvce -ComputerName Server1 -ServiceName W32Time,iphlpsvc
.EXAMPLE
Resume-WinSvce -ComputerName Server1,Server2 -ServiceName W32Time,iphlpsvc -Credential
#>
param ($ComputerName,
$ServiceName,
[Switch]$Credential)
#Set trusted hosts
Set-Item WSMan:\localhost\Client\TrustedHosts –Value “*” -ea 0 -Force
#Get number of computers
$WinSrvNumber=($ComputerName).count
#Resume service on one computer with credential
if (($WinSrvNumber -eq 1) -and ($Credential -eq $true)){
#Call Get-WinSvceCredential function
Get-WinSvceCredential
#Create variable accept single or multi values
[System.Collections.ArrayList]$WinSvceName=@($ServiceName)
#Verify ability to resume service
$WinGetSvce=Invoke-Command -ComputerName $ComputerName -Credential $Global:WinCredential -ScriptBlock{Get-Service -Name $using:WinSvceName}
$WinGetSvceResume=$WinGetSvce.CanPauseAndContinue