Last active
April 7, 2020 16:59
-
-
Save kevinCefalu/7b878f774a1765f9fb70a61972f015da to your computer and use it in GitHub Desktop.
Function to download a thingiverse file archive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ThingiverseFile | |
{ | |
[CmdletBinding()] | |
[OutputType([void])] | |
param ( | |
[Parameter(Mandatory)] | |
[uint32] $Id, | |
[Parameter(Mandatory)] | |
[IO.DirectoryInfo] $OutputDirectory | |
); | |
do | |
{ | |
[IO.DirectoryInfo] $Directory = "$([IO.Path]::GetTempPath())\$([Guid]::NewGuid().Guid)"; | |
} | |
while ($Directory.Exists) | |
New-Item -ItemType Directory -Path $Directory | Out-Null; | |
[IO.FileInfo] $FilePath = Join-Path -Path $Directory -ChildPath "$([Guid]::NewGuid().Guid).zip"; | |
Invoke-WebRequest -Uri "https://www.thingiverse.com/thing:$Id/zip" -OutFile $FilePath; | |
[IO.DirectoryInfo] $OriginalOutputDirectoryPath = $OutputDirectory.FullName; | |
$OutputDirectoryIncrement = 1; | |
while ($OutputDirectory.Exists) | |
{ | |
[IO.DirectoryInfo] $OutputDirectory = ( | |
$OriginalOutputDirectoryPath.FullName + | |
'-' + $OutputDirectoryIncrement++ | |
); | |
} | |
Expand-7Zip -ArchiveFileName $FilePath -TargetPath $OutputDirectory; | |
Remove-Item -Path $Directory -Recurse -Force; | |
Start-Process $OutputDirectory; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment