What It Does
The script downloads Url to DestinationPath. It never runs, opens or unpacks what it downloads. Only https is accepted, a redirect to anything but https is refused, and so is an address with a user name or password in it. It will not download without a way to check the file: ExpectedSha256 is required, and only AllowUnverified=true downloads without one, in which case the result says NOT verified and prints the file's SHA-256 so you can pin it.
MaxSizeMB, 500 by default, is enforced twice: from the size the server announces, before any data is saved, and while streaming when the size is not announced. The file is saved to a temporary file in the destination folder, its SHA-256 is compared when you supply one, and a match is moved into place in one step. With AllowUnverified=true and no expected hash, it reports the computed hash and moves the file into place unverified. A failed download leaves the destination file untouched, but a newly created destination folder can remain.
An existing file is never replaced unless Overwrite=true, and then a copy of it is saved to the MSPOutpost backup folder first. If the existing file already has the SHA-256 you expect, nothing is downloaded and the run says it is already in place. It tries up to 3 times on network errors and server errors, including HTTP 408 and 429. It does not retry other 4xx replies or size, redirect or hash problems. Query strings, which can carry access tokens, are never printed or logged; only the host and path are.
It refuses a destination inside the Windows folder or the MSPOutpost data folder, a UNC path, a relative path, and a folder name instead of a file. A missing destination folder is created.
Before You Run It
- Take the SHA-256 from the vendor's own site, for the exact file you will download.
- Give a full local path with a drive letter, such as C:\Temp\agent.msi. A UNC path or a relative path is refused.
- Run it with DryRun=true first to see what would be downloaded. It downloads nothing.
- Use AllowUnverified only to learn a hash, then pin it with ExpectedSha256 from then on.
- Run the downloaded file yourself or with another script. This one never does.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: Low. Set DryRun=true, or run it with -WhatIf, to list every change without making any.
- What it changes: Creates (or, with Overwrite, replaces) one file at DestinationPath and its folder; leaves no temporary files.
- Restart: It never restarts the machine.
- Typical run: Seconds to minutes, by file size.
- Example answer: "Downloaded 12.4 MB from vendor.example to C:\Temp\agent.msi; SHA-256 matched"
- Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- Url: The https address to download. Required.
- DestinationPath: Full path of the file to create, for example C:\Temp\agent.msi. The folder is created if needed. Required.
- ExpectedSha256: The SHA-256 the file must have: 64 hexadecimal characters, any case. Take it from the vendor. Required unless AllowUnverified is true.
- AllowUnverified: true downloads without a hash to check (the file cannot be proven to be the one you meant). true or false. Default false.
- MaxSizeMB: Refuse a file bigger than this many megabytes. Whole number, 1 to 10240. Default 500.
- Overwrite: true replaces an existing file at DestinationPath (after backing it up). Default false.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.