Solved

Powershell Help

Posted on 2016-09-28
2
28 Views
Last Modified: 2016-10-18
Hi

The purpose of this script is to scan every email from the past 24 hours that has hit email xxxx@domain.com
The subject has a username in pond or hash (#xxxx#) for example. RE: account notification for #charlesb# expiring
I have found a logic to extract the text between the pond/hash, but unfortunately it only does it for the first result and does not continue to the next. Please advise

##date minus 24hours
$startdate=("{0:dd/MM/yyyy hh:mm:ss }" -f (get-date).AddDays(-1))
##todays date
$enddate=get-date -Format G


Get-TransportServer -Identity 'hubserv' |Get-Messagetrackinglog -Recipients: xxxx@domain.com -EventID “RECEIVE” -Start ”$startdate” -End ”$enddate” |select MessageSubject, Sender | export-csv C:\Charlie\email.csv -NoTypeInformation

$messagesubjects=import-csv C:\Charlie\email.csv | Out-String

foreach($messagesubject in $messagesubjects) {

$start= $messagesubject.indexof("#") +1

$end= $messagesubject.indexof("#", $start)
$length =$end - $start
$messagesubjects.substring($start, $length) | out-file C:\Charlie\usertest.txt -Append

}
0
Comment
Question by:RMDOps
2 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points (awarded by participants)
ID: 41821196
In your last line, use $messagesubject instead of $messagesubjects.
Of course you are aware that the steps you take to get the result are long-winded and cumbersome?
0

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now