none
(Help Needed) Batch File: Copy a String of Piped Commands to Clipboard RRS feed

  • Question

  • 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

    Any and all help is greatly appreciated.

    • Edited by McGuinty Thursday, May 16, 2019 3:09 PM Removed batch script text (500+ lines)
    Thursday, May 16, 2019 1:44 PM

All replies

  • First thing, generally when you ask for help on the Internet, people want what is called a minimal reproducible example. i.e. a script that is as short as possible but still exhibits the error you want to have fixed. People will never read 500 lines of script to find the one line that is not working. In your case, such an example would've been as simple as.

    @echo off
    echo ls | ? { $_.Name -like "*.csv" }

    Second, your problem comes because | is a special character in batch scripting too, so it gets interpreted by the shell. You need to escape it by putting a ^ in front.

    @echo off
    echo ls ^| ? { $_.Name -like "*.csv" }
    Simply making that minimal example can occasionally already help you solve the problem yourself btw.
    • Edited by athmanb Thursday, May 16, 2019 2:11 PM
    Thursday, May 16, 2019 2:10 PM
  • Thank you athmanb. In hindsight I should have attached a link to the file or as you said just provided the snippet of the particular command. As I"m not well versed in scripting/coding, I wasn't aware of the ^ being an escape command.

    I escaped out the | characters, but this simply causes it to echo, as is expected. If the | for clip is left in it will error out noting a failure on the previous previous section of the command. However, if the entire echo is enclosed in double quotes, the clip board function works, but also includes the double quotes and ^. This will cause the command to fail in EXC Shell. Is there a way around this?



    • Edited by McGuinty Thursday, May 16, 2019 2:55 PM
    Thursday, May 16, 2019 2:55 PM
  • Combining your recommendation and some more digging through forums, I found what I was looking for. On top of escaping the | character, I also have to escape each sequence within the command, so for each | I actually require 3 ^ within the command. Below works for our needs.

    Thank you for pointing us in the right direction.

    Thursday, May 16, 2019 3:37 PM
  • That is absolutely no reason to use a batch file to do this.  Just write a PS script that does all of this.

    Please read the following carefully:

    How to ask questions in a technical forum

    An image of code is not helpful


    \_(ツ)_/

    Thursday, May 16, 2019 4:54 PM