Merge pull request #282 from lucdekens/master

Save-PowerCLI v2.1
This commit is contained in:
Kyle Ruddy
2019-05-08 10:33:35 -04:00
committed by GitHub

View File

@@ -1,27 +1,30 @@
function Save-PowerCLI { function Save-PowerCLI {
<# <#
.SYNOPSIS .SYNOPSIS
Advanced function which can be used to easily download specific versions of PowerCLI from an online gallery 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 Downloads a specific version of PowerCLI and all the dependencies at the appropriate version
.NOTES .NOTES
Author: 1.0 - Dimitar Milov Author: 1.0 - Dimitar Milov
Author: 2.0 - Kyle Ruddy, @kmruddy 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 Dynamic parameter used to specify the PowerCLI version
.PARAMETER Path .PARAMETER Path
Directory path where the modules should be downloaded Directory path where the modules should be downloaded
.PARAMETER Repository .PARAMETER Repository
Repository to access the PowerCLI modules 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) 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\ Save-PowerCLI -RequiredVersion '10.0.0.7895300' -Path .\Downloads\
Downloads PowerCLI 10.0.0 to the Downloads folder Downloads PowerCLI 10.0.0 to the Downloads folder
.EXAMPLE .EXAMPLE
Save-PowerCLI -RequiredVersion '6.5.2.6268016' -Path .\Downloads\ -Simple 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 Downloads PowerCLI 6.5.2 to the Downloads folder and removes the nested version folders
#> #>
[CmdletBinding(SupportsShouldProcess = $True)] [CmdletBinding(SupportsShouldProcess = $True)]
param( param(
[Parameter(Mandatory = $true, Position = 1)] [Parameter(Mandatory = $true, Position = 1)]
@@ -62,14 +65,14 @@ function Save-PowerCLI {
$AttributeCollection.Add($ValidateSetAttribute) $AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter # 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) $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary return $RuntimeParameterDictionary
} }
begin { begin {
$powercliModuleName = 'VMware.PowerCLI' $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' $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 = @() $orderedDependencies = @()
@@ -94,14 +97,23 @@ function Save-PowerCLI {
if (Get-ChildItem -Path (Join-Path $path $dependency.Name) | Where-Object {$_.Name -ne $dependency.MinimumVersion}) { if (Get-ChildItem -Path (Join-Path $path $dependency.Name) | Where-Object {$_.Name -ne $dependency.MinimumVersion}) {
# Save dependencies with minimum version # Save dependencies with minimum version
Find-Module $dependency.Name -RequiredVersion $dependency.MinimumVersion | Save-Module -Path $Path 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 { 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) { if ($Simple) {
function FolderCleanup { function FolderCleanup {
@@ -142,6 +154,5 @@ function Save-PowerCLI {
} }
} }
}
} }
}