1+ # Requires -Version 4.0
2+ # Requires -Modules ActiveDirectory
3+
4+ <#
5+ . SYNOPSIS
6+ Lists computers where disabled or inactive
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/Computers
23+
24+ . Parameter OUPath
25+ Specifies the AD path
26+
27+ . Parameter DomainAccount
28+ Active Directory Credential for remote execution on jumphost without CredSSP
29+
30+ . Parameter Disabled
31+ Shows the disabled computers
32+
33+ . Parameter InActive
34+ Shows the inactive computers
35+
36+ . Parameter DomainName
37+ Name of Active Directory Domain
38+
39+ . Parameter SearchScope
40+ Specifies the scope of an Active Directory search
41+
42+ . Parameter AuthType
43+ Specifies the authentication method to use
44+ #>
45+
46+ param (
47+ [Parameter (Mandatory = $true , ParameterSetName = " Local or Remote DC" )]
48+ [Parameter (Mandatory = $true , ParameterSetName = " Remote Jumphost" )]
49+ [string ]$OUPath ,
50+ [Parameter (ParameterSetName = " Local or Remote DC" )]
51+ [Parameter (ParameterSetName = " Remote Jumphost" )]
52+ [switch ]$Disabled ,
53+ [Parameter (ParameterSetName = " Local or Remote DC" )]
54+ [Parameter (ParameterSetName = " Remote Jumphost" )]
55+ [switch ]$InActive ,
56+ [Parameter (Mandatory = $true , ParameterSetName = " Remote Jumphost" )]
57+ [PSCredential ]$DomainAccount ,
58+ [Parameter (ParameterSetName = " Local or Remote DC" )]
59+ [Parameter (ParameterSetName = " Remote Jumphost" )]
60+ [string ]$DomainName ,
61+ [Parameter (ParameterSetName = " Local or Remote DC" )]
62+ [Parameter (ParameterSetName = " Remote Jumphost" )]
63+ [ValidateSet (' Base' , ' OneLevel' , ' SubTree' )]
64+ [string ]$SearchScope = ' SubTree' ,
65+ [Parameter (ParameterSetName = " Local or Remote DC" )]
66+ [Parameter (ParameterSetName = " Remote Jumphost" )]
67+ [ValidateSet (' Basic' , ' Negotiate' )]
68+ [string ]$AuthType = " Negotiate"
69+ )
70+
71+ Import-Module ActiveDirectory
72+
73+ # Clear
74+ # $ErrorActionPreference='Stop'
75+ try {
76+ $resultMessage = @ ()
77+ if ($PSCmdlet.ParameterSetName -eq " Remote Jumphost" ){
78+ if ([System.String ]::IsNullOrWhiteSpace($DomainName )){
79+ $Domain = Get-ADDomain - Current LocalComputer - AuthType $AuthType - Credential $DomainAccount - ErrorAction Stop
80+ }
81+ else {
82+ $Domain = Get-ADDomain - Identity $DomainName - AuthType $AuthType - Credential $DomainAccount - ErrorAction Stop
83+ }
84+ if ([System.String ]::IsNullOrWhiteSpace($OUPath )){
85+ $OUPath = $Domain.DistinguishedName
86+ }
87+ if ($Disabled -eq $true ){
88+ $computers = Search-ADAccount - Credential $DomainAccount - Server $Domain.PDCEmulator - AuthType $AuthType - AccountDisabled - ComputersOnly `
89+ - SearchBase $OUPath - SearchScope $SearchScope | Select-Object DistinguishedName, SAMAccountName | Sort-Object - Property SAMAccountName
90+ if ($computers ){
91+ foreach ($itm in $computers ){
92+ $resultMessage = $resultMessage + (" Disabled: " + $itm.DistinguishedName + ' ;' + $itm.SamAccountName )
93+ }
94+ $resultMessage = $resultMessage + ' '
95+ }
96+ }
97+ if ($InActive -eq $true ){
98+ $computers = Search-ADAccount - Credential $DomainAccount - Server $Domain.PDCEmulator - AuthType $AuthType - AccountInactive - ComputersOnly `
99+ - SearchBase $OUPath - SearchScope $SearchScope | Select-Object DistinguishedName, SAMAccountName | Sort-Object - Property SAMAccountName
100+ if ($computers ){
101+ foreach ($itm in $computers ){
102+ $resultMessage = $resultMessage + (" Inactive: " + $itm.DistinguishedName + ' ;' + $itm.SamAccountName )
103+ }
104+ }
105+ }
106+ }
107+ else {
108+ if ([System.String ]::IsNullOrWhiteSpace($DomainName )){
109+ $Domain = Get-ADDomain - Current LocalComputer - AuthType $AuthType - ErrorAction Stop
110+ }
111+ else {
112+ $Domain = Get-ADDomain - Identity $DomainName - AuthType $AuthType - ErrorAction Stop
113+ }
114+ if ([System.String ]::IsNullOrWhiteSpace($OUPath )){
115+ $OUPath = $Domain.DistinguishedName
116+ }
117+ if ($Disabled -eq $true ){
118+ $computers = Search-ADAccount - Server $Domain.PDCEmulator - AuthType $AuthType - AccountDisabled - ComputersOnly `
119+ - SearchBase $OUPath - SearchScope $SearchScope | Select-Object DistinguishedName, SAMAccountName | Sort-Object - Property SAMAccountName
120+ if ($computers ){
121+ foreach ($itm in $computers ){
122+ $resultMessage = $resultMessage + (" Disabled: " + $itm.DistinguishedName + ' ;' + $itm.SamAccountName )
123+ }
124+ $resultMessage = $resultMessage + ' '
125+ }
126+ }
127+ if ($InActive -eq $true ){
128+ $computers = Search-ADAccount - Server $Domain.PDCEmulator - AuthType $AuthType - AccountInactive - ComputersOnly `
129+ - SearchBase $OUPath - SearchScope $SearchScope | Select-Object DistinguishedName, SAMAccountName | Sort-Object - Property SAMAccountName
130+ if ($computers ){
131+ foreach ($itm in $computers ){
132+ $resultMessage = $resultMessage + (" Inactive: " + $itm.DistinguishedName + ' ;' + $itm.SamAccountName )
133+ }
134+ }
135+ }
136+ }
137+ if ($SRXEnv ) {
138+ $SRXEnv.ResultMessage = $resultMessage
139+ }
140+ else {
141+ Write-Output $resultMessage
142+ }
143+ }
144+ catch {
145+ throw
146+ }
147+ finally {
148+ }
0 commit comments