0

Hi i am trying to download a text file using Powershell Invoke-WebRequest method

Invoke-WebRequest http://www.example.org/script.txt -Outfile DL_script.txt

content of script.txt:

cls
@echo off
 ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
  ECHO args = "ELEV " >> "%vbsGetPrivileges%"
  ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
  ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
  ECHO Next >> "%vbsGetPrivileges%"

  if '%cmdInvoke%'=='1' goto InvokeCmd 

  ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
  goto ExecElevation

but after downloading.. file looks like this (All Code in Single line, no new line) how can i download file in real format so i can run script

cls@echo offECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"ECHO args = "ELEV " >> "%vbsGetPrivileges%"ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"ECHO Next >> "%vbsGetPrivileges%" if '%cmdInvoke%'=='1' goto InvokeCmd  ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
goto ExecElevation

i need a single line command for download script in real format. tried with these methods but same output (everything in single line)

# Method 1:
Invoke-WebRequest $url -OutFile $path_to_file

# Method 2:
(New-Object System.Net.WebClient).DownloadFile($url, $path_to_file)

# Method 3 (requires BITS may not be immediate):
Import-Module BitsTransfer
Start-BitsTransfer -Source $url -Destination $path_to_file
1
  • You might have to accept what you want isn’t possible in a single line. Is there a reason it needs to be in a single line?
    – Ramhound
    Feb 24, 2021 at 14:05

1 Answer 1

1

As for this...

i need a single line command for download script

... ditto to what Ramhound stated, but in PowerShell, there is a difference between a one-liner (pipelined) command...

SendCommand1 | ReceiveCommand...

...,and commands in the pipe must support pipelined input, then there is all code on one line.

SomeCodedCommand;SomeOtherCodedCommand

The semi-colon means each block is independent of the other.

Chapter 4 - One-liners and the pipeline

What’s a PowerShell One-Liner & NOT a PowerShell One-Liner?

How your file is rendered on the download can be related to who the source delivers it, not necessarily something to do with PowerShell.

One way to determine this is not to do the download and just check what the website page is delivering. Hit the site, using your browser developer tools, and see what is there.

Since we'd not have access to the file you are using (which looks very suspicious, btw) via the site you are showing, you should try another site, and text file for validation.

For example, this site's text file comes down as expected on one of my isolated test systems.

$url = 'https://lists.evolt.org/archive/Week-of-Mon-20020408.txt'
Invoke-WebRequest $url -OutFile 'C:\Temp\Week-of-Mon-20020408.txt'
Get-Content -Path 'C:\Temp\Week-of-Mon-20020408.txt'

Or this sample one...

$url = 'https://filesamples.com/samples/document/txt/sample1.txt'
Invoke-WebRequest $url -OutFile 'C:\Temp\sample1.txt'
Get-Content -Path 'C:\Temp\sample1.txt'
6
  • Emphasis on the “very suspicious.” We may well be looking at malware.
    – JG7
    Feb 24, 2021 at 18:02
  • Roger that, and it could be the OP's AV smacking up against this and rendering is in an inoperable state as well.
    – postanote
    Feb 24, 2021 at 18:06
  • that example code is copied from stackoverflow just to check text format stackoverflow.com/questions/7044985/…
    – Sadam
    Feb 24, 2021 at 18:55
  • That's fine, but that Q&A is not offering up a resource URL for downloading that answer. So, this means you are hosting this answer as a text file at another URL. Thus knowing where you got the original code is not really prudent. As I pointed, in my answer, other URL hosting text files come down fully formatted as expected. Did you try them yourself? To test your scenario, that means us installing IIS, and spinning up a website, hosting the file, and trying to download it as a validation.
    – postanote
    Feb 24, 2021 at 19:05
  • 1
    If you are getting this issue no matter what text file you download from, then this is an environmental issue on your host. Either try it from another machine or VM you have access to. If you are doing this at work, then edge-based risk management tools can also be impacting your use case. Again, the URLs I lited, work from my labs as expected my common host system. So, I cannot reproduce what you are seeing on my end.
    – postanote
    Feb 24, 2021 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .