0

I need print on Powershell a line comand through a variable , I called $em_result, for example, em_result=20, Thanks.

$em_result = ´gc C:\Users\mtmachadost\Desktop\Test\logError.txt | ?{$_ -match 'cajica11'} | %{($_ -split "\s+")[3]} |  Measure -Sum | Select -Exp Sum'´
Write-Host"$em_result"
2
  • f you just run the command gc C:\Users\mtmachadost\Desktop\Test\logError.txt | ?{$_ -match 'cajica11'} | %{($_ -split "\s+")[3]} | Measure -Sum | Select -Exp Sum' does that output anything? If not, then $em_result will not have anything, and you need to change the question to reflect that you need help with the command Commented May 21, 2015 at 15:41
  • yes, the result is a number, for example 58, but, I need, save the line comand, in a varible, and print the variable result through em_result em_result=58 Commented May 21, 2015 at 15:43

3 Answers 3

2

If you want to save command line to variable, I would recommend to save it as ScriptBlock rather as String:

$em_result = {gc C:\Users\mtmachadost\Desktop\Test\logError.txt | ?{$_ -match 'cajica11'} | %{($_ -split "\s+")[3]} |  Measure -Sum | Select -Exp Sum'}
Write-Host "`$em_result = $(&$em_result)"

This way you:

  1. does not have to escape things.
  2. can convert it to string.
  3. can invoke it by invoke operator (& or .).
  4. have syntax highlighting when you edit it in ISE.
  5. any syntax errors get caught when it parsed, not when it executed.
  6. ScriptBlock linked to its file and line, so you can set breakpoints in it.
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. This would be preferential over using a string. Forgot about that.
1

While I am not sure of the motivation for what you are trying to accomplish it sounds like you are trying to save the command $em_result so that you can run when you want. So that way, you are not saving the point in time result but rather every time you call it the result will be from that time.

Like Tony Hinkle answered you need to save the command as a string. However there is more to escape than just the quotes. The pipeline variable $_ would also come into play. As it stands a simple here-string would make it so you don't have to worry about escaping anything.

$em_result = @'
gc 'C:\Users\mtmachadost\Desktop\Test\logError.txt' | ?{$_ -match 'cajica11'} | %{($_ -split "\s+")[3]} |  Measure -Sum | Select -Exp Sum
'@

Now you could call this string and get the results

Write-Host "`$em_result = $(Invoke-Expression $em_result)"

I guess you were trying to use the backtick pair like and escape quote pair which made me think this is what you wanted. Backtick will only escape the one following character. Invoke-Expression will execute the string we pass it as code.

1 Comment

Thanks--I add the escapes to $ in my answer--just didn't go through it closely enough.
0

When assigning it, you need to specify that it is a string, or else Powershell will try to execute it. You also need to delimit it with double quotations, and escape the double quotation marks and dollar signs in the command with a backtick so that they are considered part of the string, not a delimiter to end the string.

$em_result = [string]"gc C:\Users\mtmachadost\Desktop\Test\logError.txt | ?{`$_ -match 'cajica11'} | %{(`$_ -split `"\s+`")[3]} |  Measure -Sum | Select -Exp Sum'"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.