i tried to create a tool for my company to send an email to our IT when a user account is expired.
This part works fine so far.
But we have an issue. Sometimes we change the expiration date of the accounts. The problem is that i saved the names into a txt file because we run that script every night to check for new expired accounts. If that txt wouldn´t exist, then we would get hundrets of emails every day.
I tried multiple convert things like "[Datetime] $UserAccountsExpiredDate -ge $TodaysDate" But there is always an error message like this:
"@{AccountExpirationDate=13.11.2022 00:00:00}" vom Typ "Selected.Microsoft.ActiveDirectory.Management.ADUser" kann nicht in den Typ "System.DateTime" konvertiert
werden."
In C:\Scripte\AbgelaufeneUserSkript\Abfrage_abgelaufene_User_NicoTest_Teiltest.ps1:38 Zeichen:9
if(([Datetime] $userAccountExpiredDate -ge $TodaysDate))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [], RuntimeException
FullyQualifiedErrorId : InvalidCastConstructorException
I know that many of you cant speak german. The AccountExpirationDate can´t be converted into System-Datetime.
I couldn´t find any solution, so im here right now. My original code is below this:
$Users = Get-ADUser -Filter * -Properties Name, Enabled, AccountExpirationDate, LastLogonDate, department | ? {($_.AccountExpirationDate -NE $NULL -AND $_.AccountExpirationDate -LT (Get-Date)) }
$TxtPath = "C:\scripte\AbgelaufeneUserSkript\Abgelaufene_Benutzer.txt"
$UserDeactivated = Get-Content $TxtPath
$TodaysDate = (Get-Date).ToString()
if(Test-Path $TxtPath)
{
}
else
{
New-Item $TxtPath
}
echo $TodaysDate
foreach($userintxt in Get-Content $TxtPath)
{
Write-Host $userintxt
$userAccountExpiredDate = (Get-ADUser $userintxt -Properties AccountExpirationDate) | Select-Object AccountExpirationDate
Write-Host $userAccountExpiredDate
echo $userintxt.Name
if(($userAccountExpiredDate -ge $TodaysDate))
{
Write-Host $userintxt.Name "This account is no more expired"
}
else
{
Write-Host $userintxt.Name "This account is still expired"
}
}
CodePudding user response:
You can use [Datetime]::ParseExact
to convert the string to a date time:
if for example your string date format is: 13.11.2022 00:00:00
You Can Parse it like this:
$Date = [datetime]::ParseExact("13.11.2022 00:00:00","dd.MM.yyyy HH:mm:ss",$null)
Change the format as needed, see more information: Date Format and DateTime.ParseExact
CodePudding user response:
Since AccountExpirationDate
is already a DateTime object (LDAP property accountExpires
, converted to local time), you can compare it to another DateTime object directly.
Therefore, set $TodaysDate
to a real DateTime object:
# do not stringify it, but instead take the date as DateTime object set to midnight
$TodaysDate = (Get-Date).Date
Then your code only needs to Expand property AccountExpirationDate using
$userAccountExpiredDate = (Get-ADUser $userintxt -Properties AccountExpirationDate).AccountExpirationDate
or
$userAccountExpiredDate = Get-ADUser $userintxt -Properties AccountExpirationDate | Select-Object -ExpandProperty AccountExpirationDate
After that,
if ($userAccountExpiredDate -ge $TodaysDate) {..}
should work just fine.