none
Powershell, quoting the quoting quotes - storing large string RRS feed

  • Question

  • I am using an Orchestrator run book to read and email and pass the body to a PS script.

    The string being passed is large and I cant get it to store in a variable because there are quotes AND an apostrophe in the string.

    Its something like this:

    Some text "User's DB" some more text

    I need to store this in a variable so I can use the Select-String command to get a line.  However, I keep getting the Unexpected token 'User' error.  I know this has to do with quoting stuff, but I have tried everything.

    The script is kinda like this:

    $a = <Body from Read Email Activity> < I cant get the body to store in $a because of the quotes.

    $a | Select-String "String:" -quiet

    Thanks

    AS



    • Edited by JL-4 Friday, November 16, 2012 2:58 PM
    Friday, November 16, 2012 2:53 PM

Answers

  • $a = @'
    Alert: Some text about the alert
    Description: Package "User DB DBU's" failed. <---  Problem area
    DM Status: 1-AP
    AlertID: 3784yejbf-354f-f34-4f1234ffvw2v 
    Server: SERVERNAME
    '@

    In Orchestrator,

    $a = @'
    {PublishedData}
    '@


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, November 16, 2012 4:02 PM
    Moderator

All replies

  • In any PowerShell string you can always escape any character with the backtick, `, character. You can replace embedded " characters with `" and ' with `' (if required). Does this help?


    Richard Mueller - MVP Directory Services

    Friday, November 16, 2012 3:10 PM
    Moderator
  • I'd try using a here-string:

    $a = @"
    $(select-string.....)
    "@

    Within the @" -"@ delimiters, variables and subexpressions get expanded, but normal quoting rules aren't applied, so the quotes and backticks should get interpreted literally.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, November 16, 2012 3:16 PM
    Moderator
  • Here is what is passed from the Orchestrator as text, its no quoted or anything and is passed as one string

    Alert: Some text about the alert

    Description: Package "User DB DBU's" failed. <---  Problem area

    DM Status: 1-AP

    AlertID: 3784yejbf-354f-f34-4f1234ffvw2v

    Server: SERVERNAME

    I want to store all of the above in a variable, lets say $a.

    I am not sure how to implement your suggestions on this.

    Thanks

    AS


    • Edited by JL-4 Friday, November 16, 2012 3:51 PM
    Friday, November 16, 2012 3:51 PM
  • In any PowerShell string you can always escape any character with the backtick, `, character. You can replace embedded " characters with `" and ' with `' (if required). Does this help?


    Richard Mueller - MVP Directory Services


    Since all these messages are different and I dont know whether or not there are quotes or apostrphies in the body of the email, how would I tell it to escape the " and the ' in passed text??
    Friday, November 16, 2012 4:01 PM
  • $a = @'
    Alert: Some text about the alert
    Description: Package "User DB DBU's" failed. <---  Problem area
    DM Status: 1-AP
    AlertID: 3784yejbf-354f-f34-4f1234ffvw2v 
    Server: SERVERNAME
    '@

    In Orchestrator,

    $a = @'
    {PublishedData}
    '@


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, November 16, 2012 4:02 PM
    Moderator
  • Ok, now we are getting somewhere.  Thank you, however the output is the entire body. Here is the code I have right now

    $b = @'

    {Body from Monitor Mailbox}

    '@

    $alert = $b | Select-String "AlertID:"

    When I do a get-content on a text file that has the same text as the body of the email and then send that to Select-String "AlertID:" it outputs the exact line I need.  But I am not sure it behaves the same.  Do I need to put my Select-String inside the @' '@??

    Thanks

    AS

    Friday, November 16, 2012 4:26 PM
  • Ok, now we are getting somewhere.  Thank you, however the output is the entire body. Here is the code I have right now

    $b = @'

    {Body from Monitor Mailbox}

    '@

    $alert = $b | Select-String "AlertID:"

    When I do a get-content on a text file that has the same text as the body of the email and then send that to Select-String "AlertID:" it outputs the exact line I need.  But I am not sure it behaves the same.  Do I need to put my Select-String inside the @' '@??

    Thanks

    AS

    Actually, your solution gave me what I needed.  I was able to integrate that in with my current script using .Replace and .Substring to get the alertID.  I just sort of wanted to get rid of those 2 statements.  But it works

    Thanks!!

    Friday, November 16, 2012 4:35 PM
  • I wouldn't use select-string for that.  Just use a -match with a multi-line regex to extract the parts of message body you want.

     

    http://mjolinor.wordpress.com/2012/01/05/powershell-multiline-regex-matching/


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, November 16, 2012 4:39 PM
    Moderator
  • @' '@ worked.  I ended up re-writing the entire script from scratch.  The logic wasnt working too well, but now is running fine.

    Thanks

    AS

    Monday, November 19, 2012 2:18 PM
  • That Orchestrator data bus will make you very good at passing strings between script blocks after awhile.

     

      Another useful trick is to create hash tables as key-value pairs in a here-string, then put that onto the data bus and use convertfrom-stringdata on the other end to get it back to a PS hash table.  If you use an expandable here-string (@" - "@ instead of @'-'@) you can use local variables in the hash table and they'll get expanded before the string gets published.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Monday, November 19, 2012 2:42 PM
    Moderator