Im trying to make a script to identify Distro groups that have not received any email in a specific time range (for this script it is 60 days) but I keep running in to issues with the startdate when I run the script
[color=#ff0000]"Write-ErrorMessage : Cannot process argument transformation on parameter 'StartDate'. Cannot convert value
"13/03/2024" to type "System.Nullable`1[System.DateTime]". Error: "String '13/03/2024' was not recognized as a valid
DateTime."
At C:\Users\A058327\AppData\Local\Temp\tmpEXO_yjkfgkua.jv3\tmpEXO_yjkfgkua.jv3.psm1:1191 char:13
+ Write-ErrorMessage $ErrorObject
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-MessageTrace], ParameterTransformationException
+ FullyQualifiedErrorId : [Server=SJ0PR19MB4796,RequestId=644c09f4-be7e-40f7-3e21-44baa56068c5,TimeStamp=Wed, 13 M
ar 2024 16:50:27 GMT],Write-ErrorMessage"[/color]
this is the script I am referring to:
# Connect to Office 365 Exchange Online
#$UserCredential = Get-Credential
#Connect-ExchangeOnline -Credential $UserCredential
# Define the date range (in this case, 60 days ago)
$StartDate = (Get-Date).AddDays(-60)
# Get all distribution groups
$Groups = Get-DistributionGroup -ResultSize Unlimited
# Initialize an array to store groups with no recent emails
$GroupsNoEmail = @()
# Iterate through each group
foreach ($Group in $Groups) {
# Check the last received email date for the group
$LastEmailDate = Get-MessageTrace -RecipientAddress $Group.PrimarySmtpAddress -StartDate $StartDate -EndDate (Get-Date) -PageSize 1 | Select-Object -ExpandProperty Received
if (!$LastEmailDate) {
# If no emails were received within the specified date range, add the group to the array
$GroupsNoEmail += $Group
}
}
# Output groups with no recent emails
if ($GroupsNoEmail.Count -gt 0) {
Write-Host "Distribution groups that have not received an email in the last 60 days:"
$GroupsNoEmail | Select-Object DisplayName, PrimarySmtpAddress
} else {
Write-Host "No distribution groups found that have not received an email in the last 60 days."
}
Any Ideas? Any help is appriciated.