1

There is a working batch file with PowerShell code in one line:

powershell -Command "& {$cont = wget http://САЙТ.info/; $cont = $cont.Content; $FilePath = 'contentFronHtml.txt' -f $env:SystemDrive; $cont | Out-File -FilePath $FilePath -Append -Width 200;}"

I want to break the PowerShell code to get something like:

SET LONG_COMMAND="$cont = wget http://САЙТ.info/;"
"$cont = $cont.Content;"
"$FilePath = 'contentFronHtml.txt' -f $env:SystemDrive;$cont | Out-File -
FilePath; $FilePath -Append -Width 200;"
powershell -Command "& {LONG_COMMAND}"

How do I connect strings of PowerShell code?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vlad74
  • 13
  • 5
  • String concatenation is done using + and for escape sequence you need to use baktick. – Ranadip Dutta Nov 07 '17 at 09:58
  • You can encode your script to base64 since powershell can run a base64 encoded script from the command prompt. – bluuf Nov 07 '17 at 11:24
  • I tried it did not work. you can use a concrete example – Vlad74 Nov 07 '17 at 11:36
  • 1
    base64 encode your script! That's a horrible idea, as an example as it changes `powershell -Command 'echo "Hello World"'` into `powershell.exe -encodedCommand ZQBjAGgAbwAgACIASABlAGwAbABvACAAVwBvAHIAbABkACIA` -- which is totally unreadable by a human! – henrycarteruk Nov 07 '17 at 11:40
  • See: https://stackoverflow.com/questions/36672784/convert-a-small-ps-script-into-a-long-line-in-a-batch-file – Aacini Nov 07 '17 at 12:11

3 Answers3

0

I would save your PowerShell code as a .ps1 file and then run it from the command line:

powershell -File "C:\folder\test.ps1"

Then to break long commands up over multiple lines, follow the answer from: How to enter a multi-line command

This means you're separating your PowerShell code from your batch code, and no longer have inline PowerShell code within a batch file which can get hard to read.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
0

Do something like this:

echo>%temp%\file.ps1 SET LONG_COMMAND="$cont = wget http://САЙТ.info/;"
echo>>%temp%\file.ps1 "$cont = $cont.Content;"
echo>>%temp%\file.ps1 "$FilePath = 'contentFronHtml.txt' -f
echo>>%temp%\file.ps1 $env:SystemDrive;$cont ^| Out-File -
echo>>%temp%\file.ps1 FilePath; $FilePath -Append -Width 200;"
echo>>%temp%\file.ps1 powershell -Command "& {LONG_COMMAND}"

powershell %temp%\file.ps1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wkup
  • 79
  • 1
  • 1
  • 8
0

This is how I would expect to do this sort of thing:

PowerShell -Command "first line up to the end of a command;"^
 "next line up to the end of a command;"^
 "another line up to the end of a command;"^
 "last line"

You could also try this idea too:

Set "MyCmnd=first line up to the end of a command;"
Set "MyCmnd=%MyCmnd% next line up to the end of a command;"
Set "MyCmnd=%MyCmnd% another line up to the end of a command;"
Set "MyCmnd=%MyCmnd% last line"
Echo %MyCmnd%
Compo
  • 36,585
  • 5
  • 27
  • 39