需要在 powershell 中更新 New-Timespan cmdlet 的方法

问题描述 投票:0回答:1

我们正在使用从此处/spiceworks 的其他条目拼凑而成的 powershell 脚本。将其移至较新的服务器后,我们无法找到某些地方的差异,并且获取时间跨度行导致以下错误:

New-TimeSpan:无法绑定参数“End”。无法将“System.TimeSpan”类型的“60.00:00:00”值转换为“System.DateTime”类型。 在 C:\ExpirationScript\PasswordChangeNotification.ps1:61 字符:54

  • $daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
    
  •                                                  ~~~~~~~~~~
    
    • CategoryInfo:InvalidArgument:(:) [New-TimeSpan],ParameterBindingException
    • FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.NewTimeSpanCommand

尝试了在 stackoverflow 上找到的几种不同的想法来替换此变量,但没有成功:

 $daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days

有什么想法吗?预先感谢您。

使用的脚本:

# Please Configure the following variables....
$smtpServer="localhost"
$expireindays = 7
$from = "[email protected]"
$logging = "Enabled" # Set to Disabled to Disable Logging
$logFile = "c:\pathtoscripting\ADpasscheck.csv" # ie. c:\mylog.csv
$testing = "Disabled" # Set to Disabled to Email Users
$testRecipient = "[email protected]"
$nullRecipient = "[email protected]"
$date = Get-Date -format ddMMyyyy
$exemptUsers = "Administrator"
#
###################################################################################################################

# Check Logging Settings
if (($logging) -eq "Enabled")
{
    # Test Log File Path
    $logfilePath = (Test-Path $logFile)
    if (($logFilePath) -ne "True")
    {
        # Create CSV File and Headers
        New-Item $logfile -ItemType File
        Add-Content $logfile "Date,Name,EmailAddress,DaystoExpire,ExpiresOn"
    }
} # End Logging Check

# Get Users From AD who are Enabled, Passwords Expire and are Not Currently Expired #Enabled was missing from this line so disabled accounts were being found
Import-Module ActiveDirectory
$users = get-aduser -filter 'Enabled -eq $true' -properties Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, EmailAddress
$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge

# Process Each User for Password Expiry
foreach ($user in $users)
{
    If ($exemptUsers -contains $user.SamAccountName) { Continue; }
    $Name = (Get-ADUser $user | foreach { $_.Name})
    $emailaddress = $user.emailaddress
    $passwordSetDate = (get-aduser $user -properties * | foreach { $_.PasswordLastSet })
    $PasswordPol = (Get-AduserResultantPasswordPolicy $user)
    # Check for Fine Grained Password
    if (($PasswordPol) -ne $null)
    {
        $maxPasswordAge = ($PasswordPol).MaxPasswordAge
    }
 
    $expireson = $passwordsetdate + $maxPasswordAge
    $today = (get-date)
    $daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
       
    # Set Greeting based on Number of Days to Expiry.

    # Check Number of Days to Expiry
    $messageDays = $daystoexpire

    if (($messageDays) -ge "1")
    {
        $messageDays = "in " + "$daystoexpire" + " days."
    }
    else
    {
        $messageDays = "today."
    }

尝试重新设计线路以使用此处找到的其他想法(日期时间对象),但无法正确合并: powershell 中的新 TimeSpan cmdlet

powershell active-directory
1个回答
0
投票

更改这两行。

(Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
$expireson = $passwordsetdate + $maxPasswordAge

(Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
$expireson = $passwordsetdate.AddDays($maxPasswordAge)
© www.soinside.com 2019 - 2024. All rights reserved.