Hello,
I'm working on a batch script to assist out Help Desk with frequent needs to remove phishing emails from different client environment Exchange environments, both On-Prem and O365. The batch simply compiles the user input to generate the needed EXC Shell
commands and the only piece I'm having an issue with is getting it to copy to the clipboard. When I attempt to run the command it actually references one of the commands I'm attempting to send to clipboard noting it is an unknown command. So, it appears to
be trying to run the command rather than send it to the clipboard.
I believe this issue is related to | characters being part of the generated text needing to be copied to the clipboard. I've found a few articles noting how to copy to clipboard from batch, but nothing that details how to do so with the | character or limitations based on it.
We run an EstimateOnly command to identify how many phishing messages exist in the mail system from the sender, subject, or sender and subject and then we do a DeleteContent command afterward. All of this is sent to a CSV so it can be logged with the supporting ticket.
For a TL;DR version, we're looking to be able to copy the following commands to clipboard, individually so they can be pasted into the EXC Shell and executed. If the "ECHO "commands" | clip" setup isn't correct, can someone point us in the right direction?
ECHO Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery from:'%MailAddress%','subject:"%MailSub%"' -EstimateResultOnly | Export-csv "C:\TEMP\Phishing_EstimateResult_Sender_And_Subject.csv" -NoType | clip
ECHO Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery from:'%MailAddress%','subject:"%MailSub%"' -DeleteContent -Force | Export-csv "C:\TEMP\Phishing_DeleteContent_Sender_And_Subject.csv" -NoType | clip
Below is the full script. Any and all help is greatly appreciated.
=============================================
@ECHO OFFTITLE EXC Mailbox Phishing Messages Cleanup
Color 0C
:START
cls
color 0C
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO This script should only ever be ran by a member of the Network Services Team.
ECHO.
ECHO.
ECHO Before beginning, make sure your account has the Mailbox Import/Export Role
ECHOas well as the Mailbox Search Role. Once confirmed all work is done through EXC Shell
ECHOonce the command is generated through this batch file.
ECHO.
ECHO.
ECHO New-ManagementRoleAssignment -Role "Mailbox Import Export" -User "<user name or alias>"
ECHO.
ECHO New-ManagementRoleAssignment -Role "Mailbox Search" -User "<user name or alias>"
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
pause
cls
REM ===========================================================================================================
REM
REMJob Type Selection
REM
REM ===========================================================================================================
:JobType
color 0A
IF NOT EXIST C:\TEMP mkdir C:\TEMP
cls
ECHO Is this an EstimateOnly or DeleteContent job?
ECHO.
ECHO 1. EstimateOnly
ECHO 2. DeleteContent
ECHO.
set /p JobTypes=Type option:
if 'JobTypes'=='1' goto :EstimateOnly
if 'JobTypes'=='2' goto :DeleteContent
if 'JobTypes'=='' goto :Fault
goto :Fault
REM ===========================================================================================================
REM
REMJob Type: EstimateOnly
REM
REM ===========================================================================================================
:EstimateOnly
color 0A
cls
REM ===========================================================================================================
REM
REMSearch Type: Selection
REM
REM ===========================================================================================================
ECHO How do you want to search?
ECHO.
ECHO 1. Search by Sender
ECHO 2. Search by Subject
ECHO 3. Search by Sender and Subject
ECHO.
set /p SearchType=Type option:
if 'SearchType'=='1' goto :EstType1
if 'SearchType'=='2' goto :EstType2
if 'SearchType'=='3' goto :EstType3
if 'SearchType'=='' goto :Fault
goto :Fault
REM ===========================================================================================================
REM
REMEstimateOnly Search by Sender
REM
REM ===========================================================================================================
:EstType1
cls
ECHO What is the Sender's email?
ECHO.
set /p MailAddress=Sender email address:
cls
ECHO Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery from:'%MailAddress%' -EstimateResultOnly | Export-csv "C:\TEMP\Phishing_EstimateResult_SenderOnly.csv" -NoType | clip
cls
ECHO Command send to clipboard for use in EXC Shell. Would you like to start a new job?
ECHO.
ECHO 1. YES
ECHO 2. NO
ECHO.
set /p NewJob=Type option:
if 'NewJob'=='Y' goto :START
if 'NewJob'=='N' goto :EOF
if 'NewJob'=='' goto :Fault
goto :Fault
REM ===========================================================================================================
REM
REMEstimateOnly Search by Subject
REM
REM ===========================================================================================================
:EstType2
cls
ECHO What is the Subject of the email?
ECHO.
set /p MailSub=Subject of email:
cls
ECHO Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery 'subject:"%MailSub%"' -EstimateResultOnly | Export-csv "C:\TEMP\Phishing_EstimateResult_SubjectOnly.csv" -NoType | clip
cls
ECHO Command send to clipboard for use in EXC Shell. Would you like to start a new job?
ECHO.
ECHO 1. YES
ECHO 2. NO
ECHO.
set /p NewJob=Type option:
if 'NewJob'=='Y' goto :START
if 'NewJob'=='N' goto :EOF
if 'NewJob'=='' goto :Fault
goto :Fault
REM ===========================================================================================================
REM
REMEstimateOnly Search by Sender and Subject
REM
REM ===========================================================================================================
:EstType3
cls
ECHO What is the Sender's email?
ECHO.
set /p MailAddress=Sender email address:
cls
ECHO What is the Subject of the email?
ECHO.
set /p MailSub=Subject of email:
cls
ECHO Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery from:'%MailAddress%','subject:"%MailSub%"' -EstimateResultOnly | Export-csv "C:\TEMP\Phishing_EstimateResult_Sender_And_Subject.csv" -NoType | clip
cls
ECHO Command send to clipboard for use in EXC Shell. Would you like to start a new job?
ECHO.
ECHO 1. YES
ECHO 2. NO
ECHO.
set /p NewJob=Type option:
if 'NewJob'=='Y' goto :START
if 'NewJob'=='N' goto :EOF
if 'NewJob'=='' goto :Fault
goto :Fault
REM ===========================================================================================================
REM
REMJob Type: DeleteContent
REM
REM ===========================================================================================================
:DeleteContent
color 0A
cls
REM ===========================================================================================================
REM
REMSearch Type Selection
REM
REM ===========================================================================================================
ECHO How do you want to search?
ECHO.
ECHO 1. Search by Sender
ECHO 2. Search by Subject
ECHO 3. Search by Sender and Subject
ECHO.
set /p SearchType=Type option:
if 'SearchType'=='1' goto :DelConType1
if 'SearchType'=='2' goto :DelConType2
if 'SearchType'=='3' goto :DelConType3
if 'SearchType'=='' goto :Fault
goto :Fault
REM ===========================================================================================================
REM
REMDeleteContent Search by Sender
REM
REM ===========================================================================================================
:DelConType1
cls
ECHO What is the Sender's email?
ECHO.
set /p MailAddress=Sender email address:
cls
ECHO Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery from:'%MailAddress%' -DeleteContent -Force | Export-csv "C:\TEMP\Phishing_DeleteContent_SenderOnly.csv" -NoType | clip
cls
ECHO Command send to clipboard for use in EXC Shell. Would you like to start a new job?
ECHO.
ECHO 1. YES
ECHO 2. NO
ECHO.
set /p NewJob=Type option:
if 'NewJob'=='Y' goto :START
if 'NewJob'=='N' goto :EOF
if 'NewJob'=='' goto :Fault
goto :Fault
REM ===========================================================================================================
REM
REMDeleteContent Search by Subject
REM
REM ===========================================================================================================
:DelConType2
cls
ECHO What is the Subject of the email?
ECHO.
set /p MailSub=Subject of email:
cls
ECHO Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery 'subject:"%MailSub%"' -DeleteContent -Force | Export-csv "C:\TEMP\Phishing_DeleteContent_SubjectOnly.csv" -NoType | clip
cls
ECHO Command send to clipboard for use in EXC Shell. Would you like to start a new job?
ECHO.
ECHO 1. YES
ECHO 2. NO
ECHO.
set /p NewJob=Type option:
if 'NewJob'=='Y' goto :START
if 'NewJob'=='N' goto :EOF
if 'NewJob'=='' goto :Fault
goto :Fault
REM ===========================================================================================================
REM
REMDeleteContent Search by Sender and Subject
REM
REM ===========================================================================================================
:DelConType3
cls
ECHO What is the Sender's email?
ECHO.
set /p MailAddress=Sender email address:
cls
ECHO What is the Subject of the email?
ECHO.
set /p MailSub=Subject of email:
cls
ECHO Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery from:'%MailAddress%','subject:"%MailSub%"' -DeleteContent -Force | Export-csv "C:\TEMP\Phishing_DeleteContent_Sender_And_Subject.csv" -NoType | clip
cls
ECHO Command send to clipboard for use in EXC Shell. Would you like to start a new job?
ECHO.
ECHO 1. YES
ECHO 2. NO
ECHO.
set /p NewJob=Type option:
if 'NewJob'=='Y' goto :START
if 'NewJob'=='N' goto :EOF
if 'NewJob'=='' goto :Fault
goto :Fault
REM ===========================================================================================================
REM
REMSelection Fault Warning
REM
REM ===========================================================================================================
:Fault
cls
color CF
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO Invalid selection, please try again.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
pause
goto :START
:EOF
=======================================
Thanks again for any help you guys can provide.