forked from guyrleech/Citrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet PVS device info.ps1
More file actions
882 lines (725 loc) · 37.5 KB
/
Get PVS device info.ps1
File metadata and controls
882 lines (725 loc) · 37.5 KB
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
#Requires -version 3.0
<#
Grab all PVS devices and their collections, machine catalogues, delivery groups and more and output to csv or a grid view
Use of this script is entirely at the risk of the user. The author accepts no liability or responsibility for any undesired issues it may cause
Guy Leech, 2018
Modification history:
19/01/18 GL Fixed issue where not showing not booted off latest if different vdisk assigned from what is booted off
Fixed issue with PoSH v5
Added -name filter and -tags
Added warning for pre PoSH v5 and more than 30 columns in Out-GridView
Added error trapping when fails to retrive boot time
22/01/18 GL Added action GUI
26/01/18 GL Added ability to match AD group regex and output group memberships
01/02/18 GL Added disk version descriptions and dns lookup option
22/02/18 GL Added capability to include machines retrieved from DDCs which are not present in PVS
Changed main device collection to .NET array so we can add potential orphans to it
Added MaxRecordCount parameter for DDC
25/02/18 GL Added GUI options to remove from PVS and DDC
27/02/18 GL Added saving DDC and PVS servers to registry
14/03/18 GL Changed AD Account Exists to Created date
16/03/18 GL "Remove from AD" and "Remove from Hypervisor" (VMware) actions added
Added ability to identify orphaned VMs in VMware
Include VM information if have hypervisor connection (VMware)
Added progress bar
17/03/18 GL Completion message after actions completed with error count
18/03/18 GL Disable Remove from Hypervisor and AD buttons if not available
Added profiling and optimised
Made MessageBox calls app modal as well as system
20/03/18 GL Added remote memory, CPU and hard disk status via WMI/CIM
Added -help option
21/03/18 GL Changed action UI to use context menus rather than buttons
Added vdisk file size
06/04/18 GL Retry count, device IP and PVS server booted from fields added
-noGridview option added and will produce both csv and gridview if -csv specified unless -nogridview used
17/04/18 GL Implemented timeout when getting remote information as Invoke-Command can take up to 25 minutes to timeout.
Added remote domain health check via Test-ComputerSecureChannel and AD account modification time
30/05/18 GL Added Load indexes
01/06/18 GL Changed to run device queries in runspaces to speed up
19/06/18 GL Split main code into module so can re-use in other scripts
20/06/18 GL Added option to split VMware VM names in case have description, etc after an _ character or similar
#>
<#
.SYNOPSIS
Produce grid view or csv report of Citrix Provisioning Services devices, cross referenced with information from Delivery Controllers and Active Directory
.DESCRIPTION
Alows easy identification of potential problems such as devices booted off the wrong vdisk or vdisk version, devices with no AD account or devices overdue a reboot.
Devices can be selected in the grid view and then shutdown, restarted, booted or maintenance mode turned on or off from a simple GUI
.PARAMETER pvsServers
Comma separated list of PVS servers to contact. Do not specify multiple servers if they use the same SQL database
.PARAMETER ddcs
Comma separated list of Delivery Controllers to contact. Do not specify multiple servers if they use the same SQL database
.PARAMETER registry
Pick up the PVS and DDC servers from the registry rather than the command line. Must previously have saved them with -save and an optional server set name via -serverSet
.PARAMETER serverSet
Pick up the PVS and DDC servers from the registry sub key specified in this paramter rather than the command line. Must previously have saved them with -save and this server set name
.PARAMETER save
Save the PVS and DDC servers to the registry for later use with -registry. Use -serverSet to specify named sets of servers, e.g. preproduction and production
.PARAMETER csv
Path to a csv file that will have the results written to it.
.PARAMETER noGridView
Do not produce an on screen grid view containing the results. Default behaviour will display one.
.PARAMETER cpuSamples
The number of CPU usage samples to gather from a remote system if -noRemoting is not specified. The higher the number, the more accurate the figure but the longer it will take.
Set to zero to not gather CPU but still gather other remote data like memory usage and free disk space
.PARAMETER noRemoting
Will not try and contact active devices to gather information like last boot time. If WinRM not setup correctly or other issues mean remote calls will fail then this option can speed the script up
.PARAMETER noOrphans
Do not display machines which are present on DDCs/Studio but not in PVS. May be physical, MCS or VMs with local disks or could be orphans that did exist in PVS but do not any longer.
.PARAMETER jobTimeout
Timeout in seconds for the command to retrieve information remotely from a device.
.PARAMETER maxRecordCount
Citrix DDC cmdlets by default only return 250 items so if there are more machines than this on a DDC then this parameter may need to be increased
.PARAMETER name
Only show devices whose name matches the regular expression specified
.PARAMETER tags
Adds a column containing Citrix tags, if present, for each device
.PARAMETER ADGroups
A regular expression of AD groups to match and will output ones that match for which the device is a member
.PARAMETER noProgress
Do not show a progress bar
.PARAMETER hypervisors
Comma separated list of VMware vSphere servers to connect to in order to gather VM information such as memory and CPU configuration.
Will prompt for authentication if no pass thru or saved credentials
.PARAMETER vmPattern
When hypervisors are specified, or retrieved from the registry, a regular expression must be specified to match the VM names in vCenter to prevent all VMs
being included rather than just XenApp/XenDesktop ones
.PARAMETER provisioningType
The type of catalogue provisioning to check for in machines that are suspected of being orphaned from PVS as they were found on a DDC but not PVS
.PARAMETER dns
Perform DNS ipV4 lookup. This can cause the script to run slower.
.PARAMETER noMenu
Do not display "OK" and "Cancel" buttons on grid view so no action menu is presented after grid view is closed
.PARAMETER messageText
Text to send to users in the action GUI. If none specified and the message option is clicked then you will be prompted for the text
.PARAMETER messageCaption
Caption of the message to send to users in the action GUI. If none specified and the message option is clicked then you will be prompted for the text
.PARAMETER timeout
Timeout in seconds for PVS power commands
.PARAMETER splitVM
Split VMware name on a specific character such as an underscore where the second and subsequent parts of the name are descriptions, etc
.PARAMETER help
Show full help via Get-Help cmdlet
.EXAMPLE
& '.\Get PVS device info.ps1' -pvsservers pvsprod01,pvstest01 -ddcs ddctest02,ddcprod03
Retrieve devices from the listed PVS servers, cross reference to the listed DDCs (list order does not matter) and display on screen in a sortable and filterable grid view
.EXAMPLE
& '.\Get PVS device info.ps1' -pvsservers pvsprod01 -ddcs ddcprod03 -name CTXUAT -tags -dns -csv h:\pvs.ctxuat.csv
Retrieve devices matching regular expression CTXUAT from the listed PVS server, cross reference to the listed DDC and output to the given csv file, including Citrix tag information and IPv4 address from DNS query
.EXAMPLE
& '.\Get PVS device info.ps1' -pvsservers pvsprod01 -ddcs ddcprod03 -ADGroups '^GRP-' -hypervisors vmware01 -vmPattern '^CTX[15]\d{3}$'
Retrieve all devices from the listed PVS server, cross reference to the listed DDC and VMware, including AD groups which start withg "GRP", and output to an on-screen grid view
.NOTES
Uses local PowerShell cmdlets for PVS, DDCs and VMware, as well as Active Directory, so run from a machine where both PVS and Studio consoles and the VMware PowerCLI are installed.
#>
[CmdletBinding()]
Param
(
[Parameter(ParameterSetName='Manual',mandatory=$true,HelpMessage='Comma separated list of PVS servers')]
[string[]]$pvsServers ,
[Parameter(ParameterSetName='Manual',mandatory=$true,HelpMessage='Comma separated list of Delivery controllers')]
[string[]]$ddcs ,
[Parameter(ParameterSetName='Manual',mandatory=$false)]
[switch]$save ,
[Parameter(ParameterSetName='Registry',mandatory=$true,HelpMessage='Use default server set name from registry')]
[switch]$registry ,
[string]$serverSet = 'Default' ,
[string[]]$hypervisors ,
[string]$csv ,
[switch]$dns ,
[string]$name ,
[switch]$tags ,
[string]$ADgroups ,
[switch]$noProgress ,
[switch]$noRemoting ,
[switch]$noMenu ,
[switch]$noOrphans ,
[switch]$noGridView ,
[ValidateSet('PVS','MCS','Manual','Any')]
[string]$provisioningType = 'PVS' ,
[string]$configRegKey = 'HKCU:\software\Guy Leech\PVS Fetcher' ,
[string]$messageText ,
[string]$messageCaption ,
[int]$maxRecordCount = 2000 ,
[int]$jobTimeout = 120 ,
[int]$timeout = 60 ,
[int]$cpuSamples = 2 ,
[int]$maxThreads = 10 ,
[string]$pvsShare ,
[string]$splitVM ,
[switch]$help ,
[string[]]$snapins = @( 'Citrix.Broker.Admin.*' ) ,
[string[]]$modules = @( 'ActiveDirectory', "$env:ProgramFiles\Citrix\Provisioning Services Console\Citrix.PVS.SnapIn.dll" , 'Guys.Common.Functions.psm1' ) ,
[string]$vmWareModule = 'VMware.VimAutomation.Core'
)
if( $help )
{
Get-Help -Name ( & { $myInvocation.ScriptName } ) -Full
return
}
$columns = [System.Collections.ArrayList]( @( 'Name','DomainName','Description','PVS Server','DDC','SiteName','CollectionName','Machine Catalogue','Delivery Group','Load Index','Load Indexes','Registration State','Maintenance_Mode','User_Sessions','devicemac','active','enabled',
'Store Name','Disk Version Access','Disk Version Created','AD Account Created','AD Account Modified','Domain Membership','AD Last Logon','AD Description','Disk Name','Booted off vdisk','Booted Disk Version','Vdisk Production Version','Vdisk Latest Version','Latest Version Description','Override Version',
'Retries','Booted Off','Device IP','Booted off latest','Disk Description','Cache Type','Disk Size (GB)','vDisk Size (GB)','Write Cache Size (MB)' ) )
if( $dns )
{
$null = $columns.Add( 'IPv4 address' )
}
if( $tags )
{
$null = $columns.Add( 'Tags')
}
if( ! [string]::IsNullOrEmpty( $ADgroups ) )
{
$null = $columns.Add( 'AD Groups')
}
if( ! $noRemoting )
{
$null = $columns.Add( 'Boot_Time' )
$null = $columns.Add( 'Available Memory (GB)' )
$null = $columns.Add( 'Committed Memory %' )
$null = $columns.Add( 'Free disk space %' )
if( $cpuSamples -gt 0 )
{
$null = $columns.Add( 'CPU Usage %' )
}
}
if( $snapins -and $snapins.Count -gt 0 )
{
ForEach( $snapin in $snapins )
{
Add-PSSnapin $snapin -ErrorAction Continue
}
}
ForEach( $module in $modules )
{
Import-Module $module -ErrorAction SilentlyContinue
[bool]$loaded = $?
if( ! $loaded -and $module -notmatch '^[a-z]:\\' -and $module -notmatch '^\\\\' ) ## only check script folder if not an absolute or UNC path
{
## try same folder as the script if there is no path in the module name
Import-Module (Join-Path ( & { Split-Path -Path $myInvocation.ScriptName -Parent } ) $module ) -ErrorAction Continue
$loaded = $?
}
if( ! $loaded )
{
Write-Warning "Unable to load module `"$module`" so functionality may be limited"
}
}
$messageWindowXAML = @"
<Window x:Class="Direct2Events.MessageWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Direct2Events"
mc:Ignorable="d"
Title="Send Message" Height="414.667" Width="309.333">
<Grid>
<TextBox x:Name="txtMessageCaption" HorizontalAlignment="Left" Height="53" Margin="85,20,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="180"/>
<Label Content="Caption" HorizontalAlignment="Left" Height="24" Margin="10,20,0,0" VerticalAlignment="Top" Width="63"/>
<TextBox x:Name="txtMessageBody" HorizontalAlignment="Left" Height="121" Margin="85,171,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="180"/>
<Label Content="Message" HorizontalAlignment="Left" Height="25" Margin="10,167,0,0" VerticalAlignment="Top" Width="56" RenderTransformOrigin="0.47,1.333"/>
<StackPanel Orientation="Horizontal" Height="43" Margin="10,332,0,0" Width="283">
<Button x:Name="btnMessageOk" Content="OK" Height="20" Width="89"/>
<Button x:Name="btnMessageCancel" Content="Cancel" Height="20" Margin="50,0,0,0" Width="89"/>
</StackPanel>
<ComboBox x:Name="comboMessageStyle" HorizontalAlignment="Left" Height="27" Margin="85,98,0,0" VerticalAlignment="Top" Width="180">
<ComboBoxItem Content="Information" IsSelected="True"/>
<ComboBoxItem Content="Question"/>
<ComboBoxItem Content="Exclamation"/>
<ComboBoxItem Content="Critical"/>
</ComboBox>
<Label Content="Level" HorizontalAlignment="Left" Height="27" Margin="15,98,0,0" VerticalAlignment="Top" Width="58"/>
</Grid>
</Window>
"@
$pvsDeviceActionerXAML = @'
<Window x:Name="formDeviceActioner" x:Class="PVSDeviceViewerActions.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PVSDeviceViewerActions"
mc:Ignorable="d"
Title="PVS Device Actioner" Height="522.565" Width="401.595">
<Grid Margin="0,0,-20.333,-111.667">
<ListView x:Name="lstMachines" HorizontalAlignment="Left" Height="452" Margin="23,22,0,0" VerticalAlignment="Top" Width="346">
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="VMware" Name="VMwareContextMenu" >
<MenuItem Header="Power On" Name="VMwarePowerOnContextMenu" />
<MenuItem Header="Power Off" Name="VMwarePowerOffContextMenu" />
<MenuItem Header="Restart" Name="VMwareRestartContextMenu" />
<MenuItem Header="Delete" Name="VMwareDeleteContextMenu" />
<MenuItem Header="Reconfigure" >
<MenuItem Header="CPUs" Name="VMwareReconfigCPUsContextMenu" />
<MenuItem Header="Memory" Name="VMwareReconfigMemoryContextMenu" />
</MenuItem>
</MenuItem>
<MenuItem Header="PVS" Name="PVSContextMenu">
<MenuItem Header="Boot" Name="PVSBootContextMenu" />
<MenuItem Header="Shutdown" Name="PVSShutdownContextMenu" />
<MenuItem Header="Restart" Name="PVSRestartContextMenu" />
<MenuItem Header="Delete" Name="PVSDeleteContextMenu" />
</MenuItem>
<MenuItem Header="DDC" Name="DDCContextMenu" >
<MenuItem Header="Maintenance Mode On" Name="DDCMaintModeOnContextMenu" />
<MenuItem Header="Maintenance Mode Off" Name="DDCMaintModeOffContextMenu" />
<MenuItem Header="Message Users" Name="DDCMessageUsersContextMenu" />
<MenuItem Header="Delete" Name="DDCDeleteContextMenu" />
</MenuItem>
<MenuItem Header="AD" Name="ADContextMenu">
<MenuItem Header="Delete" Name="ADDeleteContextMenu" />
</MenuItem>
<MenuItem Header="Windows" Name="WindowsContextMenu">
<MenuItem Header="Shutdown" Name="WinShutdownModeOnContextMenu" />
<MenuItem Header="Restart" Name="WinRestartContextMenu" />
</MenuItem>
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView>
<GridViewColumn Header="Device" DisplayMemberBinding ="{Binding 'Name'}" />
<GridViewColumn Header="Boot Time" DisplayMemberBinding ="{Binding 'Boot_Time'}" />
<GridViewColumn Header="Users" DisplayMemberBinding ="{Binding 'User_Sessions'}" />
<GridViewColumn Header="Maintenance Mode" DisplayMemberBinding ="{Binding 'Maintenance_Mode'}" />
</GridView>
</ListView.View>
<ListBoxItem Content="Device"/>
</ListView>
</Grid>
</Window>
'@
## Adding so we can make it app modal as well as system
Add-Type @'
using System;
using System.Runtime.InteropServices;
namespace PInvoke.Win32
{
public static class Windows
{
[DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, String text, String caption, uint type);
}
}
'@
Add-Type -TypeDefinition @'
public enum MessageBoxReturns
{
IDYES = 6,
IDNO = 7 ,
IDOK = 1 ,
IDABORT = 3,
IDCANCEL = 2 ,
IDCONTINUE = 11 ,
IDIGNORE = 5 ,
IDRETRY = 4 ,
IDTRYAGAIN = 10
}
'@
Function Save-ConfigToRegistry( [string]$serverSet = 'Default' , [string[]]$DDCs , [string[]]$PVSServers , [string[]]$hypervisors )
{
[string]$key = Join-Path $configRegKey $serverSet
if( ! ( Test-Path $key -ErrorAction SilentlyContinue ) )
{
$null = New-Item -Path $key -Force
}
Set-ItemProperty -Path $key -Name 'DDC' -Value $DDCs
Set-ItemProperty -Path $key -Name 'PVS' -Value $PVSServers
Set-ItemProperty -Path $Key -Name 'Hypervisor' -Value $hypervisors
}
Function Get-ConfigFromRegistry
{
Param
(
[string]$serverSet = 'Default' ,
[ref]$DDCs ,
[ref]$PVSServers ,
[ref]$hypervisors
)
[string]$key = Join-Path $configRegKey $serverSet
if( ! ( Test-Path $key -ErrorAction SilentlyContinue ) )
{
Write-Warning "Config registry key `"$key`" does not exist"
}
$hypervisors.value = Get-ItemProperty -Path $key -Name 'Hypervisor' -ErrorAction SilentlyContinue | select -ExpandProperty 'Hypervisor'
$DDCs.value = Get-ItemProperty -Path $key -Name 'DDC' -ErrorAction SilentlyContinue | select -ExpandProperty 'DDC'
$PVSServers.value = Get-ItemProperty -Path $key -Name 'PVS' -ErrorAction SilentlyContinue | select -ExpandProperty 'PVS'
}
Function Load-GUI( $inputXaml )
{
$form = $NULL
$inputXaml = $inputXaml -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[xml]$XAML = $inputXaml
$reader = New-Object Xml.XmlNodeReader $xaml
try
{
$Form = [Windows.Markup.XamlReader]::Load( $reader )
}
catch
{
Write-Error "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .NET is installed.`n$_"
return $null
}
$xaml.SelectNodes("//*[@Name]") | ForEach-Object `
{
Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -Scope Script
}
return $form
}
Function Display-MessageBox( $window , $text , $caption , [System.Windows.MessageBoxButton]$buttons , [System.Windows.MessageBoxImage]$icon )
{
if( $window -and $window.Handle )
{
[int]$modified = switch( $buttons )
{
'OK' { [System.Windows.MessageBoxButton]::OK }
'OKCancel' { [System.Windows.MessageBoxButton]::OKCancel }
'YesNo' { [System.Windows.MessageBoxButton]::YesNo }
'YesNoCancel' { [System.Windows.MessageBoxButton]::YesNo }
}
[int]$choice = [PInvoke.Win32.Windows]::MessageBox( $Window.handle , $text , $caption , ( ( $icon -as [int] ) -bor $modified ) ) ## makes it app modal so UI blocks
switch( $choice )
{
([MessageBoxReturns]::IDYES -as [int]) { 'Yes' }
([MessageBoxReturns]::IDNO -as [int]) { 'No' }
([MessageBoxReturns]::IDOK -as [int]) { 'Ok' }
([MessageBoxReturns]::IDABORT -as [int]) { 'Abort' }
([MessageBoxReturns]::IDCANCEL -as [int]) { 'Cancel' }
([MessageBoxReturns]::IDCONTINUE -as [int]) { 'Continue' }
([MessageBoxReturns]::IDIGNORE -as [int]) { 'Ignore' }
([MessageBoxReturns]::IDRETRY -as [int]) { 'Retry' }
([MessageBoxReturns]::IDTRYAGAIN -as [int]) { 'TryAgain' }
}
}
else
{
[Windows.MessageBox]::Show( $text , $caption , $buttons , $icon )
}
}
Function Perform-Action( [string]$action , $form )
{
$_.Handled = $true
## Get HWND so we can make app modal dialogues
$thisWindow = [System.Windows.Interop.WindowInteropHelper]::new($form)
$answer = Display-MessageBox -text "Are you sure you want to $action these $($WPFlstMachines.SelectedItems.Count) devices ?" -caption 'Confirm' -buttons YesNo -icon Question -window $thisWindow
if( $answer -ne 'Yes' )
{
return
}
if( $action -eq 'Message' )
{
$messageForm = Load-GUI $messageWindowXAML
if( $messageForm )
{
## Load up caption and text and set callbacks
$WPFtxtMessageCaption.Text = if( [string]::IsNullOrEmpty( $messageCaption ) ) { "Message from $env:USERNAME at $(Get-Date -Format F)" } else { $messageCaption }
$WPFtxtMessageBody.Text = $messageText
$WPFbtnMessageOk.Add_Click({
$messageForm.DialogResult = $true
$messageForm.Close()
})
$WPFbtnMessageOk.IsDefault = $true
$WPFbtnMessageCancel.Add_Click({ $messageForm.Close() })
$WPFbtnMessageCancel.IsCancel = $true
$result = $messageForm.ShowDialog()
if( ! $result )
{
return
}
}
}
if( $form )
{
$oldCursor = $form.Cursor
$form.Cursor = [Windows.Input.Cursors]::Wait
}
[int]$errors = 0
ForEach( $device in $WPFlstMachines.SelectedItems )
{
Write-Verbose "Action $action on $($device.Name)"
switch -regex ( $action )
{
'Message' { Get-BrokerSession -AdminAddress $device.ddc -MachineName ( $env:USERDOMAIN + '\' + $device.Name ) | Send-BrokerSessionMessage -AdminAddress $device.ddc -Title $WPFtxtMessageCaption.Text -Text $WPFtxtMessageBody.Text -MessageStyle ($WPFcomboMessageStyle.SelectedItem.Content) ;break }
'Remove From AD' { Remove-ADComputer -Identity $device.Name -Confirm:$False ;break }
'Remove From DDC' { Remove-BrokerMachine -Force -AdminAddress $device.ddc -MachineName $( if( [string]::IsNullOrEmpty( $device.DomainName ) ) { $device.Name } else { $device.DomainName + '\' + $device.Name } ) ; break }
'Remove From PVS' { Set-PvsConnection -Server $device.'PVS Server'; Remove-PvsDevice -DeviceName $device.Name ; break }
'Maintenance Mode On' { Set-BrokerMachine -AdminAddress $device.ddc -InMaintenanceMode $true -MachineName ( $device.DomainName + '\' + $device.Name ) ; break }
'Maintenance Mode Off' { Set-BrokerMachine -AdminAddress $device.ddc -InMaintenanceMode $false -MachineName ( $device.DomainName + '\' + $device.Name ) ; break }
'Reboot' { Restart-Computer -ComputerName $device.Name ; break }
'Shutdown' { Stop-Computer -ComputerName $device.Name ; break }
'Remove from Hypervisor' { Get-VM -Name $device.Name | Remove-VM -DeletePermanently -Confirm:$false ;break }
'VMware Boot' { Get-VM -Name $device.name | Start-VM -Confirm:$false ;break }
'VMware Power Off' { Get-VM -Name $device.name | Stop-VM -Confirm:$false ;break }
'VMware Restart' { Get-VM -Name $device.name | Restart-VM -Confirm:$false ;break }
'PVS Boot|PVS Power Off|PVS Restart' ` ## Can't use New-BrokerHostingPowerAction as may not be known to DDC
{
Set-PvsConnection -Server $device.'PVS Server'
if( $_ -match 'Boot' )
{
if( [string]::IsNullOrEmpty( $device.'Disk Name' ) )
{
$answer = Display-MessageBox -window $thisWindow -text "$($device.Name) has no vdisk assigned so may not boot - continue ?" -caption 'Confirm' -buttons YesNo -icon Question
if( $answer -ne 'Yes' )
{
continue
}
}
$thePvsTask = Start-PvsDeviceBoot -DeviceName $device.Name
}
elseif( $_ -match 'Off' )
{
$thePvsTask = Start-PvsDeviceShutdown -DeviceName $device.Name
}
elseif( $_ -match 'Restart' )
{
$thePvsTask = Start-PvsDeviceReboot -DeviceName $device.Name
}
$timer = [Diagnostics.Stopwatch]::StartNew()
[bool]$timedOut = $false
while ( $thePvsTask -and $thePvsTask.State -eq 0 )
{
$percentFinished = Get-PvsTaskStatus -Object $thePvsTask
if( ! $percentFinished -or $percentFinished.ToString() -ne 100 )
{
Start-Sleep -timer 500
if( $timer.Elapsed.TotalSeconds -gt $timeout )
{
$timeOut = $true
break
}
}
$thePvsTask = Get-PvsTask -Object $thePvsTask
}
$timer.Stop()
if ( $timedOut )
{
Display-MessageBox -window $thisWindow -text "Failed to perform action on $($device.Name) - timed out after $timeout seconds" -caption $action -buttons OK -icon Error
$errors++
}
elseif ( ! $thePvsTask -or $thePvsTask.State -ne 2)
{
Display-MessageBox -window $thisWindow -text "Failed to perform action on $($device.Name)" -caption $action -buttons OK -icon Error
$errors++
}
}
default { Write-Warning "Unknown command `"$action`"" }
}
if( ! $? )
{
$errors++
}
}
if( $form )
{
$form.Cursor = $oldCursor
}
[string]$status = [System.Windows.MessageBoxImage]::Information
if( $errors )
{
$status = [System.Windows.MessageBoxImage]::Error
}
Display-MessageBox -window $thisWindow -text "$errors / $($WPFlstMachines.SelectedItems.Count) errors" -caption "Finished $action" -buttons OK -icon $status
}
<#
Function Get-RemoteInfo( [string]$computer , [int]$cpuSamples , [int]$jobTimeout )
{
[hashtable]$results = @{}
[scriptblock]$code = `
{
Param([string]$computer,[int]$cpuSamples)
Invoke-Command -ComputerName $computer -ScriptBlock `
{
$osinfo = Get-CimInstance Win32_OperatingSystem
$logicalDisks = Get-CimInstance -ClassName Win32_logicaldisk -Filter 'DriveType = 3'
$cpu = $(if( $using:cpuSamples -gt 0 ) { [math]::Round( ( Get-Counter -Counter '\Processor(*)\% Processor Time' -SampleInterval 1 -MaxSamples $using:cpuSamples |select -ExpandProperty CounterSamples| Where-Object { $_.InstanceName -eq '_total' } | select -ExpandProperty CookedValue | Measure-Object -Average ).Average , 1 ) }) -as [int]
$domainMembership = Test-ComputerSecureChannel
$osinfo,$logicalDisks,$cpu,$domainMembership
}
}
try
{
## use a runspace so we can have a timeout
$runspace = [RunspaceFactory]::CreateRunspace()
$runspace.Open()
$command = [PowerShell]::Create().AddScript($code)
$command.Runspace = $runspace
$null = $command.AddParameters( @( $computer , $cpuSamples ) )
$job = $command.BeginInvoke()
## wait for command to finish
$wait = $job.AsyncWaitHandle.WaitOne( $jobTimeout * 1000 , $false )
if( $wait -or $job.IsCompleted )
{
if( $command.HadErrors )
{
Write-Warning "Errors occurred in remote command on $computer :`n$($command.Streams.Error)"
}
else
{
$osinfo,$logicalDisks,$cpu,$domainMembership = $command.EndInvoke($job)
if( $osinfo ) ## assume that if this is not null then we at least got some info - need to avoid divide by zero
{
$results =
@{
'Boot_Time' = $osinfo.LastBootUpTime
'Available Memory (GB)' = [Math]::Round( $osinfo.FreePhysicalMemory / 1MB , 1 )
'Committed Memory %' = 100 - [Math]::Round( ( $osinfo.FreeVirtualMemory / $osinfo.TotalVirtualMemorySize ) * 100 , 1 )
'CPU Usage %' = $cpu
'Free disk space %' = ( $logicalDisks | Sort DeviceID | ForEach-Object { [Math]::Round( ( $_.FreeSpace / $_.Size ) * 100 , 1 ) }) -join ' '
'Domain Membership' = $( if( $domainMembership ) { 'OK' } else { 'Bad' } )
}
}
else
{
Write-Warning "No data returned from remote command on $computer"
}
}
## if we do these after timeout too then takes an age to return which defeats the point of running via a runspace
$command.Dispose()
$runSpace.Dispose()
}
else
{
Write-Warning "Job to retrieve info from $computer is still running after $jobTimeout seconds so aborting"
$null = $command.BeginStop($null,$null)
## leaking command and runspace but if we dispose it hangs
}
}
catch
{
Write-Error "Failed to get remote info from $computer : $($_.ToString())"
}
$results
}
#>
if( $noProgress )
{
$ProgressPreference = 'SilentlyContinue'
}
if( $registry )
{
Get-ConfigFromRegistry -serverSet $serverSet -DDCs ( [ref] $ddcs ) -PVSServers ( [ref] $pvsServers ) -hypervisors ( [ref] $hypervisors )
if( ! $ddcs -or ! $ddcs.Count -or ! $pvsServers -or ! $pvsServers.Count )
{
Write-Warning "Failed to get PVS and/or DDC servers from registry key `"$configRegKey`" for server set `"$serverSet`""
return
}
}
elseif( $save )
{
Save-ConfigToRegistry -serverSet $serverSet -DDCs $ddcs -PVSServers $pvsServers -hypervisors $hypervisors
}
if( $hypervisors -and $hypervisors.Count )
{
if( [string]::IsNullOrEmpty( $name ) )
{
Write-Error "Must specify a VM name pattern via -name when cross referencing to VMware"
exit
}
Import-Module $vmWareModule -ErrorAction Stop
Write-Progress -Activity "Connecting to hypervisors $($hypervisors -split ' ')" -PercentComplete 1
$null = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
if( Connect-VIserver -Server $hypervisors )
{
$null = $columns.Add( 'CPUs')
$null = $columns.Add( 'Memory (GB)')
$null = $columns.Add( 'Hard Drives (GB)')
$null = $columns.Add( 'NICs')
$null = $columns.Add( 'Hypervisor')
## we pass the modules list to our function so that they can be loaded into the runspaces
$modules += $vmWareModule
}
else
{
Write-Error "Failed to connect to vmware $($hypervisors -join ' ')"
exit
}
}
if( $PSVersionTable.PSVersion.Major -lt 5 -and $columns.Count -gt 30 -and [string]::IsNullOrEmpty( $csv ) )
{
Write-Warning "This version of PowerShell limits the number of columns in a grid view to 30 and we have $($columns.Count) so those from `"$($columns[30])`" will be lost in grid view"
}
[datetime]$startTime = Get-Date
[hashtable]$devices = Get-PVSDevices -pvsservers $pvsServers -ddcs $ddcs -hypervisors $hypervisors -dns:$dns -name $name -tags:$tags -adgroups $ADgroups -noRemoting:$noRemoting -cpusamples:$cpuSamples -noProgress:$noprogress `
-maxThreads $maxThreads -timeout $timeout -jobTimeout $jobTimeout -maxRecordCount $maxRecordCount -provisioningType $provisioningType -noOrphans:$noOrphans -pvsShare $pvsShare -modules $modules -splitVM $splitVM
if( $devices -and $devices.Count )
{
if( ! [string]::IsNullOrEmpty( $csv ) )
{
$devices.GetEnumerator() | ForEach-Object { $_.Value } | Select $columns | Sort Name | Export-Csv -Path $csv -NoTypeInformation -NoClobber
}
if( ! $noGridView )
{
[hashtable]$params = @{}
if( $noMenu )
{
$params.Add( 'Wait' , $true )
}
else
{
$params.Add( 'PassThru' , $true )
}
[string]$title = "$(Get-Date -Format G) : $($devices.count) PVS devices via $($pvsServers -join ' ') & ddc $($ddcs -join ' ')"
if( ! [string]::IsNullOrEmpty( $name ) )
{
$title += " matching `"$name`""
}
[array]$selected = @( $devices.GetEnumerator() | ForEach-Object { $_.Value } | Select $columns | Sort Name | Out-GridView -Title $title @Params )
if( $selected -and $selected.Count )
{
$mainForm = Load-GUI $pvsDeviceActionerXAML
if( ! $mainForm )
{
return
}
$mainForm.Title += " - $($selected.Count) devices"
if( $hypervisors -and $hypervisors.Count )
{
$WPFVMwarePowerOnContextMenu.Add_Click({ Perform-Action -action 'VMware Boot' -form $mainForm })
$WPFVMwarePowerOffContextMenu.Add_Click({ Perform-Action -action 'VMware Power Off' -form $mainForm })
$WPFVMwareRestartContextMenu.Add_Click({ Perform-Action -action 'VMware Restart' -form $mainForm })
$WPFVMwareDeleteContextMenu.Add_Click({ Perform-Action -action 'Remove From Hypervisor' -form $mainForm })
}
else
{
$WPFVMwareContextMenu.IsEnabled = $false
}
if( Get-Module ActiveDirectory -ErrorAction SilentlyContinue )
{
$WPFADDeleteContextMenu.Add_Click({ Perform-Action -action 'Remove From AD' -form $mainForm })
}
else
{
$WPFADContextMenu.IsEnabled = $false
}
$WPFDDCMessageUsersContextMenu.Add_Click({ Perform-Action -action Message -form $mainForm })
$WPFDDCDeleteContextMenu.Add_Click({ Perform-Action -action 'Remove From DDC' -form $mainForm })
$WPFDDCMaintModeOffContextMenu.Add_Click({ Perform-Action -action 'Maintenance Mode Off' -form $mainForm })
$WPFDDCMaintModeOnContextMenu.Add_Click({ Perform-Action -action 'Maintenance Mode On' -form $mainForm })
$WPFWinShutdownModeOnContextMenu.Add_Click({ Perform-Action -action Shutdown -form $mainForm })
$WPFWinRestartContextMenu.Add_Click({ Perform-Action -action Reboot -form $mainForm })
$WPFPVSDeleteContextMenu.Add_Click({ Perform-Action -action 'Remove From PVS' -form $mainForm })
$WPFPVSBootContextMenu.Add_Click({ Perform-Action -action 'PVS Boot' -form $mainForm })
$WPFPVSShutdownContextMenu.Add_Click({ Perform-Action -action 'PVS Power Off' -form $mainForm })
$WPFPVSRestartContextMenu.Add_Click({ Perform-Action -action 'PVS Restart' -form $mainForm })
$WPFlstMachines.Items.Clear()
$WPFlstMachines.ItemsSource = $selected
## Select all items since already selected them in grid view
$WPFlstMachines.SelectAll()
$null = $mainForm.ShowDialog()
## Put in clipboard so we can paste into something if we need to
if( $selected )
{
$selected | Clip.exe
}
}
}
}
else
{
Write-Warning "No PVS devices found via $($pvsServers -join ' ')"
}
if( ( Get-Variable global:DefaultVIServers -ErrorAction SilentlyContinue ) -and $global:DefaultVIServers.Count )
{
Disconnect-VIServer -Server $hypervisors -Force -Confirm:$false
}