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"
EBGreen
  • 36,735
  • 12
  • 65
  • 85
  • 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 – Tony Hinkle May 21 '15 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 – Milton T. Machado S May 21 '15 at 15:43

3 Answers3

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.
user4003407
  • 21,204
  • 4
  • 50
  • 60
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.

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
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'"
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35