Solved

Powershell Help

Posted on 2016-09-28
2
29 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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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…

863 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

19 Experts available now in Live!

Get 1:1 Help Now