/
PSSystemAdministrator.psm1
4100 lines (3060 loc) · 110 KB
/
PSSystemAdministrator.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
<#
This module is meant to be used in a Windows Domain by a domain administrator with PowerShell 7.
By:
Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
function Add-DHCPReservation{
<#
.SYNOPSIS
Adds a reservation in DHCP for a computer.
.DESCRIPTION
This function adds an IPv4 reservation for a computer in all DHCP servers in a domain.
.PARAMETER ComputerName
Name of the computer that will get a reservation in DHCP.
.PARAMETER IPAddress
IP address of the reservation made in DHCP.
.INPUTS
None.
.OUTPUTS
None.
.NOTES
This function is meant to be used in a domain with one DHCP scope.
This function will attempt to add the reservation to each DHCP server in the domain.
The target computer will not get the new address immediately if it does not already use that IP.
You can create a reservation with an IP that is currently leased to another computer. You will need to manually trigger an IP change in the computer currently holding the IP, then in the computer getting the new reservation. The computer in the reservation will then be using the assigned IP.
You can allow the DHCP servers to naturally renew their leases over time. Eventually the computer will get the IP assigned to it in the reservation.
If a reservation with the computer or IP address passed to the function already exists, then the function will stop and notify you which DHCP server it is located on.
If this function fails because it is unable to run the Get-DhcpServerInDC command, then run 'Import-Module DhcpServer -SkipEditionCheck' in Powershell to install the needed module.
.EXAMPLE
Add-DHCPReservation -ComputerName "Computer1" -IPAddress 10.10.10.123
This will create a reservation in all available DHCP servers in a domain for the computer name passed to it for the IP address passed to it.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[cmdletbinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory=$true)]
[string]$ComputerName,
[Parameter(Mandatory=$true)]
[string]$IPAddress,
[string]$Description = ""
)
Import-Module DhcpServer -SkipEditionCheck
$dhcpServers = (Get-DhcpServerInDC).DnsName
$hostName = (Get-ADComputer -Identity $ComputerName).DNSHostName
$reservationExists = $false
foreach($server in $dhcpServers){
$dhcpServer = $server.split(".")[0]
$scopeId = (Get-DhcpServerv4Scope -ComputerName $dhcpServer | Select-Object -First 1).ScopeId
$reservations = Get-DhcpServerv4Reservation -ScopeId $scopeId -ComputerName $dhcpServer
# Check for reservations that already contain the host name or ip address passed to the funciton.
foreach($r in $reservations){
if($r.Name -EQ $hostName){
Write-Host "The computer $hostName already has a reservation on $dhcpServer"
$reservationExists = $true
}
if($r.IPAddress -EQ $IPAddress){
Write-Host "The IP address $IPAddress already has a reservation on $dhcpServer"
$reservationExists = $true
}
}
}
if($reservationExists -eq $true){
return
}
foreach($server in $dhcpServers){
$dhcpServer = $server.split(".")[0]
$scopeId = (Get-DhcpServerv4Scope -ComputerName $dhcpServer | Select-Object -First 1).ScopeId
$dhcpLeases = Get-DhcpServerv4Lease -ComputerName $dhcpServer -ScopeId $scopeId -AllLeases
$clientId = ($dhcpLeases | Where-Object -Property HostName -EQ $hostName | Select-Object -First 1).ClientId
if ($PSCmdlet.ShouldProcess($server, "Adding dhcp reservation for $ComputerName for $IPAddress")) {
Add-DhcpServerv4Reservation -ScopeId $scopeId -ComputerName $dhcpServer -IPAddress $IPAddress -ClientId $clientId -Description $Description -Name $hostName
}
}
}
function Disable-Computer{
<#
.SYNOPSIS
Disables a computer.
.DESCRIPTION
Disables a computer or group of computers by passing host names or computer AD objects to this function.
.PARAMETER Name
This is the host name of the computer that will be disable.
.INPUTS
Computer AD objects can be passed to this function from the pipeline.
.OUTPUTS
None.
.NOTES
.EXAMPLE
Disable-Computer -Name "Computer1"
Disables the computer named "Computer1" in Active Directory.
.EXAMPLE
"Computer1","Computer2" | Disable-Computer
Disables computers Computer1 and Computer2 in Active Directory.
.EXAMPLE
Get-ADComputer Computer1 | Disable-Computer
Disables Computer1 in Active Directory.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[cmdletbinding(SupportsShouldProcess)]
param(
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)]
[Alias('ComputerName')]
[string]$Name
)
begin{
$computers = @()
}
process{
$computers += $Name
}
end{
foreach ($computer in $computers){
if($PSCmdlet.ShouldProcess($computer)){
$computer | Get-ADComputer | Disable-ADAccount
}
}
}
}
function Disable-User{
<#
.SYNOPSIS
Disables a user.
.DESCRIPTION
Disables a user or group of users by passing SamAccountName or user AD objects to this funtion.
.PARAMETER SamAccountName
This is the user name of the user that will be disabled.
.INPUTS
User AD objects can be passed to this function.
.OUTPUTS
None.
.NOTES
.EXAMPLE
Disable-User -Name "User1"
Disables the user named User1 in Active Directory.
.EXAMPLE
"User1","User2" | Disable-User
Disables users User1 and User2 in Active Directory.
.EXAMPLE
Get-ADUser "User1" | Disable-User
Disables User1 in Active Directory.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[cmdletbinding(SupportsShouldProcess)]
param(
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)]
[string]$SamAccountName
)
begin{
$users = @()
}
process{
$users += $SamAccountName
}
end{
foreach ($user in $users){
if($PSCmdlet.ShouldProcess($user)){
$user | Get-ADUser | Disable-ADAccount
}
}
}
}
function Enable-Computer{
<#
.SYNOPSIS
Enables a computer.
.DESCRIPTION
Enables a computer or group of computers by passing host names or computer AD objects to this function.
.PARAMETER Name
This is the host name of the computer that will be enable.
.INPUTS
Computer AD objects can be passed to this function from the pipeline.
.OUTPUTS
None.
.NOTES
.EXAMPLE
Enable-Computer -Name "Computer1"
Enables the computer named "Computer1" in Active Directory.
.EXAMPLE
"Computer1","Computer2" | Enable-Computer
Enables computers Computer1 and Computer2 in Active Directory.
.EXAMPLE
Get-ADComputer Computer1 | Enable-Computer
Enables Computer1 in Active Directory.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[cmdletbinding(SupportsShouldProcess)]
param(
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)]
[Alias('ComputerName')]
[string]$Name
)
begin{
$computers = @()
}
process{
$computers += $Name
}
end{
foreach ($computer in $computers){
if($PSCmdlet.ShouldProcess($computer)){
$computer | Get-ADComputer | Enable-ADAccount
}
}
}
}
function Enable-User {
<#
.SYNOPSIS
Enables a user.
.DESCRIPTION
Enables a user or group of users by passing SamAccountNames or user AD objects to this funtion.
.PARAMETER SamAccountName
This is the user name of the user that will be enabled.
.INPUTS
User AD objects can be passed to this function.
.OUTPUTS
None.
.NOTES
.EXAMPLE
Enable-User -SamAccountName "User1"
Enables the user named User1 in Active Directory.
.EXAMPLE
"User1","User2" | Enable-User
Enables users User1 and User2 in Active Directory.
.EXAMPLE
Get-ADUser "User1" | Enable-User
Enables User1 in Active Directory.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[cmdletbinding(SupportsShouldProcess)]
param(
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)]
[string]$SamAccountName
)
begin{
$users = @()
}
process{
$users += $SamAccountName
}
end{
foreach ($user in $users){
if($PSCmdlet.ShouldProcess($user)){
$user | Get-ADUser | Enable-ADAccount
}
}
}
}
function Enable-WakeOnLan{
<#
.SYNOPSIS
Configures a computer to allow wake on lan.
.DESCRIPTION
Configures a computer's ethernet network adapter to respond to wake on lan commands. This allow the computer to be turned on while it is shut down.
.PARAMETER Name
Target computer's host name.
.INPUTS
Computer AD objects
.OUTPUTS
None.
.NOTES
This function needs to be run against a computer before you can be sure that the Start-Computer function in the PSSystemAdministrator modile will work.
.EXAMPLE
Enable-WakeOnLan -Name 'Computer1'
Sets the network adapter on 'Computer1' to respond to WOL commands and boot from a shutdown state.
.EXAMPLE
'Computer1','Computer2' | Enable-WakeOnLan
Sets the network adapters on 'Computer1' and 'Computer2' to respond to WOL commands and boot from a shutdown state.
.EXAMPLE
Get-OUComputer -OrganizationalUnit 'Department X' | Enable-WakeOnLan
Sets the network adapters on all computers in the 'Department X' OU to respond to WOL commands and boot from a shutdown state.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
Based on: https://docs.microsoft.com/en-us/powershell/module/netadapter/enable-netadapterpowermanagement
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
[Alias("ComputerName")]
[string]$Name
)
begin{
$domainController = (Get-ADDomainController).Name
$scopeID = (Get-DhcpServerv4Scope -ComputerName $domainController).ScopeID
}
process{
$cimSession = New-CimSession -ComputerName $Name
$computerMAC = (Get-DhcpServerv4Lease -ComputerName $domainController -ScopeId $scopeID | Where-Object -Property hostname -match $Name).clientid
$adapterName = (Get-NetAdapter -CimSession $cimSession | Where-Object -Property MacAddress -match $computerMAC).Name
if($PSCmdlet.ShouldProcess($Name)){
Enable-NetAdapterPowerManagement -CimSession $cimSession -Name $adapterName -WakeOnMagicPacket
}
}
end{}
}
function Get-AccessedFile{
<#
.SYNOPSIS
Gets all files in a directory that have been accessed in the last 24 hours.
.DESCRIPTION
Gets all files in a directory recursively that have been accessed less than a day ago. Directory and days in the past can be adjusted.
.PARAMETER Path
Function will gather all files recursively from the directory at the end of the path.
.PARAMETER Days
Function will return only files that have been accessed this number of days into the past.
.INPUTS
You can pipe multiple paths to this function.
.OUTPUTS
Array of PS objects that includes file Name, LastAccessTme, SizeMB, and FullName.
.NOTES
.EXAMPLE
Get-AccessedFile -Path "C:\Directory1" -Days
Gets all files recursively in the "Directory1" folder that have been accessed within 5 days.
.EXAMPLE
"C:\Directory1","C:\Directory2" | Get-AccessedFile
Gets all files recursively in the "Directory1" and "Directory2" folders that have been accessed in the last day.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[cmdletbinding()]
param(
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)]
[Alias('FullName')]
[string]$Path,
[int]$Days = 1
)
begin{
$files = @()
$fileAge = (Get-Date).AddDays(-1 * $Days)
}
process{
$files += Get-ChildItemLastAccessTime -Path $Path |
Where-Object -Property LastAccessTime -GT $fileAge
}
end{
return $files
}
}
function Get-ActiveComputer{
<#
.SYNOPSIS
Gets a list of computers that have logged onto the domain in the last 30 days.
.DESCRIPTION
Gets a list of computers from Active Directory that have logged onto the domain in the last 30 days. By default all computers are checked, but can be limited to a specific organizational unit. Days inactive can be adjusted.
.PARAMETER Days
Sets how recently in days the computer account has to be active for it to be returned.
.PARAMETER OrganizationalUnit
Limits the function to a specific organizational unit.
.INPUTS
None.
.OUTPUTS
Computer AD object for each computer returned.
.NOTES
.EXAMPLE
Get-ActiveComputer
Lists all computers in the domain that have been online in the last 30 days.
.EXAMPLE
Get-ActiveComputer -Days 100
Lists all computers in the domain that have been online in the last 100 days.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[CmdletBinding()]
Param(
[int]$Days = 30,
[string]$OrganizationalUnit = ""
)
if($OrganizationalUnit -eq ""){
$computers = Get-ADComputer -Filter * |
Get-ComputerLastLogonTime |
Where-Object -Property LastLogon -GT ((Get-Date).AddDays(($Days * -1)))
}else{
$computers = Get-OUComputer -OrganizationalUnit $OrganizationalUnit |
Get-ComputerLastLogonTime |
Where-Object -Property LastLogon -GT ((Get-Date).AddDays(($Days * -1)))
}
return $computers
}
function Get-ActiveFile{
<#
.SYNOPSIS
Gets all files in a directory that have been written in the last 24 hours.
.DESCRIPTION
Gets all files in a directory recursively that have been written to going back one day. Path and days back can be adjusted.
.PARAMETER Path
Sets directory the function returns files from.
.PARAMETER Days
Sets how recently, in days, the file has been written to.
.INPUTS
You can pipe multiple paths to this function.
.OUTPUTS
Array of PS objects that includes file Name, LastWriteTime, SizeMB, and FullName.
.NOTES
.EXAMPLE
Get-ActiveFile -Path "C:\Directory1" -Days 5
Gathers all files recursively in the "Directory1" folder that have been written to within 5 days.
.EXAMPLE
"C:\Directory1","C:\Directory2" | Get-ActiveFile
Gathers all files recursively in the "Directory1" and "Directory2" folders that have been written to in the last day.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[cmdletbinding()]
param(
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)]
[string]$Path,
[int]$Days = 1
)
begin{
$files = @()
$fileAge = (Get-Date).AddDays(-1*$Days)
}
process{
$files += Get-ChildItemLastWriteTime -Path $Path |
Where-Object -Property LastWriteTime -GT $fileAge
}
end{
return $files
}
}
function Get-ActiveUser{
<#
.SYNOPSIS
Gets a list of all users that have logged on in the last 30 days.
.DESCRIPTION
Gets a list of all users in active directory that have logged on in the last 30 days. Days and Organizational Unit can be adjusted.
.PARAMETER Days
Sets how long the user account has to be inactive for it to be returned.
.PARAMETER OrganizationalUnit
Focuses the function on a specific AD organizational unit.
.INPUTS
None.
.OUTPUTS
PS objects with user names and last logon date.
.NOTES
Function is intended to help find inactive user accounts.
.EXAMPLE
Get-ActiveUser
Lists all users in the domain that have not checked in for more than 3 months.
.EXAMPLE
Get-ActiveUser -Days 2
Lists all users in the domain that have not checked in for more than 2 days.
.EXAMPLE
Get-ActiveUser -Days 45 -OrganizationalUnit "Company Servers"
Lists all users in the domain that have not checked in for more than 45 days in the "Company Servers" organizational unit.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[CmdletBinding()]
Param(
[int]$Days = 30,
[string]$OrganizationalUnit = ""
)
if($OrganizationalUnit -eq ""){
$users = Get-ADUser -Filter * |
Get-UserLastLogonTime |
Where-Object -Property LastLogon -GT ((Get-Date).AddDays($Days * -1))
}else{
$users = Get-OUUser -OrganizationalUnit $OrganizationalUnit |
Get-UserLastLogonTime |
Where-Object -Property LastLogon -GT ((Get-Date).AddDays($Days * -1))
}
return $users
}
function Get-ChildItemLastAccessTime{
<#
.SYNOPSIS
Gets all files in a directory and returns information including file name and last access time.
.DESCRIPTION
Gets all files in a directory recursively and returns file name, last access time, size in MB, and full name.
.PARAMETER Path
Function will gather all files recursively from this directory.
.INPUTS
You can pipe multiple paths to this function.
.OUTPUTS
Array of PS objects that includes FileNames, LastAccessTime, SizeMB, and FullName.
.NOTES
.EXAMPLE
Get-ChildItemLastAccessTime -Path "C:\Directory1"
Gathers information on all files recursively in the "Directory1" directory.
.EXAMPLE
"C:\Directory1","C:\Directory2" | Get-ChildItemLastAccessTime
Gathers all files recursively in the "Directory1" and "Directory2" folders.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[cmdletbinding()]
param(
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)]
[string]$Path
)
begin{
$files = @()
}
process{
$files += Get-ChildItem -Path $Path -File -Recurse | Get-ItemLastAccessTime
}
end{
return $files
}
}
function Get-ChildItemLastWriteTime{
<#
.SYNOPSIS
Gets all files in a directory and returns information including last write time.
.DESCRIPTION
This function gathers all files in a directory recursively. Returns file name, last write time, size in MB, and full name.
.PARAMETER Path
Function will gather all files recursively from this directory.
.INPUTS
None.
.OUTPUTS
Array of PS objects that includes file Name, LastWriteTime, SizeMB, and FullName.
.NOTES
.EXAMPLE
Get-ChildItemLastWriteTime -Path "C:\Directory1"
Gathers all files recursively in the "Directory1" folder and returns file names, last write time, size in MB, and full name.
.EXAMPLE
"C:\Directory1","C:\Directory2" | Get-ChildItemLastWriteTime
Gathers all files recursively in the "Directory1" and "Directory2" folders.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[cmdletbinding()]
param(
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)]
[string]$Path
)
begin{
$files = @()
}
process{
$files += Get-ChildItem -Path $Path -File -Recurse | Get-ItemLastWriteTime
}
end{
return $files
}
}
function Get-ComputerCurrentUser{
<#
.SYNOPSIS
Gets the current user logged onto a computer.
.DESCRIPTION
Gets the currently logged in user on a computer or computers.
.PARAMETER Name
The host name of the computer that the current user will be returned from.
.INPUTS
An array of host names or AD Computer objects.
.OUTPUTS
Returns a PS Object with the computer Name and CurrentUser.
.NOTES
.EXAMPLE
Get-ComputerCurrentUser
Gets the current user on the local computer.
.EXAMPLE
Get-ComputerCurrentUser -Name 'Computer1'
Gets the current logged on user from 'Computer1'.
.EXAMPLE
'Computer1','Computer2' | Get-ComputerCurrentUser
Returns the current logged on user from 'Computer1' and 'Computer2'.
.EXAMPLE
Get-OUComputer -OrganizationalUnit 'Department X' | Get-ComputerCurrentUser
Returns the current logged on users for all computer in the 'Deparment X' Active Directory organizational unit. Get-OUComputer is a function from the PSSystemAdministrator module.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[CmdletBinding()]
Param(
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[Alias('ComputerName')]
[string]$Name = $env:COMPUTERNAME
)
begin{
$computerUserList = @()
}
process{
$currentUser = (Get-CimInstance -ComputerName $Name -ClassName Win32_ComputerSystem).UserName
if(!$null -eq $currentUser){
$currentUser = $currentUser.split('\')[-1]
}else{
$currentUser = ""
}
$computerUserList += [PSCustomObject]@{
ComputerName = $Name;
UserName = $currentUser
}
}
end{
return $computerUserList
}
}
function Get-ComputerDriveInformation{
<#
.SYNOPSIS
Gets information about the drives on a computer.
.DESCRIPTION
Returns information about the drives on a computer or group of computers. The information includes computer name, drive, volume name, size, free space, and if the drive has less than 20% space left.
.PARAMETER Name
Specifies the computer the function will gather information from.
.INPUTS
You can pipe host names or AD computer objects.
.OUTPUTS
Returns PS objects to the host the following information about the drives on a computer: Name, DeviceID, VolumeName,SizeGB, FreeGB, and indicates those under 20% desc space remaining.
.NOTES
Compatible with Window 7 and newer.
Results include mapped drives.
.EXAMPLE
Get-ComputerDriveInformation
Gets drive information for the local host.
.EXAMPLE
Get-ComputerDriveInformation -Name computer
Gets ddrive information for "computer".
.EXAMPLE
Get-ADComputer -Filter * | Get-ComputerDriveInformation
Gets drive information for all computers in AD.
.LINK
By Ben Peterson
linkedin.com/in/benponline
github.com/benponline
twitter.com/benponline
paypal.me/teknically
#>
[CmdletBinding()]
Param(
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
[Alias('ComputerName')]
[string]$Name = $env:COMPUTERNAME
)
begin{
$driveInformationList = @()
}
process{
$driveInformationList += Get-CimInstance -ComputerName $Name -ClassName win32_logicaldisk -Property DeviceID,VolumeName,Size,FreeSpace,DriveType |
Where-Object -Property DriveType -EQ 3 |
Select-Object -Property @{n="Computer";e={$Name}},`
@{n="DeviceID";e={$_.deviceid}},`
@{n="VolumeName";e={$_.volumename}},`
@{n="SizeGB";e={$_.size / 1GB -as [int]}},`
@{n="FreeGB";e={$_.freespace / 1GB -as [int]}},`
@{n="Under20Percent";e={if(($_.freespace / $_.size) -le 0.2){"True"}else{"False"}}}
}
end{
$driveInformationList = $driveInformationList | Where-Object -Property SizeGB -NE 0
return $driveInformationList
}
}
function Get-ComputerFailedLogonEvent{
<#
.SYNOPSIS
Gets failed logon events from a computer in the last day.
.DESCRIPTION
Gets failed logon events from a computer or computers in the last day. Can adjust how far back in days events are returned.
.PARAMETER Name
Host name of the computer events will be returned from.
.PARAMETER Days
Sets how far back in days the function will look for failed logon events.
.INPUTS
Computer AD objects.
.OUTPUTS
PS objects for computer failed logon events with Computer, TimeWritten, EventID, InstanceId, and Message.
.NOTES
Requires administrator privilages.
Requires "Printer and file sharing", "Network Discovery", and "Remote Registry" to be enabled on computers that are searched. This funtion can take a long time to complete if more than 5 computers are searched.
.EXAMPLE
Get-ComputerFailedLogonEvent
This cmdlet returns the last 5 system errors from localhost.
.EXAMPLE
Get-ComputerFailedLogonEvent -Name "Server" -Days 2
This function returns the last 2 days of failed logon events from Server.
.EXAMPLE
"computer1","computer2" | Get-ComputerFailedLogonEvent
This function returns failed logon events from "computer1" and "computer2" in the last day.
.EXAMPLE
Get-ADComputer "Computer1" | Get-ComputerFailedLogonEvent