0

Problem found on server with Powershell v 4.0 (server with Powershell 5.1 they do not seem affected by the same problem)

This a strange behavior of Invoke-WebRequest command.

Take this example:

For some reason, I need to know the file size before download it on my server.

Then I use the following commands:

$WebClient = Invoke-WebRequest -Uri $element -Method Head -Credential $Cred
$filesize = $webClient.Headers.'Content-Length'

The problem is before send $WebClient request Powershell use (for Commit RAM) about 120MB, but after the request powershell enlarge your RAM a the file $filesize value (example remote file is 800MB, new Commit RAM is 920MB).

When you work with Powershell WinRM session limited at 1GB of RAM this can be a problem. (OOM)

1 Answer 1

1

You can use Invoke-WebRequest with the HEAD method to get just the headers and not download anything. If the resource you're requesting has a known length, then you'll get a Content-Length header which you can use:

(Invoke-WebRequest $url -Method Head).Headers.'Content-Length'

Just note that not all servers return the Content-Length header for all requests. In that case you will need to use your above method which reads the entire file, although it's slower and wasteful of memory.

You can also interface directly to the Windows DLLs that do Internet requests, either WinINet (not available on Windows Server), or using Windows HTTP Services.

2
  • If you send and HEAD request, and the remote system know the Content-Lenght of the resource request, Powershell (and all other language) not receive the file, but only a the HEAD with the Content-Lenght inside. So, I guess, it makes no sense to allocate as much memory as the file is large if I only do a head request. Commented Dec 5, 2018 at 13:40
  • Actually you are right and there is a way using Invoke-WebRequest. I rewrote my answer.
    – harrymc
    Commented Dec 5, 2018 at 13:52

You must log in to answer this question.

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