boche.net – VMware vEvangelist 6分钟前
MegaSceneryEarth automated HTTP download PowerShell script
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何使用PowerShell脚本自动化下载MegaSceneryEarth的风景文件。作者在购买了多个风景包后,面临手动下载大量2GB文件的问题。为了解决这个问题,他编写了一个PowerShell脚本,通过解析下载链接、自动处理文件名和循环下载,实现了高效、自动化的下载过程。该脚本支持自定义下载路径和起始文件编号,并能处理不同数量的文件,极大地节省了下载时间,让用户可以更专注于模拟飞行体验。

💻 **手动下载的痛点:** 作者购买了MegaSceneryEarth的风景包,但下载过程需要手动点击大量2GB的文件,耗时且繁琐。

💡 **PowerShell脚本的诞生:** 为了解决手动下载的效率问题,作者开发了一个PowerShell脚本,用于自动化下载过程。

⚙️ **脚本的核心功能:** 脚本能够解析下载链接、自动处理文件名、循环下载文件,并支持自定义下载路径和起始文件编号。

🚀 **脚本的优势:** 使用脚本可以极大地节省下载时间,提高效率,让用户可以更专注于模拟飞行体验。

🛠️ **脚本的灵活性:** 脚本可以处理不同数量的文件,并且可以根据用户的需求进行配置。

Regulars: Thank you for stopping by but just as a heads up, this post is not VMware virtualization related.

After a bit of hardware trouble from the local store, my replacement flight sim rig is up and running Lockheed Martin Prepar3D 4.3. I’m trying to shake the rust off after not having flown a leg for quite some time but everything sim and add-on related is looking good and I was able to fly KMSP-KLAX with many more FPS that I’ve been used to the past many years on FSX.

Now, out of sheer coincidence late last week I received a promotion from MegaSceneryEarth. Normally I ignore these but since this was a 50% off deal and I’m essentially starting from ground zero with no scenery for Prepar3D, I took a look. After some chin scratching, I checked out with Minnesota, Arizona, Northern California, and Southern California. I felt like quite the opportunist until the email arrived with file download instructions. Each scenery is broken up into 2GB download chunks. Between the four sceneries, I was presented with 123 download links. With no FTP option that I’m aware of, I’m going to have to babysit this process. With each 2GB download taking anywhere from 5-10 minutes, this is going to feel a lot like installing Microsoft Office 2016 via floppy disk. Except longer. MegaSceneryEarth is available on shipped DVDs or USB sticks they cost a small fortune and of course the shipping and handling time. This is going to be painful.

But… there’s a cheaper, faster, and fully automated way. PowerShell scripting.

Now admittedly for my first scenery, I threw the v1.0 PowerShell script version together quickly using the Invoke-WebRequest cmdlet with my own static scenery-specific lines of code to serve my own needs. And it worked. I launched the script, walked away for a few hours, and when I came back I had a directory full of MegaSceneryEarth .zip files ready for installation. It worked beautifully and it didn’t consume bunches of my time.

I still had three sceneries left to download. While I could have made new static scripts for the remaining three sceneries, my mind was already going down the road that this script would be more powerful if one script worked on a variety of MegaSceneryEarth downloads. It becomes flexible to use for my future MegaSceneryEarth sceneries as well as extensible if shared with the community.

So on Sunday, version 2.0 is what I came up with. It takes three inputs in the form of modifying variables at the beginning of the script before running it:

    The download URL for the first file of the sceneryThe path on the local hard drive where all of the .zip files should be downloaded toThe starting point or first file to download. Usually this will be 1 but if for some reason a previous download was stopped or interrupted at say 6, we’d want to continue from there assuming we don’t want to re-download the first 5 files all over again.

The script does the rest of the heavy lifting. All three of the above are required for functionality but by far the first variable is the most important and where I spent almost all of my time making the script flexible so that it works across MegaSceneryEarth sceneries. When provided with the download URL of the first file of the scenery, the script does a bunch of parsing, splitting, and concatenation of stuff to automatically determine some things:

    It figures out what the base path and file name URL is for the scenery. For any given scenery, these parts never change across all of the download links within a scenery but they will change across sceneries.It figures out how many .zip files there are to download in total for the entire scenery.Because MegaSceneryEarth likes to use leading zeroes as part of their naming convention, and leading zero strings aren’t the same thing as leading zero integers in terms of PowerShell constructs, it figures out how to deal with those for file names and looping iterations. Basically when you go from “009” files in a scenery download to “010”, without dealing with that, stuff breaks. I think most MegaSceneryEarth sceneries have more than 9 files, but some have less (Deleware has 1). The script deals with both kinds. That I know of, MegaSceneryEarth doesn’t have a scenery with more than 99 .zip files but version 2.1 of the script will accommodate that.It figures out the correct file name to save on the local hard drive for each scenery file downloaded. This may sound stupid but that I know of, the Invoke-WebRequest cmdlet requires the output file name and doesn’t automatically assume the file name should be the same as the file name being downloaded via HTTP such as a copy command to a folder would.

Generally speaking, for automation to work properly, it relies on input consistency so that all assumptions it makes are 100% correct. Translated, this script relies on consistent file/path naming conventions used by MegaSceneryEarth. Not only within sceneries, but across sceneries as well.

And so here it is. Copy the PowerShell script below and execute in your favorite PowerShell environment. Most of the time I simply use Windows PowerShell ISE. The code isn’t signed so you may need to temporarily relax your PowerShell ExecutionPolicy to Bypass . It’s there to protect you by default.

####################################################################################################################### MegaSceneryEarth.ps1 version 2.1 7/31/18## This PowerShell script automates unattended HTTP download of up to 999 MegaSceneryEarth scenery files# Relies on MegaSceneryEarth naming conventions current as of version date above# Tested successfully with four MegaSceneryEarth downloads# As with anything on the internet, use at your own risk, no warranty provided by the author## Update the three variables below as necessary for your specific scenery and download parameters## Jason Boche# http://boche.net# jason@boche.net####################################################################################################################### Provide the first file download link copied from the MegaSceneryEarth .htm link provided with license key email# Internet Explorer: Right click the download link and choose "Copy shortcut"# Google Chrome: Right click the download link and choose "Copy link address"# Then paste the link between the quotation marks$firstfile1 = "paste first file download link here.zip"# Provide the path on your hard drive where the files will temporarily be saved.# This will usually be where you have the MegaSceneryEarthInstallManager.exe file for subsequent installation# Syntax requires a trailing backslash (\)$dlpath = "D:\junk\"# Provide the starting file count. Usually we start with 1 unless you're starting the download somewhere after 1$startcount = 1# Do not edit anything below this line####################################################################################################################### Get the HTTP path$firstfile2 = split-path $firstfile1$firstfile3 = $firstfile2.Replace("\","/")$httppath = $firstfile3 + "/"# Get the .zip file name$firstfile4 = split-path $firstfile1 -leaf# Get the static part of the .zip file name$firstfile5 = $firstfile4 -split "001_of_"$firstfile6 = $firstfile5[0]$fileprefix = $firstfile6# Get the total number of scenery files in string format with and without .zip extension$firstfile7 = $firstfile5[1] -split ".zip"$firstfile8 = $firstfile5[1]# Convert total number of scenery files in string format to number format$total = [decimal]::Parse($firstfile7)if ($startcount -gt $total){Write-Host Error: Starting file specified is greater than total number of files. Please check the `$startcount variablepauseexit}# If there are 1-9 scenery files, download thoseif ($total -ge 1){$leadingzero = "00"while ($startcount -le $total){if ($startcount -gt 9){break}Write-Host Downloading $httppath$fileprefix$leadingzero$startcount"_of_"$firstfile8 to $dlpath$fileprefix$leadingzero$startcount"_of_"$firstfile8Invoke-WebRequest -Uri $httppath$fileprefix$leadingzero$startcount"_of_"$firstfile8 -OutFile $dlpath$fileprefix$leadingzero$startcount"_of_"$firstfile8$startcount ++}}# If there are 10-99 scenery files, download those alsoif ($total -ge 10){$leadingzero = "0"while ($startcount -le $total){if ($startcount -gt 99){break}Write-Host Downloading $httppath$fileprefix$leadingzero$startcount"_of_"$firstfile8 to $dlpath$fileprefix$leadingzero$startcount"_of_"$firstfile8Invoke-WebRequest -Uri $httppath$fileprefix$leadingzero$startcount"_of_"$firstfile8 -OutFile $dlpath$fileprefix$leadingzero$startcount"_of_"$firstfile8$startcount ++}}# If there are 100-999 scenery files, download those alsoif ($total -ge 100){$leadingzero = ""while ($startcount -le $total){if ($startcount -gt 999){break}Write-Host Downloading $httppath$fileprefix$leadingzero$startcount"_of_"$firstfile8 to $dlpath$fileprefix$leadingzero$startcount"_of_"$firstfile8Invoke-WebRequest -Uri $httppath$fileprefix$leadingzero$startcount"_of_"$firstfile8 -OutFile $dlpath$fileprefix$leadingzero$startcount"_of_"$firstfile8$startcount ++}}

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

PowerShell 自动化下载 MegaSceneryEarth 脚本
相关文章