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,147 +1,158 @@
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
Dynamic parameter used to specify the PowerCLI version - fixed issue with downloading the correct versions
.PARAMETER Path - added a working cleanup of unwanted versions
Directory path where the modules should be downloaded .PARAMETER RequiredVersion
.PARAMETER Repository Dynamic parameter used to specify the PowerCLI version
Repository to access the PowerCLI modules .PARAMETER Path
.PARAMETER Simple Directory path where the modules should be downloaded
Switch used to specify the nested version folders should be removed (therefore adding PowerShell 3/4 compatibility) .PARAMETER Repository
.EXAMPLE Repository to access the PowerCLI modules
Save-PowerCLI -RequiredVersion '10.0.0.7895300' -Path .\Downloads\ .PARAMETER Simple
Downloads PowerCLI 10.0.0 to the Downloads folder Switch used to specify the nested version folders should be removed (therefore adding PowerShell 3/4 compatibility)
.EXAMPLE .EXAMPLE
Save-PowerCLI -RequiredVersion '6.5.2.6268016' -Path .\Downloads\ -Simple Save-PowerCLI -RequiredVersion '10.0.0.7895300' -Path .\Downloads\
Downloads PowerCLI 6.5.2 to the Downloads folder and removes the nested version folders Downloads PowerCLI 10.0.0 to the Downloads folder
#> .EXAMPLE
[CmdletBinding(SupportsShouldProcess = $True)] Save-PowerCLI -RequiredVersion '6.5.2.6268016' -Path .\Downloads\ -Simple
param( Downloads PowerCLI 6.5.2 to the Downloads folder and removes the nested version folders
[Parameter(Mandatory = $true, Position = 1)] #>
[ValidateScript( { Test-Path $_} )] [CmdletBinding(SupportsShouldProcess = $True)]
$Path, param(
[Parameter(Mandatory = $false, Position = 2)] [Parameter(Mandatory = $true, Position = 1)]
[string]$Repository = 'PSGallery', [ValidateScript( { Test-Path $_} )]
[Parameter(Mandatory = $false, Position = 3)] $Path,
[Switch]$Simple [Parameter(Mandatory = $false, Position = 2)]
) [string]$Repository = 'PSGallery',
DynamicParam [Parameter(Mandatory = $false, Position = 3)]
{ [Switch]$Simple
# Set the dynamic parameters name )
$ParameterName = 'RequiredVersion' DynamicParam
{
# Create the dictionary # Set the dynamic parameters name
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $ParameterName = 'RequiredVersion'
# Create the collection of attributes # Create the dictionary
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Create and set the parameters' attributes # Create the collection of attributes
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$ParameterAttribute.ValueFromPipeline = $true
$ParameterAttribute.ValueFromPipelineByPropertyName = $true # Create and set the parameters' attributes
$ParameterAttribute.Mandatory = $true $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Position = 0 $ParameterAttribute.ValueFromPipeline = $true
$ParameterAttribute.ValueFromPipelineByPropertyName = $true
# Add the attributes to the attributes collection $ParameterAttribute.Mandatory = $true
$AttributeCollection.Add($ParameterAttribute) $ParameterAttribute.Position = 0
# Generate and set the ValidateSet # Add the attributes to the attributes collection
$pcliVersions = Find-Module -Name 'VMware.PowerCLI' -AllVersions $AttributeCollection.Add($ParameterAttribute)
$arrSet = $pcliVersions | select-Object -ExpandProperty Version
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) # Generate and set the ValidateSet
$pcliVersions = Find-Module -Name 'VMware.PowerCLI' -AllVersions
# Add the ValidateSet to the attributes collection $arrSet = $pcliVersions | select-Object -ExpandProperty Version
$AttributeCollection.Add($ValidateSetAttribute) $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
# Create and return the dynamic parameter # Add the ValidateSet to the attributes collection
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) $AttributeCollection.Add($ValidateSetAttribute)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary # Create and return the dynamic parameter
$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 $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 = @()
foreach ($depModuleName in $depsOrder) {
$orderedDependencies += $desiredPowerCLIModule.Dependencies | Where-Object {$_.Name -eq $depModuleName}
} }
begin { foreach ($remainingDep in $desiredPowerCLIModule.Dependencies) {
$powercliModuleName = 'VMware.PowerCLI' if ($orderedDependencies.Name -notcontains $remainingDep.Name) {
$desiredPowerCLIModule = Find-Module -Name $powercliModuleName -RequiredVersion $RequiredVersion -Repository $Repository $orderedDependencies += $remainingDep
$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 = @()
foreach ($depModuleName in $depsOrder) {
$orderedDependencies += $desiredPowerCLIModule.Dependencies | Where-Object {$_.Name -eq $depModuleName}
} }
foreach ($remainingDep in $desiredPowerCLIModule.Dependencies) { }
if ($orderedDependencies.Name -notcontains $remainingDep.Name) { }
$orderedDependencies += $remainingDep
} process {
# Save PowerCLI Module Version
$desiredPowerCLIModule | Save-Module -Path $Path
# Working with the depenent modules
foreach ($dependency in $orderedDependencies) {
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
} }
} }
}
process {
# Save PowerCLI Module Version end {
$desiredPowerCLIModule | Save-Module -Path $Path Get-Item -Path "$($Path)\*" -PipelineVariable dir |
ForEach-Object -Process {
# Working with the depenent modules $children = Get-ChildItem -Path $dir.FullName -Directory
foreach ($dependency in $orderedDependencies) { if($children.Count -gt 1){
if (Get-ChildItem -Path (Join-Path $path $dependency.Name) | Where-Object {$_.Name -ne $dependency.MinimumVersion}) { $tgtVersion = $orderedDependencies.GetEnumerator() | where {$_.Name -eq $dir.Name}
# Save dependencies with minimum version $children | where{$_.Name -ne $tgtVersion.MinimumVersion} |
Find-Module $dependency.Name -RequiredVersion $dependency.MinimumVersion | Save-Module -Path $Path ForEach-Object -Process {
Remove-Item -Path $_.FullName -Recurse -Force -Confirm:$false
# 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 { if ($Simple) {
if ($Simple) {
function FolderCleanup {
function FolderCleanup { param(
param( [Parameter(Mandatory = $true, Position = 0)]
[Parameter(Mandatory = $true, Position = 0)] [ValidateScript( { Test-Path $_} )]
[ValidateScript( { Test-Path $_} )] $ParentFolder,
$ParentFolder, [Parameter(Mandatory = $true, Position = 1)]
[Parameter(Mandatory = $true, Position = 1)] [String]$ModuleName,
[String]$ModuleName, [Parameter(Mandatory = $true, Position = 2)]
[Parameter(Mandatory = $true, Position = 2)] $Version
$Version )
)
$topFolder = Get-Item -Path (Join-Path $ParentFolder $ModuleName)
$topFolder = Get-Item -Path (Join-Path $ParentFolder $ModuleName) $versionFolder = $topFolder | Get-ChildItem -Directory | Where-Object {$_.Name -eq $Version}
$versionFolder = $topFolder | Get-ChildItem -Directory | Where-Object {$_.Name -eq $Version} $versionFolder | Get-ChildItem | Copy-Item -Destination $topFolder
$versionFolder | Get-ChildItem | Copy-Item -Destination $topFolder
# Checking for any nested folders within the PowerCLI module version folder
# Checking for any nested folders within the PowerCLI module version folder if ($versionFolder| Get-ChildItem -Directory) {
if ($versionFolder| Get-ChildItem -Directory) {
# Obtaining and storing the child items to a variable, then copying the items to the parent folder's nested folder
# Obtaining and storing the child items to a variable, then copying the items to the parent folder's nested folder $nestFolder = $versionFolder| Get-ChildItem -Directory
$nestFolder = $versionFolder| Get-ChildItem -Directory foreach ($nestDir in $nestFolder) {
foreach ($nestDir in $nestFolder) { $nestDir | Get-ChildItem | Copy-Item -Destination (Join-Path $topFolder $nestDir.Name)
$nestDir | Get-ChildItem | Copy-Item -Destination (Join-Path $topFolder $nestDir.Name)
}
} }
# Removing any of the former, no longer needed, directory structure
$versionFolder| Remove-Item -Recurse -Force
} }
FolderCleanup -ParentFolder $Path -ModuleName $desiredPowerCLIModule.Name -Version $desiredPowerCLIModule.Version # Removing any of the former, no longer needed, directory structure
foreach ($cleanUp in $orderedDependencies) { $versionFolder| Remove-Item -Recurse -Force
FolderCleanup -ParentFolder $Path -ModuleName $cleanUp.Name -Version $cleanUp.MinimumVersion
}
} }
FolderCleanup -ParentFolder $Path -ModuleName $desiredPowerCLIModule.Name -Version $desiredPowerCLIModule.Version
foreach ($cleanUp in $orderedDependencies) {
FolderCleanup -ParentFolder $Path -ModuleName $cleanUp.Name -Version $cleanUp.MinimumVersion
}
} }
} }
}