I'm attempting to prompt user input to search for specific emails using a piped command like this:
get-mailbox -ResultSize unlimited | search-mailbox -searchquery 'subject:"*$Subject*"' -LogOnly -Loglevel Full -TargetMailbox "$Self" -TargetFolder "$Folder"}
When I perform a search like this, the subject is never changed to what $Subject is even if I manually input it. I'm using a variable set like $Subject = Read-Host "Type in the email subject"
Essentially what I'm trying to accomplish is have a technician choose what fields to search for an email with. Sender/Date/To/From etc. By choosing an option, it will perform the code in that section. The most important field is the Subject. But the subject never populates to what the variable is.
If I type the whole search string without variables it works perfectly.
What am I missing? Thanks!
To expand the $Subject variable, construct the query first or inside parantheses:
$Subject="Good Morning"
$SearchQuery = 'Subject:'+'"*'+$subject+'*"'
Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery $SearchQuery [...]
Related
Trying to use PowerShell to search O365 mailbox to get all messages where subject line doesn't contain specific string, specifically a square bracket [
The command below works fine in principal to find all messages where subject contains a specific word, but can't get it to work to find all messages where subject DOES NOT contain a specific word (or in this case character [)
Search-Mailbox -Identity "fred#email.com" -SearchQuery 'Subject:"*" -[' -EstimateResultOnly
The eventual aim is to scan a mailbox to remove all content other than messages which contain square brackets in the subject line, so will use -DeleteContent switch or similar, for now just using -EstimateResultOnly or -LogOnly -LogLevel Full and -TargetFolder -TargetMailbox to see a list of what it finds
What am I missing - all comments appreciated.
Thanks
Use a -filter with -notlike should work.
I have a requirement to search mailboxes for an email based on subject line and report if the email is in the Inbox or another folder. The below code works well for individual users.
Search-Mailbox -Identity "test#example.com" -SearchQuery ‘Subject:”Suspect email alert”‘ -TargetMailbox “admin” -TargetFolder “inbox”-LogOnly -LogLevel Full
The report received states if the email is in that particular mailbox and the location. This is great for one user, though I need to do this for over 500 users.
I have a csv file with a list of email addresses I need to check for this particular email and it's location. So I have imported the csv file to a variable. Then put the command in a foreach loop, though cannot get this working correctly.
Correction: updated to txt file a used get-content
$users = Get-Content -Path userslist.txt
foreach ($user in $users) {
Search-Mailbox $user -SearchQuery ‘Subject:”Suspect email alert”‘ -TargetMailbox “admin” -TargetFolder “inbox”-LogOnly -LogLevel Full }
How do I run through the list of users, so I can get one report rather than a report for each user individually. Any ideas would be great.
Issue was with the csv file formatting. imported text file and loop running fine. Would be good to get the results into one Search results file, rather than one for each loop.
Okay, so this seems like a silly question, but I can't seem to find anything about it. I've checked in the AQS and haven't seen anything about it yet either. So I'm trying to conduct a search on the subject line of all mailboxes in my organization with a query like this:
foreach ($db in $mdblist) {Get-Mailbox -Database $db -ResultSize Unlimited |
Search-Mailbox -SearchQuery 'subject:"Monthly Expences Report" AND attachment:"Report_######.doc"' -LogOnly -LogLevel FULL -TargetMailbox joseph.c.larrew -TargetFolder Searches\1003333 |
Both the subject parameter and attachment parameter are literal strings and I did mean to spell "expences" that way.
I can do a search for just the subject and a separate search for the attachment and both give positive results, but when I "AND" them, no results. I've done a search against a specific mailbox that I know should come up with a positive and still no dice. Any thoughts?
Is there a plugin or tool that will let me display a list of objects to a user (Format-Table style) and allow them to use the cursor to select a choice from the list, including potentially scrolling a long list? I would like to be able to do something like this:
Get-User -anr $search |Get-Choice| Set-User -EnableAccount true
This script should display a list of matching accounts at a console prompt, allow the user to scroll up and down the list interactively, and select a choice by pressing Enter (or pass null if the user hits escape). Only one account would be passed to Set-User, rather than a list of all matching choices.
Obviously details could differ. While I would prefer a console version, a graphical one would be acceptable (that popped up a Windows dialog). The exact keystrokes could be different. But the core goal (accepting a list, getting user input, piping out the result) should be met.
in v3:
Get-User -anr $search | Out-GridView -PassThru | Set-User -EnableAccount true
Take a look to Out-Form
pseudo use:
out-form -title "Enable Account" -data (Get-user -anr $search) -columnNames ("AccountName") `
-columnProperties ("SamAccountName") -actions #{Enable It!" = { $_ | Set-User -EnableAccount true}}
I wonder if you can help me. I am trying to extract a list of recipients that have sent emails to themselves (from domain1 to domain2) using get-messagetrackinglog tool within Exchange. My little script seems to work fine with the sender's part, however after a few days of pondering I am unable to make it work with recipients. I suspect that this is because there can be many recipients (as opposed to only one sender) for each email and this requires an array to be assigned to the recipients variable however being new to powershell I am not entirely sure how to do it and how to pipe it through. The logic is the following:
a) I want to use split command to segregate all senders (using space delimiter) and fetch them into an array,
b) then to detach both sender's and recipients' first parts of addresses from domains using # delimiter and
c) compare their first part of email addresses that I've got (values returned from adam#domain1 and adam#domain2).
d) If they match - I need email message results to be exported to .csv.
I have got this far:
Get-MessageTrackingLog -server "pdnaex1" -EventID "SEND" -start "01/10/2010 00:00:00" -end "31/10/2010 23:59:59" -resultsize Unlimited | Where {[string]$.sender.split("#")[0] -like [string]$.recipients.split("#")[0]} | Select timestamp,#{Name="Sender";Expression={$.sender}},#{Name="Recipients";Expression={$.recipients}},messagesubject | export-csv X:\XXX.csv
I know above is incorrect but hope I make my question clear.
Any help is greatly appreciated. I suspect my script fails because I fail to populate an array of recipients and compare sender's value against each entry within that array but I cant work out how to do that.
You can use -like to compare an array to a scalar, but you cannot use it to compare a scalar to an array. The array argument has to come first.
PS C:> $a = "a","b","c"
PS C:> $b = "b"
PS C:> $a -like $b
b
PS C:> $b -like $a
False
You can get an arry by casting $_.recipients as [string] and splitting it on the #, but you can't select just the user part from that array easily.
Additionally, you need to reference the senders and recipients as $.sender and $.recipients, rather than $sender and $recipients.
Partially tested:
Get-MessageTrackingLog -server "pdnaex1" -EventID "SEND" -start "01/10/2010 00:00:00" -end "31/10/2010 23:59:59" -resultsize Unlimited | Where {($.recipients |% {$.split("#")[0]}) -like $_.sender.split("#")[0]} | Select timestamp,#{Name="Sender";Expression={$.sender}},#{Name="Recipients";Expression={$.recipients}},messagesubject | export-csv X:\XXX.csv
Note: the underscores after the $ don't seem to be showing up in the post in the answer.