Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

How run cmd script from powershel using variable with spaces.

$
0
0

I am trying to run cmd script from powershell using variables.

If variable contains no spaces script works well, what should I do?

My script (c:\scripts\script.cmd):

C:\scripts\sed\sed.exe -T -n -e "/%1/p" c:\test.txt > C:\scripts\test2.txt

C:\scripts\sed\sed.exe -T -n -e "/%2/p" c:\scripts\test2.txt > C:\scripts\output.txt

My powershell script (c:\scripts\test.ps1):

$search1 = 'Exception:'

$search2 = 'IOException while'

& C:\scripts\script.cmd $search1 $search2

Then I get message:

PS C:\Windows\System32\WindowsPowerShell\v1.0> C:\Scripts\test.ps1

C:\Windows\System32\WindowsPowerShell\v1.0>C:\scripts\sed\sed.exe -T -n -e "/Exception:/p" c:\scripts\test.txt  1>C:\scripts\test2.txt 

C:\Windows\System32\WindowsPowerShell\v1.0>C:\scripts\sed\sed.exe -T -n -e "/"IOException while"/p" c:\scripts\test2.txt  1>C:\scripts\output.txt 

script.cmd : C:\scripts\sed\sed.exe: -e expression #1, char 12: unterminated address regex

At C:\Scripts\test.ps1:3 char:2

+ & <<<<  C:\Scripts\script.cmd $search1 $search2

    + CategoryInfo          : NotSpecified: (C:\scripts\sed\...d address regex:String) [], RemoteException

    + FullyQualifiedErrorId : NativeCommandError

Why when is space in variable ($search2 = 'IOException while') in cmd script there is a " before and after variable and command look like this:

C:\Windows\System32\WindowsPowerShell\v1.0>C:\scripts\sed\sed.exe -T -n -e "/"IOException while"/p" c:\scripts\test2.txt  1>C:\scripts\output.txt 

instead of this:

C:\Windows\System32\WindowsPowerShell\v1.0>C:\scripts\sed\sed.exe -T -n -e "/IOException while/p" c:\scripts\test2.txt  1>C:\scripts\output.txt 

How can I change the variable declaration to get needed result ?


Viewing all articles
Browse latest Browse all 6937

Trending Articles