无法使用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)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...