WriteAllBytes OutOfMemory in PowerShell


WriteAllBytes OutOfMemory in PowerShell



I have an API which returns a file as byte.
I am trying to download this file onto a local machine using PowerShell (needs to be PowerShell for other automation reasons).
I am using WriteAllBytes, however, it throws error with files which are larger than 100MB (I think, threshold might be different).


byte


WriteAllBytes



Are there any other ways to download these files and convert byte into an actual file?


byte



Here is what I have at the moment:


$fileInfo = New-Object ($namespace + ".fileInfoRequest")
$fileInfo.Filename = "$($File)"
$fileInfo.Hash = "e0d123e5f316bef78bfdf5a008837577" #random hash so ignore this.
$FileDetails = $WebService.GetFileInfo($fileInfo)
if ($FileDetails.Exists -eq "True") {
[IO.File]::WriteAllBytes("$($InstallPath)$($File)", $WebService.GetFileData($FileDetails))
} else {
Write-Host -ForegroundColor Red "File $($File.FileName) could not be found in the system"
}



$WebService.GetFileData($FileDetails) returns the file data in byte so this is the one that I need to manipulate somehow.


$WebService.GetFileData($FileDetails)





WriteAllBytes() can write hundreds of MB without problem, so I'd suspect the issue is with your API. Which we know nothing about.
– Ansgar Wiechers
Oct 9 '17 at 20:44


WriteAllBytes()





I did think it might be the case but unfortunately I cannot say what exactly the API is and how it's used. All I can say is that it is a web service written in C# (So technically not really an API lol) I will probably need to seek for an advise from the developers who wrote it. Thanks anyways
– Arturs Kirsis
Oct 9 '17 at 20:48





it throws error - what is "it" and what is the exact error message?
– wOxxOm
Oct 12 '17 at 13:45


it throws error




1 Answer
1



I faced the same message right this morning.
Weirdly in my case the problem was triggered only when using a remote powershell session, so I can see affinity with API which can be passing trough network as well.
It happened, in my case, that the same command from a "standard" powershell session opened directly on the server console was not raising the error.



I was able to avoid it by using the following on an admin powershell session on the server console:


set-item wsman:localhostShellMaxMemoryPerShellMB 2048



After that all the remote powershell session stop to give OutOfmemory.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages