Powershell Invoke-WebRequest with Method Head allocate too much RAM

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)

Max Monterumisi

Posted 2018-12-05T09:47:13.863

Reputation: 1

Answers

0

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.

harrymc

Posted 2018-12-05T09:47:13.863

Reputation: 306 093

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. – Max Monterumisi – 2018-12-05T13:40:37.283

Actually you are right and there is a way using Invoke-WebRequest. I rewrote my answer. – harrymc – 2018-12-05T13:52:22.310