I'm sure this is an easy one, but it's got the better of me this afternoon (I'm relatively new to Powershell). Also, I'm fairy sure my code could be more elegant.
To start with, I've got some code that goes to our exchange servers, and returns sent messages from a particular user. It puts this into an array called @Results. So, a few entries will look like this (edited for privacy):
C:\Windows\system32>$Results[3..5]
Timestamp MessageSubject Recipients
--------- -------------- ----------
01/09/2013 09:12:57 Subject1 {[email protected]}
01/09/2013 09:30:44 Subject2 {[email protected]}
01/09/2013 09:42:11 Subject3 {[email protected], recipient2@gmail...
So, I'm pretty happy with this. However, my issue is with the multiple recipient lists above. Effectively, this is its own array. From the list of sent mail, I'm not interested in internal mail, so I'd like to exclude entries with our domain on them (let's say it's globocorp.com above). So, my solution was to have a couple of loops to list out all the entries as their own lines. This is where I've got to:
For ($k=0; $k -eq $Results.count; $k++)
{
For ($j=0; $j -eq $Results[$k].Recipients.Count; $j++)
{write-host $Results[$k].timestamp, $Results[$k].Recipients[$j], $Results[$k].MessageSubject
}
}
This works, but uses a couple of loops which I think is inelegant. There must be a tidier way of achieving the above (maybe the for-each command?). My main issue is, however, what to do with the output. I don't want to use 'write-host' and would rather create the outputs as objects which I can then tabularise or create a csv from it. I can then use the 'where-object ToUpper(Recipients) -notlike "*@GLOBOCORP.COM"'function to exclude internal mail.
However, I can't just list the output, then pipe it as I get the 'empty pipe element not allowed' error, and listing out the objects sticks each entry on a new line, i.e.:
04 September 2013 18:19:12
[email protected]
Message subject
Any advice greatly appreciated!