1+ # Requires -Version 4.0
2+ # Requires -Modules ActiveDirectory
3+
4+ <#
5+ . SYNOPSIS
6+ Lists users where disabled, inactive, locked out and/or account is expired
7+
8+ . DESCRIPTION
9+
10+ . NOTES
11+ This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
12+ The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
13+ The terms of use for ScriptRunner do not apply to this script. In particular, AppSphere AG assumes no liability for the function,
14+ the use and the consequences of the use of this freely available script.
15+ PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of AppSphere AG.
16+ © AppSphere AG
17+
18+ . COMPONENT
19+ Requires Module ActiveDirectory
20+
21+ . LINK
22+ https://github.com/scriptrunner/ActionPacks/tree/master/ActiveDirectory/_QUERY_
23+
24+ . Parameter OUPath
25+ Specifies the AD path
26+
27+ . Parameter DomainAccount
28+ Active Directory Credential
29+
30+ . Parameter Disabled
31+ Show the users where account disabled
32+
33+ . Parameter InActive
34+ Show the users where account inactive
35+
36+ . Parameter Locked
37+ Show the users where account locked
38+
39+ . Parameter Expired
40+ Show the users where account expired
41+
42+ . Parameter DomainName
43+ Name of Active Directory Domain
44+
45+ . Parameter SearchScope
46+ Specifies the scope of an Active Directory search
47+
48+ . Parameter AuthType
49+ Specifies the authentication method to use
50+ #>
51+
52+ param (
53+ [Parameter (Mandatory = $true )]
54+ [string ]$OUPath ,
55+ [PSCredential ]$DomainAccount ,
56+ [bool ]$Disabled ,
57+ [bool ]$InActive ,
58+ [bool ]$Locked ,
59+ [bool ]$Expired ,
60+ [string ]$DomainName ,
61+ [ValidateSet (' Base' , ' OneLevel' , ' SubTree' )]
62+ [string ]$SearchScope = ' SubTree' ,
63+ [ValidateSet (' Basic' , ' Negotiate' )]
64+ [string ]$AuthType = " Negotiate"
65+ )
66+
67+ Import-Module ActiveDirectory
68+
69+ try {
70+ $Script :users = @ ()
71+ if ($null -ne $DomainAccount ){
72+ if ([System.String ]::IsNullOrWhiteSpace($DomainName )){
73+ $Domain = Get-ADDomain - Current LocalComputer - AuthType $AuthType - Credential $DomainAccount - ErrorAction Stop
74+ }
75+ else {
76+ $Domain = Get-ADDomain - Identity $DomainName - AuthType $AuthType - Credential $DomainAccount - ErrorAction Stop
77+ }
78+ if ([System.String ]::IsNullOrWhiteSpace($OUPath )){
79+ $OUPath = $Domain.DistinguishedName
80+ }
81+ if ($Disabled -eq $true ){
82+ $Script :users += Search-ADAccount - Server $Domain.PDCEmulator - Credential $DomainAccount - AuthType $AuthType - AccountDisabled - UsersOnly - SearchBase $OUPath - SearchScope $SearchScope `
83+ | Select-Object DistinguishedName, SamAccountName | Sort-Object - Property SamAccountName
84+ }
85+ if ($InActive -eq $true ){
86+ $Script :users += Search-ADAccount - Server $Domain.PDCEmulator - Credential $DomainAccount - AuthType $AuthType - AccountInactive - UsersOnly - SearchBase $OUPath - SearchScope $SearchScope `
87+ | Select-Object DistinguishedName, SamAccountName | Sort-Object - Property SamAccountName
88+ }
89+ if ($Locked -eq $true ){
90+ $Script :users += Search-ADAccount - Server $Domain.PDCEmulator - Credential $DomainAccount - AuthType $AuthType - LockedOut - UsersOnly - SearchBase $OUPath - SearchScope $SearchScope `
91+ | Select-Object DistinguishedName, SamAccountName | Sort-Object - Property SamAccountName
92+ }
93+ if ($Expired -eq $true ){
94+ $Script :users += Search-ADAccount - Server $Domain.PDCEmulator - Credential $DomainAccount - AuthType $AuthType - AccountExpired - UsersOnly - SearchBase $OUPath - SearchScope $SearchScope `
95+ | Select-Object DistinguishedName, SamAccountName | Sort-Object - Property SamAccountName
96+ }
97+ }
98+ else {
99+ if ([System.String ]::IsNullOrWhiteSpace($DomainName )){
100+ $Domain = Get-ADDomain - Current LocalComputer - AuthType $AuthType - ErrorAction Stop
101+ }
102+ else {
103+ $Domain = Get-ADDomain - Identity $DomainName - AuthType $AuthType - ErrorAction Stop
104+ }
105+ if ([System.String ]::IsNullOrWhiteSpace($OUPath )){
106+ $OUPath = $Domain.DistinguishedName
107+ }
108+ if ($Disabled -eq $true ){
109+ $Script :users += Search-ADAccount - Server $Domain.PDCEmulator - AuthType $AuthType - AccountDisabled - UsersOnly - SearchBase $OUPath - SearchScope $SearchScope `
110+ | Select-Object DistinguishedName, SamAccountName | Sort-Object - Property SamAccountName
111+ }
112+ if ($InActive -eq $true ){
113+ $Script :users += Search-ADAccount - Server $Domain.PDCEmulator - AuthType $AuthType - AccountInactive - UsersOnly - SearchBase $OUPath - SearchScope $SearchScope `
114+ | Select-Object DistinguishedName, SamAccountName | Sort-Object - Property SamAccountName
115+ }
116+ if ($Locked -eq $true ){
117+ $Script :users += Search-ADAccount - Server $Domain.PDCEmulator - AuthType $AuthType - LockedOut - UsersOnly - SearchBase $OUPath - SearchScope $SearchScope `
118+ | Select-Object DistinguishedName, SamAccountName | Sort-Object - Property SamAccountName
119+ }
120+ if ($Expired -eq $true ){
121+ $Script :users += Search-ADAccount - Server $Domain.PDCEmulator - AuthType $AuthType - AccountExpired - UsersOnly - SearchBase $OUPath - SearchScope $SearchScope `
122+ | Select-Object DistinguishedName, SamAccountName | Sort-Object - Property SamAccountName
123+ }
124+ }
125+ if ($SRXEnv ) {
126+ $SRXEnv.ResultList = @ ()
127+ $SRXEnv.ResultList2 = @ ()
128+ }
129+ if ($null -ne $Script :users ){
130+ foreach ($itm in $users ){
131+ if ($SRXEnv ) {
132+ $SRXEnv.ResultList += $itm.DistinguishedName # Value
133+ $SRXEnv.ResultList2 += $itm.SamAccountName # DisplayValue
134+ }
135+ else {
136+ Write-Output $itm.SamAccountName
137+ }
138+ }
139+ }
140+ }
141+ catch {
142+ throw
143+ }
144+ finally {
145+ }
0 commit comments