问题描述
早上好
尝试确定AD用户密码的年龄时遇到问题。
运行以下脚本部分时:
#Check for Fine Grained Passwords
$PasswordPol = (Get-ADUserResultantPasswordPolicy $user)
if (($PasswordPol) -ne $null)
{
$maxPasswordAge = ($PasswordPol).MaxPasswordAge
}
$expireson = $passwordsetdate.AddDays($maxPasswordAge)
$today = (Get-Date)
#Gets the count on how many days until the password expires and stores it in the $daystoexpire var
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
}
}
else
{
$daystoexpire = "N/A"
}
if (($User.Enabled -eq $True) -and ($Attvar.Lastlogon -lt ((Get-Date).AddDays(- $Days))) -and ($User.Lastlogon -ne $NULL))
{
$obj = [PSCustomObject]@{
'Name' = $User.Name
'UserPrincipalName' = $User.UserPrincipalName
'Enabled' = $Attvar.Enabled
'Protected from Deletion' = $User.ProtectedFromAccidentalDeletion
'Last logon' = $Attvar.lastlogon
'Password Never Expires' = $Attvar.PasswordNeverExpires
'Days Until Password Expires' = $daystoexpire
}
$userphaventloggedonrecentlytable.Add($obj)
}
我得到的错误如下:
Cannot convert argument "value",with value: "30.00:00:00",for "AddDays" to type "System.Double": "Cannot convert the "30.00:00:00" value of type
"System.TimeSpan" to type "System.Double"."
At C:\Users\t1-82042450\Desktop\PSHTML-AD.ps1:948 char:4
+ $expireson = $passwordsetdate.AddDays($maxPasswordAge)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [],MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
在此方面的任何帮助将不胜感激。谢谢。
解决方法
您需要提取时间跨度中的天数,这可以解决问题。
$passwordsetdate.AddDays(([timespan]$maxPasswordAge).totaldays)