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
{
# Set the dynamic parameters name
$ParameterName = 'RequiredVersion'
# Create the dictionary # Create the dictionary
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Create the collection of attributes # Create the collection of attributes
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Create and set the parameters' attributes # Create and set the parameters' attributes
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.ValueFromPipeline = $true $ParameterAttribute.ValueFromPipeline = $true
$ParameterAttribute.ValueFromPipelineByPropertyName = $true $ParameterAttribute.ValueFromPipelineByPropertyName = $true
$ParameterAttribute.Mandatory = $true $ParameterAttribute.Mandatory = $true
$ParameterAttribute.Position = 0 $ParameterAttribute.Position = 0
# Add the attributes to the attributes collection # Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute) $AttributeCollection.Add($ParameterAttribute)
# Generate and set the ValidateSet # Generate and set the ValidateSet
$pcliVersions = Find-Module -Name 'VMware.PowerCLI' -AllVersions $pcliVersions = Find-Module -Name 'VMware.PowerCLI' -AllVersions
$arrSet = $pcliVersions | select-Object -ExpandProperty Version $arrSet = $pcliVersions | select-Object -ExpandProperty Version
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
# Add the ValidateSet to the attributes collection # Add the ValidateSet to the attributes collection
$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 {
$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
# 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) {
function FolderCleanup {
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateScript( { Test-Path $_} )]
$ParentFolder,
[Parameter(Mandatory = $true, Position = 1)]
[String]$ModuleName,
[Parameter(Mandatory = $true, Position = 2)]
$Version
)
$topFolder = Get-Item -Path (Join-Path $ParentFolder $ModuleName)
$versionFolder = $topFolder | Get-ChildItem -Directory | Where-Object {$_.Name -eq $Version}
$versionFolder | Get-ChildItem | Copy-Item -Destination $topFolder
# Checking for any nested folders within the PowerCLI module version folder
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
$nestFolder = $versionFolder| Get-ChildItem -Directory
foreach ($nestDir in $nestFolder) {
$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
foreach ($cleanUp in $orderedDependencies) {
FolderCleanup -ParentFolder $Path -ModuleName $cleanUp.Name -Version $cleanUp.MinimumVersion
}
} }
} }
} }
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
}
}
}
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 {
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateScript( { Test-Path $_} )]
$ParentFolder,
[Parameter(Mandatory = $true, Position = 1)]
[String]$ModuleName,
[Parameter(Mandatory = $true, Position = 2)]
$Version
)
$topFolder = Get-Item -Path (Join-Path $ParentFolder $ModuleName)
$versionFolder = $topFolder | Get-ChildItem -Directory | Where-Object {$_.Name -eq $Version}
$versionFolder | Get-ChildItem | Copy-Item -Destination $topFolder
# Checking for any nested folders within the PowerCLI module version folder
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
$nestFolder = $versionFolder| Get-ChildItem -Directory
foreach ($nestDir in $nestFolder) {
$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
foreach ($cleanUp in $orderedDependencies) {
FolderCleanup -ParentFolder $Path -ModuleName $cleanUp.Name -Version $cleanUp.MinimumVersion
}
}
}
}