Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
BCM
User Panel

Site Notices
Posted: 3/13/2024 12:54:47 PM EDT
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.
Link Posted: 3/14/2024 8:44:38 AM EDT
[#1]
Nobody?
Link Posted: 3/14/2024 9:19:42 AM EDT
[#2]
It's how you're declaring the StartDate parameter, I think. It's expecting a DateTime param but yours is a string.

Try like this:

Friday, January 16, 2015 7:29:00 PM It is also possible to convert a string into a DateTime object by using the [dateTime] type accelerator. I first create a specific date and time, and then I call the AddHours method:

PS C:> $dte = Get-Date

PS C:> $dte.AddHours(3)

From this MS blog: https://devblogs.microsoft.com/scripting/adding-and-subtracting-dates-with-powershell/
Close Join Our Mail List to Stay Up To Date! Win a FREE Membership!

Sign up for the ARFCOM weekly newsletter and be entered to win a free ARFCOM membership. One new winner* is announced every week!

You will receive an email every Friday morning featuring the latest chatter from the hottest topics, breaking news surrounding legislation, as well as exclusive deals only available to ARFCOM email subscribers.


By signing up you agree to our User Agreement. *Must have a registered ARFCOM account to win.
Top Top