-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrench.ps1
executable file
·1516 lines (1432 loc) · 50.9 KB
/
wrench.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
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
###############################################################################
# Program Name: Wrench
# Author: Matt Tuchfarber
# Contributors: James Atkinson, Kevin Cook
# Current maintainer: James Atkinson
# Date Created: 2014-09-12
# Purpose: To enable quicker searching of users and computers in active directory
# and to allow easy manipulation of those objects. A toolkit built
# to ease help desk support and work more efficiently
###############################################################################
#Load Vairables from Enviroment File
. "$PSScriptRoot\wrench_env.ps1"
### First two functions force script to be ran as admin ###
function IsAdministrator{
$Identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$Principal = New-Object System.Security.Principal.WindowsPrincipal($Identity)
$Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
function IsUacEnabled{ (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System).EnableLua -ne 0 }
if (!(IsAdministrator)){
if (IsUacEnabled){
[string[]]$argList = @('-NoProfile','-WindowStyle','-File', $MyInvocation.MyCommand.Path)
$argList += $MyInvocation.BoundParameters.GetEnumerator() | ForEach-Object {"-$($_.Key)", "$($_.Value)"}
$argList += $MyInvocation.UnboundArguments
Start-Process PowerShell.exe -Verb Runas -WorkingDirectory $pwd -ArgumentList $argList
return
}else{
throw "You must be administrator to run this script"
}
}
############################################################
###Load Windows Form Assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Application]::EnableVisualStyles();
$msg = [System.Windows.Forms.MessageBox] # Standard message box form
### Optimal Prereq checks
$correctPSVersion = $false
$rsatInstalled = $false
$sccmConfigPSD1 = test-path "$SCCMSiteDataFile"
#check for PSd1 file that allows powershell to connect to SCCM Site Server for cmdlets
#Check Powershell version > 3
if ($psversiontable.psversion.Major -lt 3){
$msg::Show("This program requires Powershell version 3 or higher")
}else{
$correctPSVersion = $true
}
if ($correctPSVersion -eq $True){
#So long as Powesehell version is good, look for AD module of RSAT
if(get-module -list activedirectory){
$rsatInstalled=$true
}else{$msg::Show("This program requires Microsoft RSAT to be installed")
#Exit if RSAT is not installed as Wrench cannot be used without it.
}
}
if ($correctPSVersion -eq $true -AND $rsatInstalled -eq $true){
Import-Module ActiveDirectory
If ($sccmConfigPSD1 -eq $True) {
Import-Module $SCCMSiteDataFile #import sccm data file
$CMDrive = (get-psdrive -PSProvider CMSite).Name
$CMPSSuppressFastNotUsedCheck = $true #Suppress warning messages about -fast being supported in some instances
}
#Used to created GUI items
function createItem($Type, $LocationX, $LocationY, $SizeX, $SizeY, $Text, $ParentForm){
$template = New-Object System.Windows.Forms.$Type
$template.Location = New-Object System.Drawing.Size($LocationX,$LocationY)
$template.Size = New-Object System.Drawing.Size($SizeX,$SizeY)
$template.Text = $Text
$ParentForm.Controls.Add($template)
$template
}
#Used to create GUI forms
function createForm($Text, $SizeX, $SizeY, $StartPos, $BorderStyle, $MinBox, $MaxBox, $ControlBox){
$template = New-Object System.Windows.Forms.Form
$template.Text = $Text
$template.Size = New-Object System.Drawing.Size($SizeX,$SizeY)
$template.StartPosition = $StartPos
$template.MinimizeBox = $MinBox
$template.MaximizeBox = $MaxBox
$template.ControlBox = $ControlBox
$template.FormBorderStyle = $BorderStyle
$template
}
#Used to display a created form
function showForm($Form){
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
$Form.BringToFront()
}
####### MAIN FORM GUI #######
$mainForm = createForm "Wrench" 300 635 "CenterScreen" "Fixed3D" $true $false $true
$mainForm.Icon = New-Object system.drawing.icon($IconLocation)
$mainForm.KeyPreview = $True
$mainForm.Add_KeyDown({ mainKeyboard })
#Name Info
$NameLbl = createItem "Label" 10 28 60 20 "Name: " $mainForm
$NameBox = createItem "TextBox" 70 25 130 20 "" $mainForm
$NameButton = createItem "Button" 210 25 60 20 "Search" $mainForm
$NameButton.TabStop = $False
$NameButton.Add_Click({ searchByName })
#UserID Info
$UserIDLbl = createItem "Label" 10 58 60 20 "User ID: " $mainForm
$UserIDBox = createItem "TextBox" 70 55 130 20 "" $mainForm
#$UserIDBox.MaxLength = 15
$UserIDButton = createItem "Button" 210 55 60 20 "Search" $mainForm
$UserIDButton.TabStop = $False
$UserIDButton.Add_Click({ searchByUserID })
#PC Name Info
$PCLbl = createItem "Label" 10 88 60 20 "PC Name: " $mainForm
$PCBox = createItem "TextBox" 70 85 130 20 "" $mainForm
$PCBox.MaxLength = 15
$PCButton = createItem "Button" 210 85 60 20 "Search" $mainForm
$PCButton.TabStop = $False
$PCButton.Add_Click({ searchByPCName })
#IP Info
$IPLbl = createItem "Label" 10 118 18 20 "IP: " $mainForm
$IPSourceLbl = createItem "Label" 28 118 40 20 "" $mainForm
$IPBox = createItem "Textbox" 70 115 130 20 "" $mainForm
$IPBox.MaxLength = 15
$IPButton = createItem "Button" 210 115 60 20 "Search" $mainForm
$IPButton.TabStop = $False
$IPButton.Add_Click({ searchByIP })
#Phone Info
$PhoneLbl = createItem "Label" 10 148 60 20 "Phone:" $mainForm
$PhoneBox = createItem "Textbox" 70 145 200 20 "" $mainForm
$PhoneBox.ReadOnly = $True
$PhoneBox.TabStop = $False
#Lockout Info
$LockoutLbl = createItem "Label" 10 178 60 20 "Lockout:" $mainForm
$LockoutBox = createItem "Textbox" 70 175 130 20 "" $mainForm
$LockoutBox.ReadOnly = $True
$LockoutBox.TabStop = $False
$LockoutButton = createItem "Button" 210 175 60 20 "Unlock" $mainForm
$LockoutButton.TabStop = $False
$LockoutButton.Visible = $False
$LockoutButton.Add_Click({ unlockAccount })
#H Drive Info
$HDriveLbl = createItem "Label" 10 208 60 20 "H Drive:" $mainForm
$HDriveBox = createItem "Textbox" 70 208 200 20 "" $mainForm
$HDriveBox.ReadOnly = $True
$HDriveBox.TabStop = $False
#OU Info
$OULbl = createItem "Label" 10 238 60 20 "User OU:" $mainForm
$OUBox = createItem "Textbox" 70 235 200 20 "" $mainForm
$OUBox.ReadOnly = $True
$OUBox.TabStop = $False
#Buttons
$RVButton = createItem "Button" 10 265 259 20 "Connect Via SCCM Remote Control" $mainForm
$RVButton.Add_Click({ runRemoteViewer })
$UserFactsButton = createItem "Button" 10 295 122 20 "User Details" $mainForm
$UserFactsButton.Add_Click({ runUserFacts })
$UserGroupButton = createItem "Button" 10 325 122 20 "User Groups" $mainForm
$UserGroupButton.Add_Click({ runUserGroups })
$ChangePWButton = createItem "Button" 10 355 122 20 "Change Password" $mainForm
$ChangePWButton.Add_Click({newUserPassword})
$PCFactsButton = createItem "Button" 147 295 122 20 "PC Details" $mainForm
$PCFactsButton.Add_Click({ runPCFacts })
$PCGroupButton = createItem "Button" 147 325 122 20 "PC Groups" $mainForm
$PCGroupButton.Add_Click({ runPCGroups })
$PCManageButton = createItem "Button" 147 355 122 20 "Manage PC" $mainForm
$PCManageButton.Add_Click({ runManagePC })
$ViewCButton = createItem "Button" 147 385 122 20 "View C:" $mainForm
$ViewCButton.Add_Click({ runViewC })
$RDPButton = createItem "Button" 147 415 122 20 "RDP" $mainForm
$RDPButton.Add_Click({ runRDP })
$PSSessionBtn = createItem "Button" 147 445 122 20 "PS Remote" $mainForm
$PSSessionBtn.Add_Click({connectPSSession})
$OnTopCheck = createItem "Checkbox" 30 410 100 35 "Keep Wrench on Top" $mainForm
$OnTopCheck.Add_Click({ runOnTop })
$NewPSLbl = createItem "Label" 80 578 150 15 "New Powershell Window" $mainForm
$NewPSLbl.ForeColor = "Blue"
$NewPSLbl.Add_Click({ Start-Process "powershell.exe" })
$PingTimer = New-Object System.Windows.Forms.Timer
$PingTimer.Interval = 1000
$PingTimer.add_tick({ checkPing })
#Logo Image
$logo = new-object Windows.Forms.PictureBox
$logo.location = New-Object System.Drawing.Size(10,477)
$logo.size = New-Object System.Drawing.Size(260,100)
$logo.BorderStyle = "FixedSingle"
$logo.Image = [System.Drawing.Image]::Fromfile((get-item $LogoLocation));
$mainForm.controls.add($logo)
#Expand Button
$ExpandButton = createItem "Button" 264 577 15 15 ">" $mainForm
$ExpandButton.Add_Click({ expandForm })
$ExpandButton.Visible = $true
####### EXPANDED FORM GUI #######
#Draw Seperator
$pen = new-object Drawing.SolidBrush black
$formGraphics = $mainForm.createGraphics()
$mainForm.add_paint({$formGraphics.DrawLine($pen, 279, 1, 279, 620)})
#Buttons
$GPBtn = createItem "Button" 290 25 122 20 "Check Group Policy" $mainForm
$GPBtn.Add_Click({ pullGroupPolicy })
$GPTimer = New-Object System.Windows.Forms.Timer
$GPTimer.Interval = 2000
$GPTimer.add_tick({ checkGP })
$TelnetPCButton = createItem "Button" 290 55 122 20 "Telnet" $mainForm
$TelnetPCButton.Add_Click({ runTelnet })
$RenameButton = createItem "Button" 290 85 122 20 "Rename PC" $mainForm
$RenameButton.Add_Click({ runRename })
$SCCMClientCenterButton = createItem "Button" 290 115 122 20 "Client Center" $mainForm
$SCCMClientCenterButton.Add_Click({ openClientCenter })
$MSRemoteAssistance = createItem "Button" 290 145 122 20 "MS Remote Assist" $mainForm
$MSRemoteAssistance.Add_Click({ openMSRA })
#Labels
#$CMDriveLbl = createItem "Label" 290 145 122 20 "SCCM Site: $CMDrive" $mainForm
#$PSScriptRootlbl = createItem "Label" 290 160 122 60 "PSScriptRoot: $PSScriptRoot" $mainForm
####### VARIABLES #######
$global:Name = ""
$global:UserID = ""
$global:PCName = ""
$global:IP = ""
$global:Lockout = ""
$global:Phone = ""
$global:ValidName = $False
$global:ValidUserID = $False
$global:ValidPCName = $False
$global:IPWithARec = $True
### MAIN FORM FUNCTIONS ###
function searchByName{
clearVariables
$global:Name = ($NameBox.text).Trim()
testName
if ($global:validName -eq $True){
if(!(pickName)){return}
clearBoxes
$NameBox.text = $global:Name
$LockoutButton.Visible=$false
if(!(IDByLastName)){return}
PhoneByUserID
if(!(PCNameByUserID)){return}
LockoutByUserID
HDriveByUserID
OUByUserID
getIP
pingIP
}else{
$msg::Show("No User Found")
}
}
function searchByUserID{
clearVariables
$global:UserID = ($UserIDBox.Text).Trim()
testID
if($global:validID -eq $True){
if(!(pickID)){return}
clearBoxes
$UserIDBox.Text = $global:UserID
$LockoutButton.Visible=$false
NameByUserID
if(!(PCNameByUserID)){return}
LockoutByUserID
PhoneByUserID
HDriveByUserID
OUByUserID
getIP
pingIP
}else{
$msg::Show("No UserID found")
}
}
function searchByPCName{
clearVariables
$global:PCName = ($PCBox.Text).Trim()
testPCName
if($global:validPCName -eq $True){
if(!(pickPCName)){return}
clearBoxes
$PCBox.Text = $global:PCName
$LockoutButton.Visible=$false
UserIDByPCName
NameByUserID
LockoutByUserID
PhoneByUserID
HDriveByUserID
OUByUserID
getIP
pingIP
}else{
$msg::Show("No PC found")
}
}
function searchByIP{
clearVariables
$global:IP = ($IPBox.Text).Trim()
if(testIP){
clearBoxes
$IPBox.Text = $global:IP
if($global:IPWithARec){
$LockoutButton.Visible=$false
PCNameByIP
UserIDByPCName
NameByUserID
LockoutByUserID
PhoneByUserID
HDriveByUserID
OUByUserID
}
}
}
function unlockAccount{
try{
Unlock-ADAccount $global:UserID -Confirm:$False
if((Get-ADUser $global:UserID -properties LockedOut).LockedOut -eq $False){
$LockoutButton.Visible = $False
$LockoutBox.Text = "Not Locked"
}else{
$LockoutBox.Text = "Locked"
}
}catch{
$msg::Show($error[0].toString() + "`r`n`r`nIf this is in error, please check out http://support.microsoft.com/kb/2577917")
}
}
function runRemoteViewer{
if ($global:IP -eq "" -or $global:IP -eq "-"){
$msg::Show("Search PC or IP first!")
}else{
& $SCCMRemoteLocation $global:IP
}
}
function runUserFacts{
if($global:userID -eq "" -or $global:userID -eq "-"){
$msg::Show("Find a user first!")
}else{
extraUserFacts
}
}
function runUserGroups{
if($global:userID -eq "" -or $global:userID -eq "-"){
$msg::Show("Find a user first!")
}else{
viewGroups "user"
}
}
function runPCFacts{
if($global:PCName -eq "" -or $global:PCName -eq "-"){
$msg::Show("Find a PC first!")
}else{
extraPCFacts
}
}
function runPCGroups{
if($global:PCName -eq "" -or $global:PCName -eq "-"){
$msg::Show("Find a PC first!")
}else{
viewGroups "computer"
}
}
function runManagePC{
if ($global:pcname -eq "-" -and $global:IP -eq "-"){
$msg::Show("Pick a PC first")
}else{
try{
compmgmt.msc /computer:$global:pcname
}catch{
$msg::Show($error[0])
}
}
}
function runViewC{
if ($global:pcname -eq "-" -and $global:IP -eq "-"){
$msg::Show("Pick a PC first")
}else{
try{
viewCFolder
}catch{
$msg::Show($error[0])
}
}
}
function runRDP{
try{
Start-Process "mstsc.exe" "/v:$global:PCName"
}catch{
$msg::Show($error[0])
}
}
function connectPSSession{
$commandArg = '-Command "Enter-PSSession -ComputerName ' + $PCName
Start-Process "powershell.exe" -ArgumentList '-NoExit', $commandArg
}
function runOnTop{
if ($OnTopCheck.Checked -eq $true){
$mainForm.TopMost = $true
$mainForm.Update()
}else{
$mainForm.TopMost = $false
$mainForm.Update()
}
}
function mainKeyboard{
if ($_.KeyCode -eq "Enter"){
if($NameBox.Focused){ $NameButton.PerformClick() }
elseif ($PCBox.Focused){ $PCButton.PerformClick() }
elseif ($IPBox.Focused){ $IPButton.PerformClick() }
elseif ($UserIDBox.Focused){ $UserIDButton.PerformClick() }
}
}
### EXPANDED FORM FUNCTIONS ###
function pullGroupPolicy{
$resultpath = $env:temp + "\GPResult.html"
remove-item $resultpath
if($global:PCName -eq ""){
$msg::Show("Please find a computer first")
}elseif($global:PCName -ne "" -and $global:userID -eq ""){
#Runs if PCName exists but username doesn't
$global:GPJob = Start-Job {
param($PCName,$resultpath)
try{
gpresult /s $global:PCName /scope:computer /h $resultpath
}catch{ $msg::Show($error[0]) }
} -Arg @($global:PCName,$resultpath)
} elseif($global:PCName -ne "" -and $global:userID -ne ""){
#Runs if PCName and UserID are there
$global:GPJob = Start-Job {
param($PCName, $userID, $resultpath)
try{
gpresult /s $global:PCName /user $global:UserID /h $resultpath
}catch{ $msg::Show($error[0]) }
} -Arg @($global:PCName,$global:userID,$resultpath)
}
$GPTimer.Enabled = $true
$GPBtn.Forecolor = "blue"
}
function checkGP{
$state = $global:GPJob.State
$resultpath = $env:temp + "\GPResult.html"
if (($global:GPJob.State -eq "Completed")){
if (Test-Path $resultPath){
try{
$IE = New-Object -com internetexplorer.application
$IE.visible = $true
$IE.navigate($resultpath)
}catch{ $msg::Show($error[0]) }
}
$GPTimer.Enabled = $false
$GPBtn.Forecolor = "black"
}
}
function runTelnet{
if ($global:pcname -eq "-" -and $global:IP -eq "-"){
$msg::Show("Pick a PC first")
}else{
try{
telnetPC
}catch{
$msg::Show($error[0])
}
}
}
function runRename{
try{
$ProcessName = "/c start cmd /c cscript.exe " + $RenameComputerLocation
Start-Process "cmd.exe" $ProcessName
}catch{
$msg::Show($error[0])
}
}
function openClientCenter{
& $ClientCenterLocation $global:pcname
}
function openMSRA {
& msra /offerra $global:IP
}
### MAIN FORM UTILITY FUNCTIONS ###
function makeClickList([ref]$variable, $array, [ref]$returnval){
function EnterListener(){
if ($_.KeyCode -eq "Enter"){
$variable.value = $pickList.SelectedItem
$true
$pickForm.close()
}elseif ($_.Keycode -eq "Escape"){
$false
$pickForm.close()
}
}
$pickForm = createForm "Select One:" 200 180 "CenterScreen" "Fixed3D" $false $false $false
$pickForm.KeyPreview = $True #Makes form aware of key presses so it sees "ENTER"
$pickForm.Add_KeyDown({$returnval.value = EnterListener})
$pickList = createItem "ListBox" 17 17 150 100 "" $pickForm
$pickList.DataSource = $array
$pickList.add_MouseDoubleClick({$variable.value = $pickList.SelectedItem;$returnval.value=$true;$pickForm.close()})
showForm($PickForm)
}
function clearBoxes{
$NameBox.Text = ""
$UserIDBox.Text = ""
$PCBox.Text = ""
$IPBox.Text = ""
$LockoutBox.Text = ""
$PhoneBox.Text = ""
}
function clearVariables{
$global:Name = ""
$global:UserID = ""
$global:PCName = ""
$global:IP = ""
$global:Lockout = ""
$global:Phone = ""
$global:ValidName = $False
$global:ValidUserID = $False
$global:ValidPCName = $False
$global:IPWithARec = $True
$IPBox.Forecolor = "black"
}
function subWindowKeyListener($form){
if($_.Control -eq $true -and $_.KeyCode -eq "W" ){
$form.close()
}
}
### USER SEARCH FUNCTIONS
function pickName{
$spaceindex = $name.IndexOf(" ")
$commaindex = $name.IndexOf(",")
$matches = @()
if ($commaindex -gt 0){
$search = $name + "*"
$matches = @((Get-ADUser -Filter {name -like $search}).name)
}else{
if($spaceindex -gt 0){
$firstname = $name.substring(0, $spaceindex)
$lastname = $name.substring($spaceindex)
$firstname += "*"
$firstnamematches = @((Get-ADUser -Filter {GivenName -like $firstname}).name)
$lastname+= "*"
$lastnamematches = @((Get-ADUser -Filter {Surname -like $lastname}).name)
ForEach ($fname in $firstnamematches){
ForEach ($lname in $lastnamematches){
if($fname -eq $lname){
$matches += $fname
}
}
}
}else{
$search = $name + "*"
$firstnamematches = @((Get-ADUser -Filter {GivenName -like $search}).name)
$lastnamematches = @((Get-ADUser -Filter {Surname -like $search}).name)
if (($firstnamematches | Measure-Object).count -gt 0){
$matches += $firstnamematches
}
if (($lastnamematches | Measure-Object).count -gt 0){
$matches += $lastnamematches
}
}
}
if ($matches.count -eq 1){
$global:Name = $matches[0]
$NameBox.Text = $global:Name
$true
}elseif ($matches.count -gt 1){
$returnval = $false
MakeClickList ([ref]$global:Name) $matches ([ref]$returnval)
if(!$returnval){$false}else{$true}
}
}
function pickID{
$variantname = "$UserID" + "*"
$ids = @((Get-ADUser -Filter {SamAccountName -like $variantname}).SamAccountName)
if ($ids.length -eq 1){
$global:UserID = $ids[0]
$true
}else{
$returnval = $false
MakeClickList ([ref]$global:UserID) $ids ([ref]$returnval)
if(!$returnval){$false}else{$true}
}
$UserIDBox.Text = $global:UserID
}
function pickPCName{
$variantname = "$PCName" + "*"
$pcs = @((get-adcomputer -f {name -like $variantname}).name)
if ($pcs.length -eq 1){
$global:PCName = $pcs[0]
$true
}else{
$returnval = $false
MakeClickList ([ref]$global:PCName) $pcs ([ref]$returnval)
if(!$returnval){$false}else{$true}
}
$PCBox.Text = $global:PCName
}
function testName{
if (($Name).Length -lt 1){
$global:validName = $False
}else{
$spaceindex = $name.IndexOf(" ")
$commaindex = $name.IndexOf(",")
$matches = @()
if ($commaindex -gt 0){
$search = $name + "*"
$matches = Get-ADUser -Filter {name -like $search}
}else{
if($spaceindex -gt 0){
$firstname = $name.substring(0, $spaceindex)
$lastname = $name.substring($spaceindex)
$firstname += "*"
$firstnamematches = @(Get-ADUser -Filter {GivenName -like $firstname})
$lastname+= "*"
$lastnamematches = @(Get-ADUser -Filter {Surname -like $lastname})
ForEach ($fname in $firstnamematches){
ForEach ($lname in $lastnamematches){
if($fname.sid -eq $lname.sid){
$matches += $fname
}
}
}
}else{
$search = $name + "*"
$firstnamematches = @(Get-ADUser -Filter {GivenName -like $search})
$lastnamematches = @(Get-ADUser -Filter {Surname -like $search})
$matches = $firstnamematches + $lastnamematches
}
}
}
if (($matches | Measure-Object).count -lt 1){
$global:validName = $false
}else{
$global:ValidName = $true
}
}
function testID{
if (($UserIDBox.Text).length -lt 1){
$global:validID = $False
}else{
#if($UserIDBox.Text -contains "@"){
$variantname = "$UserID" + "*"
#$ids = @((Get-ADUser -Filter {UserPrincipalName -like $variantname}).UserPrincipalName)
#}else{
# variantname = "$UserID" + "*"
$ids = @((Get-ADUser -Filter {SamAccountName -like $variantname}).SamAccountName)
if ($ids[0].length -lt 1){
$global:validID = $False
}else{
$global:validID = $True
}
#}
}
}
function testPCName{
if (($PCBox.Text).length -lt 1){
$global:validPCName = $False
}else{
$variantname = "$PCName" + "*"
$pcs = @((get-adcomputer -f {name -like $variantname}).name)
if ($pcs[0].length -lt 1){
$global:validPCName = $False
}else{
$global:validPCName = $True
}
}
}
function testIP{
$Online = Test-Connection -Computername $global:IP -BufferSize 16 -Count 1 -quiet
if (!$Online){
$false
$IPBox.Forecolor = "red"
}else{
try{
$IPObject = [System.Net.IPAddress]::parse($IP)
[System.Net.IPAddress]::tryparse([string]$IP, [ref]$IPObject)
([System.Net.Dns]::GetHostByAddress($global:IP)).HostName
}catch{
$global:IPWithARec = $False
}
$IPBox.Forecolor = "green"
$true
}
}
function IDByLastName{
$variantname = "$Name" + "*"
$ID = @((Get-ADUser -Filter {name -like $variantname}).SamAccountName)
if($ID.length -eq 1){
$global:UserID = $ID[0]
$true
}else{
$returnval = $false
MakeClickList ([ref]$global:UserID) $ID ([ref]$returnval)
if(!$returnval){$false}else{$true}
}
$UserIDBox.Text = $global:UserID
}
function PCNameByUserID{
$variantid = "$UserID" + "*"
$pcnames = @((Get-WmiObject -Class SMS_UserMachineRelationship -namespace "root\sms\$SCCMNameSpace" -computer $SCCMSiteServer -filter "UniqueUserName LIKE '%$global:UserID' and Types='1' and IsActive='1' and Sources='4'").ResourceName)
if ($pcnames.length -eq 1){
$global:PCName = $pcnames[0]
$true
}else{
$returnval = $false
MakeClickList ([ref]$global:PCName) $pcnames ([ref]$returnval)
if(!$returnval){$false}else{$true}
}
if ($global:PCName.length -lt 2){
$global:PCName = "-"
$PCBox.Text = "-"
}
$PCBox.Text = $global:PCName
}
function getIP{
if($global:PCName -eq "-"){
$global:IP = "-"
}else{
if($global:Name -ne "" -and $global:name -ne "-"){getVPNIP}
if(!($global:IP -like "*.*.*.*")){
getADIP
if(!($global:IP -like "*.*.*.*")){
if(!(getDNSIP)){return}
}
}
}
$IPBox.Text = $global:IP
}
function LockoutByUserID{
if ($global:UserID -ne "-"){
$user = get-aduser $global:userID -properties LockedOut
if ($user.LockedOut -eq $True){
$LockoutBox.Text = "Locked"
$LockoutButton.Visible = $True
}else{
$LockoutBox.Text = "Not Locked"
}
}else{
$global:Lockout = "-"
$LockoutBox.Text = "-"
}
}
function PhoneByUserID{
if ($global:UserID -ne "-"){
$UserPhoneNumber = Get-ADUser $global:UserID -properties Pager, ipPhone, OfficePhone, MobilePhone
If (-not([string]::IsNullOrWhiteSpace(($UserPhoneNumber).Pager))) # If the value is NOT $null, Empty string "" , or any number of spaces " " this equals true
{
$global:Phone = $UserPhoneNumber.Pager
}
Elseif (-not([string]::IsNullOrWhiteSpace($UserPhoneNumber.ipPhone)))
{
$global:Phone = $UserPhoneNumber.ipPhone
}
Elseif (-not([string]::IsNullOrWhiteSpace($UserPhoneNumber.OfficePhone)))
{
$global:Phone = $UserPhoneNumber.OfficePhone
}
Elseif (-not([string]::IsNullOrWhiteSpace($UserPhoneNumber.MobilePhone)))
{
$global:Phone = $UserPhoneNumber.MobilePhone
}
Else {$global:Phone = '-'} # There is no phone number in AD.
}
Else {$global:Phone = '-'} # The UserName field has a "-" in it.
$PhoneBox.Text = $global:Phone
}
function NameByUserID{
if ($global:UserID -ne "-"){
$global:Name = (Get-ADUser $global:UserID).Name
}else{
$global:Name = "-"
}
$NameBox.Text = $global:Name
}
function UserIDByPCName{
if($global:PCName.length -lt 7){
$global:UserID = "-"
}else{
$variantID = ($global:PCName).Substring(0,7)
$variantname = "$variantID" + "*"
$UserName = @(((Get-WmiObject -Class SMS_UserMachineRelationship -namespace "root\sms\$SCCMNameSpace" -computer $SCCMSiteServer -filter "ResourceName='$global:PCName' and Types='1' and IsActive='1' and Sources='4'").UniqueUserName).substring($Domain.length+1))
$SamAccounts=foreach ($name in $UserName){@((Get-ADUser -Filter {SamAccountName -like $name}).SamAccountName)}
$UPN= foreach ($Account in $SamAccounts){@((Get-ADUser -Filter {SamAccountName -like $name}).UserPrincipalName)}
if ($UserName.length -eq 1){
$global:UserID = $UserName[0]
$true
}elseif($UserName.length -eq 0 ){
$global:UserID = "-"
$true
}else{
$returnval = $false
MakeClickList ([ref]$global:UserID) $UserName ([ref]$returnval)
if(!$returnval){$false}else{$true}
}
}
$UserIDBox.Text = $global:UserID
}
function PCNameByIP{
$hostname = ([System.Net.Dns]::GetHostByAddress($IP)).HostName
if ($hostname.indexof('.') -eq -1){
$global:PCName = $hostname
}else{
$global:PCName = $hostname.Substring(0,$hostname.IndexOf('.'))
}
$PCBox.Text = $PCName
}
function HDriveByUserID{
try{
if ($global:UserID -ne "-"){
$user = Get-AdUser $global:UserID -Properties HomeDirectory
$HDriveBox.Text = $user.HomeDirectory
}else{
$HDriveBox.Text = "-"
}
}catch{
}
}
function OUByUserID{
try{
if ($global:UserID -ne "-"){
$user = Get-AdUser $global:UserID -Properties CanonicalName
$OUBox.Text = $user.CanonicalName.Substring(($user.CanonicalName).IndexOf('/'))
}else{
$OUBox.Text = "-"
}
}catch{
}
}
### IP FUNCTIONS
function getVPNList{
$env = get-content -Path ($env:USERPROFILE + "\vpnenv.txt")
try{
$username = $env[0]
$password = $env[1]
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} #Ignore SSL cert
$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential($username, $password)
$webpage = $webclient.DownloadString($VPNSiteUrl) # Ateempt to access website with credentials
}catch{
$msg::Show($error[0])
}
$webpage
}
function getNameIPArray($split){
$vpnarray = @()
for ($i = 0; $i -lt $split.length; $i++){
$line = $split[$i]
$nextline = $split[$i+1]
if ($line.indexof(',') -gt 0 -and $nextline -like "*.*.*.*"){
$vpnip = ($nextline.split(" "))[4]
$name = ($line.split(" "))[6] + " " + ($line.split(" "))[7]
$vpnarray += @(,($name,$vpnip))
}elseif($nextline -like "*.*.*.*"){
$name = ($line.split(" "))[6]
$vpnip = ($nextline.split(" "))[4]
$vpnarray += @(,($name,$vpnip))
}
}
$vpnarray
}
function searchVPNArray($search, $array){
if (!($search -like "*.*.*.*") -and $name.indexof(',') -ne -1){
$firstname = $name.Substring($name.indexof(" ")+1)
$lastname = $name.Substring(0, $name.indexof(','))
#Name Types
$lastcommafirst = "*" + $name + "*"
$firstdotlast = "*" + $firstname + "." + $lastname + "*"
$lastdotfirst = "*" + $lastname + "." + $firstname + "*"
$lastf = "*" + $lastname + $firstname[0] + "*"
}
ForEach ($pair in $array){
if ($pair[1] -like $search){
$pair
}elseif ($pair[0] -like $lastcommafirst -or $pair[0] -like $firstdotlast -or $pair[0] -like $lastdotfirst -or $pair[0] -like $lastf){
$pair
}
}
}
function getVPNIP{
$env = get-content -Path ($env:USERPROFILE + "\vpnenv.txt")
if ($env.count -gt 1){
$webpage = getVPNList
$split = ($webpage -split '[\r\n]') | Where-Object {$_} #Split page into lines
if($split.count -gt 2){
$NameIPArray = getNameIPArray $split
$pair = searchVPNArray $global:name $NameIPArray
if ($pair.count -eq 2){
$global:IP = $pair[1]
$IPSourceLbl.Text ="(vpn)"
}
}
}
}
function getADIP{
try{
$global:IP = (Get-ADComputer $global:PCName -Properties IPv4Address).IPv4Address
$IPSourceLbl.Text ="(ad)"
}catch{
$global:IP = "-"
}
}
function getDNSIP{
$ips = @(([System.Net.Dns]::GetHostAddresses($global:PCName)).IPAddressToString)
if ($ips.length -eq 1){
$global:IP = $ips[0]
$IPSourceLbl.Text ="(dns)"
$true
}elseif($ips.length -gt 1){
$returnval = $false
MakeClickList ([ref]$global:IP) $ips ([ref]$returnval)
if(!$returnval){$false}else{$true}
}
}
function pingIP{
#$Online = Test-Connection -Computername $global:IP -BufferSize 16 -Count 1 -quiet
#if($Online){ $IPBox.Forecolor = "green" } else{ $IPBox.Forecolor = "red" }
$PingTimer.Enabled = $true
$global:PingJob = Start-Job {
param($IP)
Test-Connection -Computername $IP -BufferSize 16 -Count 1 -quiet
} -Arg $global:IP
}
function checkPing{
if (($global:PingJob.State -eq "Completed")){
$Pingable = @(Receive-Job -id $PingJob.id)
if ($Pingable){
$IPBox.Forecolor = "Green"
}else{
$IPBox.Forecolor = "Red"
}
$PingTimer.Enabled = $false
}
}
### BUTTON CLICK FORMS ###
function extraUserFacts{
$user = Get-ADUser $UserID -properties LockedOut, Enabled, AccountExpirationDate, Certificates, Department, Description, PasswordNeverExpires, BadPwdCount, LastBadPasswordAttempt, PasswordLastSet, WhenChanged, WhenCreated
$UserFactsForm = createForm "User Info" 300 350 "CenterScreen" "Fixed3D" $false $false $true
#$UserFactsForm.Add_KeyDown({subWindowKeyListener($UserFactsForm)})
$EnabledLbl = createItem "Label" 10 10 280 20 ("Enabled: " + $user.Enabled) $UserFactsForm
$AccountExpireLbl = createItem "Label" 10 40 280 20 ("Account Expires: " + $user.AccountExpirationDate) $UserFactsForm
$PWNeverExpireLbl = createItem "Label" 10 70 280 20 ("Password Never Expires: " + $user.PasswordNeverExpires) $UserFactsForm
$BadPWCountLbl = createItem "Label" 10 100 280 20 ("Bad Password Count: " + $user.BadPwdCount) $UserFactsForm
$CreateTimeLbl = createItem "Label" 10 130 280 20 ("Created Time: " + $user.whenCreated) $UserFactsForm
$ChangeTimeLbl = createItem "Label" 10 160 280 20 ("Modified Time: " + $user.whenChanged) $UserFactsForm
$PWDaysLbl = createItem "Label" 10 190 280 20 (getPasswordAge) $UserFactsForm
$CertBtn = createItem "Button" 2 217 10 20 "" $UserFactsForm
$CertBtn.Visible = $False
$global:CertStepper = 0
$CertBtn.Add_Click({ stepCert })
$CertLbl = createItem "Label" 10 220 270 20 (getCertText) $UserFactsForm
$UserDepartLbl = createItem "Label" 10 250 280 20 ("Department: " + $user.Department) $UserFactsForm
$UserDescriptLbl = createItem "Label" 10 280 280 20 ("Description: " + $user.Description) $UserFactsForm
showForm $UserFactsForm