Save-PowerCLI v2.1

- fixed incorrect module version download
- added a working cleanup in End

Signed-off-by: Luc Dekens <dekens.luc@gmail.com>
This commit is contained in:
LucD
2019-05-08 10:07:26 +02:00
committed by Luc
parent c91a764ffe
commit 6f22da7f67

View File

@@ -1,27 +1,30 @@
function Save-PowerCLI {
<#
.SYNOPSIS
function Save-PowerCLI {
<#
.SYNOPSIS
Advanced function which can be used to easily download specific versions of PowerCLI from an online gallery
.DESCRIPTION
.DESCRIPTION
Downloads a specific version of PowerCLI and all the dependencies at the appropriate version
.NOTES
.NOTES
Author: 1.0 - Dimitar Milov
Author: 2.0 - Kyle Ruddy, @kmruddy
.PARAMETER RequiredVersion
Author: 2.1 - Luc Dekens, @LucD22
- fixed issue with downloading the correct versions
- added a working cleanup of unwanted versions
.PARAMETER RequiredVersion
Dynamic parameter used to specify the PowerCLI version
.PARAMETER Path
.PARAMETER Path
Directory path where the modules should be downloaded
.PARAMETER Repository
.PARAMETER Repository
Repository to access the PowerCLI modules
.PARAMETER Simple
.PARAMETER Simple
Switch used to specify the nested version folders should be removed (therefore adding PowerShell 3/4 compatibility)
.EXAMPLE
.EXAMPLE
Save-PowerCLI -RequiredVersion '10.0.0.7895300' -Path .\Downloads\
Downloads PowerCLI 10.0.0 to the Downloads folder
.EXAMPLE
.EXAMPLE
Save-PowerCLI -RequiredVersion '6.5.2.6268016' -Path .\Downloads\ -Simple
Downloads PowerCLI 6.5.2 to the Downloads folder and removes the nested version folders
#>
#>
[CmdletBinding(SupportsShouldProcess = $True)]
param(
[Parameter(Mandatory = $true, Position = 1)]
@@ -62,14 +65,14 @@ function Save-PowerCLI {
$AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [String], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary
}
begin {
$powercliModuleName = 'VMware.PowerCLI'
$desiredPowerCLIModule = Find-Module -Name $powercliModuleName -RequiredVersion $RequiredVersion -Repository $Repository
$desiredPowerCLIModule = Find-Module -Name $powercliModuleName -RequiredVersion $PSBoundParameters.RequiredVersion -Repository $Repository
$depsOrder = 'VMware.VimAutomation.Sdk', 'VMware.VimAutomation.Common', 'VMware.Vim', 'VMware.VimAutomation.Cis.Core', 'VMware.VimAutomation.Core', 'VMware.VimAutomation.Nsxt', 'VMware.VimAutomation.Vmc', 'VMware.VimAutomation.Vds', 'VMware.VimAutomation.Srm', 'VMware.ImageBuilder', 'VMware.VimAutomation.Storage', 'VMware.VimAutomation.StorageUtility', 'VMware.VimAutomation.License', 'VMware.VumAutomation', 'VMware.VimAutomation.HorizonView', 'VMware.DeployAutomation', 'VMware.VimAutomation.vROps', 'VMware.VimAutomation.PCloud'
$orderedDependencies = @()
@@ -94,14 +97,23 @@ function Save-PowerCLI {
if (Get-ChildItem -Path (Join-Path $path $dependency.Name) | Where-Object {$_.Name -ne $dependency.MinimumVersion}) {
# Save dependencies with minimum version
Find-Module $dependency.Name -RequiredVersion $dependency.MinimumVersion | Save-Module -Path $Path
# Remove newer dependencies version
Get-ChildItem -Path (Join-Path $path $dependency.Name) | Where-Object {$_.Name -ne $dependency.MinimumVersion} | Remove-Item -Confirm:$false -Force -Recurse
}
}
}
end {
Get-Item -Path "$($Path)\*" -PipelineVariable dir |
ForEach-Object -Process {
$children = Get-ChildItem -Path $dir.FullName -Directory
if($children.Count -gt 1){
$tgtVersion = $orderedDependencies.GetEnumerator() | where {$_.Name -eq $dir.Name}
$children | where{$_.Name -ne $tgtVersion.MinimumVersion} |
ForEach-Object -Process {
Remove-Item -Path $_.FullName -Recurse -Force -Confirm:$false
}
}
}
if ($Simple) {
function FolderCleanup {
@@ -142,6 +154,5 @@ function Save-PowerCLI {
}
}
}
}
}