-
Notifications
You must be signed in to change notification settings - Fork 189
/
UpdatePackage.psm1
67 lines (56 loc) · 2.34 KB
/
UpdatePackage.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function Update-Package
{
[CmdletBinding()]
Param
(
# Name of the package
[Parameter(Mandatory=$true,
Position=0)]
[string[]]
$Name,
# Provider associated with the package
[Alias("Provider")]
[string]
$ProviderName
)
DynamicParam {
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Get the required dynamic parameter for install
if (-not ([string]::IsNullOrWhiteSpace($ProviderName)))
{
$providerObject = Get-PackageProvider -Name $ProviderName | Select -First 1
if ($null -ne $providerObject -and ($providerObject.DynamicOptions -ne $null -and $providerObject.DynamicOptions.Count -gt 0))
{
foreach ($option in $providerObject.DynamicOptions)
{
$optionalAttribute = New-Object System.Management.Automation.ParameterAttribute
$optionalAttribute.Mandatory = $option.IsRequired
$attributes = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$attributes.Add($optionalAttribute)
$param = New-Object System.Management.Automation.RuntimeDefinedParameter($option.Name, [System.Object], $attributes)
$paramDictionary.Add($option.Name, $param)
}
}
}
return $paramDictionary
}
Process {
$packagesToBeUpdated = Get-Package @PSBoundParameters
foreach ($package in $packagesToBeUpdated)
{
$possibleNewPackage = Find-Package -Name $package.Name -ProviderName $package.ProviderName
$possibleNewVersion = [version]$possibleNewPackage.Version
$version = [version]$package.Version
if ($possibleNewVersion -gt $version)
{
Write-Verbose "Need to update since $possibleNewVersion is found for $($package.Name) which has version $($package.Version)"
$PSBoundParameters["RequiredVersion"] = $possibleNewVersion
Install-Package @PSBoundParameters
}
else
{
Write-Verbose "$($package.Name) has the latest version $possibleNewVersion"
}
}
}
}