1
+ # Requires -Version 4.0
2
+ # Requires -Modules ActiveDirectory
3
+
4
+ <#
5
+ . SYNOPSIS
6
+ Sets the expiration date for an Active Directory account
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/Users
23
+
24
+ . Parameter OUPath
25
+ Specifies the AD path
26
+
27
+ . Parameter Username
28
+ Display name, SAMAccountName, DistinguishedName or user principal name of an Active Directory account
29
+
30
+ . Parameter DomainAccount
31
+ Active Directory Credential for remote execution without CredSSP
32
+
33
+ . Parameter Day
34
+ Specifies the day of the expiration date for an Active Directory account
35
+
36
+ . Parameter Month
37
+ Specifies the month of the expiration date for an Active Directory account
38
+
39
+ . Parameter Year
40
+ Specifies the year of the expiration date for an Active Directory account
41
+
42
+ . Parameter NeverExpires
43
+ Specifies the Active Directory account never expires
44
+
45
+ . Parameter DomainName
46
+ Name of Active Directory Domain
47
+
48
+ . Parameter SearchScope
49
+ Specifies the scope of an Active Directory search
50
+
51
+ . Parameter AuthType
52
+ Specifies the authentication method to use
53
+ #>
54
+
55
+ param (
56
+ [Parameter (Mandatory = $true , ParameterSetName = " Local or Remote DC" )]
57
+ [Parameter (Mandatory = $true , ParameterSetName = " Remote Jumphost" )]
58
+ [string ]$OUPath ,
59
+ [Parameter (Mandatory = $true , ParameterSetName = " Local or Remote DC" )]
60
+ [Parameter (Mandatory = $true , ParameterSetName = " Remote Jumphost" )]
61
+ [string ]$Username ,
62
+ [Parameter (Mandatory = $true , ParameterSetName = " Remote Jumphost" )]
63
+ [PSCredential ]$DomainAccount ,
64
+ [Parameter (ParameterSetName = " Local or Remote DC" )]
65
+ [Parameter (ParameterSetName = " Remote Jumphost" )]
66
+ [ValidateRange (1 , 31 )]
67
+ [int ]$Day = 1 ,
68
+ [Parameter (ParameterSetName = " Local or Remote DC" )]
69
+ [Parameter (ParameterSetName = " Remote Jumphost" )]
70
+ [ValidateRange (1 , 12 )]
71
+ [int ]$Month = 1 ,
72
+ [Parameter (ParameterSetName = " Local or Remote DC" )]
73
+ [Parameter (ParameterSetName = " Remote Jumphost" )]
74
+ [ValidateRange (2017 , 2030 )]
75
+ [int ]$Year ,
76
+ [Parameter (ParameterSetName = " Local or Remote DC" )]
77
+ [Parameter (ParameterSetName = " Remote Jumphost" )]
78
+ [switch ]$NeverExpires ,
79
+ [Parameter (ParameterSetName = " Local or Remote DC" )]
80
+ [Parameter (ParameterSetName = " Remote Jumphost" )]
81
+ [string ]$DomainName ,
82
+ [Parameter (ParameterSetName = " Local or Remote DC" )]
83
+ [Parameter (ParameterSetName = " Remote Jumphost" )]
84
+ [ValidateSet (' Base' , ' OneLevel' , ' SubTree' )]
85
+ [string ]$SearchScope = ' SubTree' ,
86
+ [Parameter (ParameterSetName = " Local or Remote DC" )]
87
+ [Parameter (ParameterSetName = " Remote Jumphost" )]
88
+ [ValidateSet (' Basic' , ' Negotiate' )]
89
+ [string ]$AuthType = " Negotiate"
90
+ )
91
+
92
+ Import-Module ActiveDirectory
93
+
94
+ # Clear
95
+ # $ErrorActionPreference='Stop'
96
+ try {
97
+ $Script :Domain
98
+ $Script :User
99
+
100
+ if ($PSCmdlet.ParameterSetName -eq " Remote Jumphost" ){
101
+ if ([System.String ]::IsNullOrWhiteSpace($DomainName )){
102
+ $Script :Domain = Get-ADDomain - Current LocalComputer - AuthType $AuthType - Credential $DomainAccount - ErrorAction Stop
103
+ }
104
+ else {
105
+ $Script :Domain = Get-ADDomain - Identity $DomainName - AuthType $AuthType - Credential $DomainAccount - ErrorAction Stop
106
+ }
107
+ $Script :User = Get-ADUser - Server $Script :Domain.PDCEmulator - Credential $DomainAccount - AuthType $AuthType `
108
+ - SearchBase $OUPath - SearchScope $SearchScope `
109
+ - Filter {(SamAccountName -eq $Username ) -or (DisplayName -eq $Username ) -or (DistinguishedName -eq $Username ) -or (UserPrincipalName -eq $Username )} - ErrorAction Stop
110
+ }
111
+ else {
112
+ if ([System.String ]::IsNullOrWhiteSpace($DomainName )){
113
+ $Script :Domain = Get-ADDomain - Current LocalComputer - AuthType $AuthType - ErrorAction Stop
114
+ }
115
+ else {
116
+ $Script :Domain = Get-ADDomain - Identity $DomainName - AuthType $AuthType - ErrorAction Stop
117
+ }
118
+ $Script :User = Get-ADUser - Server $Script :Domain.PDCEmulator - AuthType $AuthType `
119
+ - SearchBase $OUPath - SearchScope $SearchScope `
120
+ - Filter {(SamAccountName -eq $Username ) -or (DisplayName -eq $Username ) -or (DistinguishedName -eq $Username ) -or (UserPrincipalName -eq $Username )} - ErrorAction Stop
121
+ }
122
+ if ($null -ne $Script :User ){
123
+ $Out = ' '
124
+ if ($NeverExpires -eq $true ){
125
+ if ($PSCmdlet.ParameterSetName -eq " Remote Jumphost" ){
126
+ Set-ADUser - Identity $Script :User.SamAccountName - Credential $DomainAccount - AuthType $AuthType - Server $Script :Domain.PDCEmulator - AccountExpirationDate $null - ErrorAction Stop
127
+ }
128
+ else {
129
+ Set-ADUser - Identity $Script :User.SamAccountName - AuthType $AuthType - Server $Script :Domain.PDCEmulator - AccountExpirationDate $null - ErrorAction Stop
130
+ }
131
+ }
132
+ else {
133
+ [datetime ]$start = New-Object DateTime $Year , $Month , $Day
134
+ if ($start.ToFileTimeUtc () -lt [DateTime ]::Now.ToFileTimeUtc()){
135
+ Throw " Expiration date is in the past"
136
+ }
137
+ if ($PSCmdlet.ParameterSetName -eq " Remote Jumphost" ){
138
+ Set-ADUser - Identity $Script :User.SamAccountName - Credential $DomainAccount - AuthType $AuthType - Server $Script :Domain.PDCEmulator - AccountExpirationDate $start - ErrorAction Stop
139
+ }
140
+ else {
141
+ Set-ADUser - Identity $Script :User.SamAccountName - AuthType $AuthType - Server $Script :Domain.PDCEmulator - AccountExpirationDate $start - ErrorAction Stop
142
+ }
143
+ }
144
+ Start-Sleep - Seconds 5 # wait
145
+ if ($PSCmdlet.ParameterSetName -eq " Remote Jumphost" ){
146
+ $Script :User = Get-ADUser - Identity $Script :User.SAMAccountName - Properties * - Credential $DomainAccount - AuthType $AuthType - Server $Script :Domain.PDCEmulator
147
+ }
148
+ else {
149
+ $Script :User = Get-ADUser - Identity $Script :User.SAMAccountName - Properties * - AuthType $AuthType - Server $Script :Domain.PDCEmulator
150
+ }
151
+ if ([System.String ]::IsNullOrWhiteSpace($Script :User.AccountExpirationDate )){
152
+ $Out = " Account for user $ ( $Username ) never expires"
153
+ }
154
+ else {
155
+ $Out = [System.TimeZone ]::CurrentTimeZone.ToLocalTime([System.DateTime ]::FromFileTimeUtc($Script :User.accountExpires ))
156
+ $Out = " Account for user $ ( $Username ) expires on the $ ( $Out ) . Please inform the user in time."
157
+ }
158
+ if ($SRXEnv ) {
159
+ $SRXEnv.ResultMessage = $Out
160
+ }
161
+ else {
162
+ Write-Output $Out
163
+ }
164
+ }
165
+ else {
166
+ if ($SRXEnv ) {
167
+ $SRXEnv.ResultMessage = " User $ ( $Username ) not found"
168
+ }
169
+ Throw " User $ ( $Username ) not found"
170
+ }
171
+ }
172
+ catch {
173
+ throw
174
+ }
175
+ finally {
176
+ }
0 commit comments