install-labbuildr.ps1
<#PSScriptInfo
.VERSION 2.0 .GUID fe3fb4e9-9977-4f99-8a71-b717acac7e30 .AUTHOR "Karsten Bott" .COMPANYNAME .COPYRIGHT Karsten Bott .TAGS .LICENSEURI .PROJECTURI http://github.com/bottkars/labbuildr .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .DESCRIPTION "This is the installer for labbuildr" #> [CmdletBinding(DefaultParametersetName = "install", SupportsShouldProcess=$true, ConfirmImpact="Medium")] [OutputType([psobject])] param ( [Parameter(ParameterSetName = "install",Mandatory = $false, HelpMessage = "select a branch to install from")][string]$Installpath = "C:\labbuildr2017", [Parameter(ParameterSetName = "install",Mandatory = $false, HelpMessage = "select a branch to install from")][ValidateSet('master','testing','develop')]$branch = "master" ) function Install-FromGit { param ( [string]$Repo, [string]$RepoLocation, [string]$branch, [string]$Destination ) Write-Verbose "Using Install-FromGit function for $repo" $Uri = "https://api.github.com/repos/$RepoLocation/$repo/commits/$branch" $Zip = ("https://github.com/$RepoLocation/$repo/archive/$branch.zip").ToLower() Write-Host -ForegroundColor Magenta "Now Downloading $Repo" $DownloadPath = Join-path $Installpath "Install" if (!(Get-Item -Path $DownloadPath -ErrorAction SilentlyContinue)) { $newDir = New-Item -ItemType Directory -Path "$DownloadPath" } Write-Host "We are now downloading $Zip" Get-LABHttpFile -SourceURL $Zip -TarGetFile "$DownloadPath\$repo-$branch.zip" -ignoresize if ($branch -match "develop") { Expand-LABZip -zipfilename "$DownloadPath\$repo-$branch.zip" -destination $Destination -Folder $repo-$branch/$repo } else { Expand-LABZip -zipfilename "$DownloadPath\$repo-$branch.zip" -destination $Destination -Folder $repo-$branch } } ##### function Expand-LABZip { [CmdletBinding(DefaultParameterSetName='Parameter Set 1', HelpUri = "https://github.com/bottkars/LABbuildr/wiki/LABtools#Expand-LABZip")] param ( [string]$zipfilename, [string]$destination, [String]$Folder) $copyFlag = 16 # overwrite = yes $Origin = $MyInvocation.MyCommand if (test-path($zipfilename)) { If ($Folder) { $zipfilename = Join-Path $zipfilename $Folder } Write-Verbose "extracting $zipfilename to $destination" if (!(test-path $destination)) { New-Item -ItemType Directory -Force -Path $destination | Out-Null } $shellApplication = New-object -com shell.application $zipPackage = $shellApplication.NameSpace($zipfilename) $destinationFolder = $shellApplication.NameSpace("$destination") $destinationFolder.CopyHere($zipPackage.Items(), $copyFlag) } } function Get-LABHttpFile { [CmdletBinding(DefaultParametersetName = "1", HelpUri = "https://github.com/bottkars/LABbuildr/wiki/LABtools#GET-LABHttpFile")] param ( [Parameter(ParameterSetName = "1", Mandatory = $true,Position = 0)]$SourceURL, [Parameter(ParameterSetName = "1", Mandatory = $false)]$TarGetFile, [Parameter(ParameterSetName = "1", Mandatory = $false)][switch]$ignoresize ) if (!$TarGetFile) { $TarGetFile = Split-Path -Leaf $SourceURL } try { $Request = Invoke-WebRequest $SourceURL -UseBasicParsing -Method Head } catch [Exception] { Write-Warning "Could not downlod $SourceURL" Write-Warning $_.Exception break } $Length = $request.Headers.'content-length' try { Invoke-WebRequest $SourceURL -OutFile $TarGetFile } catch [Exception] { Write-Warning "Could not downlod $SourceURL. please download manually" Write-Warning $_.Exception break } if ( (Get-ChildItem $TarGetFile).length -ne $Length -and !$ignoresize) { Write-Warning "File size does not match" Remove-Item $TarGetFile -Force break } } ############################ Push-Location $Icon = Split-Path -Leaf $Installpath try { New-Item -ItemType Directory -Path $Installpath -force -ErrorAction Stop | Out-Null } catch [System.IO.DriveNotFoundException] { Write-Warning "could not create directory $Installpath, error was $_" break } if ($Installpath[-1] -eq "\") { $Installpath = $Installpath -replace ".$" } Set-Location $Installpath $Repo = "labbuildr" $RepoLocation = "bottkars" $Destination = "$Installpath" Install-FromGit -Repo $Repo -RepoLocation $RepoLocation -branch $branch -Destination $Destination #### $Repo = "labbuildr-scripts" $RepoLocation = "bottkars" $Destination = Join-Path $Installpath $Repo Install-FromGit -Repo $Repo -RepoLocation $RepoLocation -branch $branch -Destination $Destination #### $Repo = "labtools" $RepoLocation = "bottkars" $Destination = Join-Path $Installpath $Repo Install-FromGit -Repo $Repo -RepoLocation $RepoLocation -branch $branch -Destination $Destination #### $Repo = "vmxtoolkit" $RepoLocation = "bottkars" $Destination = Join-Path $Installpath $Repo Install-FromGit -Repo $Repo -RepoLocation $RepoLocation -branch $branch -Destination $Destination ##### $Branch | Set-Content -Path "$Installpath\labbuildr.branch" -Force # -Verbose ipmo .\vmxtoolkit ipmo .\labtools .\build-lab.ps1 -createshortcut Write-Host -ForegroundColor Cyan "Installed labbuildr in $Installpath, A Descktopicon was created for $Icon Press return to Continue and click on the Desktopicon for $Icon install your NAT Gateway with Receive-LabOpenWRT -unzip Start the NAT Gateway with Start-vmx OpenWRT" Pause stop-process -Id $PID |