Skip to content
Permalink
Browse files

Add a script to update package

  • Loading branch information
Quoc Truong
Quoc Truong committed Nov 25, 2015
1 parent c00694c commit bd718d3fd43387d31101b7140a9d271ddfdb4bf1
Showing with 67 additions and 0 deletions.
  1. +67 −0 UpdatePackage.psm1
@@ -0,0 +1,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"
}
}
}
}

0 comments on commit bd718d3

Please sign in to comment.
You can’t perform that action at this time.