Reputation: 3325
(([datetime]::FromFileTime((Get-ADUser –Identity username -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"))-(Get-Date)).Days
Will output the amount of time until an AD account expires. However, when I try to place this in a script by setting a variable equal to it, it will crash and close real fast and it doesn't work. I am unsure why.
EDIT:
Running this command only in my script, I get an error:
$Expiration = (([datetime]::FromFileTime((Get-ADUser –Identity 'MyTestUser' -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"))-(Get-Date)).Days
Error:
At C:\Users\ajstepanik\Desktop\test.ps1:1 char:161
+ ... iryTimeComputed"))-(Get-Date)).Days
+ ~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
At C:\Users\ajstepanik\Desktop\test.ps1:1 char:181
+ ... Get-Date)).Days
+ ~
Missing closing ')' in expression.
At C:\Users\ajstepanik\Desktop\test.ps1:1 char:181
+ ... Get-Date)).Days
+ ~
Missing ')' in method call.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
ception
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Upvotes: 2
Views: 12584
Reputation: 2149
When you say you're trying to set this to a variable do you mean the value? We can assign a variable to the resulting value by placing a variable before the code like so
$Expiration = (([datetime]::FromFileTime((Get-ADUser –Identity 'MyTestUser' -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"))-(Get-Date)).Days
If you wanted to make it a function you could do so like this and doing the same but calling the function instead of the code
function Get-PasswordExpirationDays ($User)
{
(([datetime]::FromFileTime((Get-ADUser –Identity $User -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"))-(Get-Date)).Days
}
$Expiration = Get-PasswordExpirationDays 'MyTestUser'
Both will set $Expiration to how many days until a persons password expires but the function will be easier to use multiple times.
Upvotes: 4