无法使用 Powershell 在脚本中确定密码使用期限

Unable to determine password age with in a script using Powershell

早上好

我在尝试确定 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)