Public/Start-EvergreenLibraryUpdate.ps1
|
function Start-EvergreenLibraryUpdate { <# .EXTERNALHELP Evergreen-help.xml #> [CmdletBinding(SupportsShouldProcess = $true)] [Alias("Invoke-EvergreenLibraryUpdate")] param ( [Parameter( Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName, HelpMessage = "Specify the path to the library.", ParameterSetName = "Path")] [ValidateNotNull()] [System.IO.FileInfo] $Path, [Parameter(Mandatory = $false, Position = 1)] [System.String] $Proxy, [Parameter(Mandatory = $false, Position = 2)] [System.Management.Automation.PSCredential] $ProxyCredential = [System.Management.Automation.PSCredential]::Empty ) begin { if ($PSBoundParameters.ContainsKey("ProxyCredential") -and -not($PSBoundParameters.ContainsKey("Proxy"))) { throw [System.ArgumentException]::New("ProxyCredential requires Proxy.") } $params = @{} if ($PSBoundParameters.ContainsKey("Proxy")) { $params.Proxy = $Proxy } if ($PSBoundParameters.ContainsKey("ProxyCredential")) { $params.ProxyCredential = $ProxyCredential } $PathComparison = if ([System.Environment]::OSVersion.Platform -eq [System.PlatformID]::Win32NT) { [System.StringComparison]::OrdinalIgnoreCase } else { [System.StringComparison]::Ordinal } } process { if (Test-Path -Path $Path -PathType "Container") { $LibraryFile = $(Join-Path -Path $Path -ChildPath "EvergreenLibrary.json") if (Test-Path -Path $LibraryFile) { Write-Verbose -Message "Library exists: $LibraryFile." $Library = Get-Content -Path $LibraryFile -ErrorAction "Stop" | ConvertFrom-Json -ErrorAction "Stop" # Get current list of install media files present in library $LibContentBefore = Get-ChildItem -Path $Path -File -Recurse -Exclude "*.json" | ` Select-Object -Property "FullName" | Sort-Object -Property "FullName" # Return the application details foreach ($Application in $Library.Applications) { [System.Array] $App = @() [System.Array] $Saved = @() [System.Array] $AppToExport = @() $AppPath = $(Join-Path -Path $Path -ChildPath $Application.Name) Write-Verbose -Message "$($MyInvocation.MyCommand): Application path: $AppPath." Write-Verbose -Message "$($MyInvocation.MyCommand): Query Evergreen for: $($Application.Name)." # Gather the application version information from Get-EvergreenApp if ([System.String]::IsNullOrEmpty($Application.Filter)) { Write-Verbose -Message "$($MyInvocation.MyCommand): No filter specified for $($Application.Name)." $App = @(Get-EvergreenApp -Name $Application.EvergreenApp @params) } else { try { Write-Verbose -Message "$($MyInvocation.MyCommand): Filter: $($Application.Filter)." $WhereBlock = [ScriptBlock]::Create($Application.Filter) $App = @(Get-EvergreenApp -Name $Application.EvergreenApp @params | Where-Object $WhereBlock) } catch { Write-Warning -Message "$($MyInvocation.MyCommand): Failed applying filter for $($Application.Name) with: $($_.Exception.Message)" } } # If something returned, add to the library if ($null -ne $App) { Write-Verbose -Message "$($MyInvocation.MyCommand): Download count for $($Application.EvergreenApp): $($App.Count)." # Save the installers to the library if ($PSCmdlet.ShouldProcess("Downloading $($App.Count) application installers.", "Save-EvergreenApp")) { $Saved = @($App | Save-EvergreenApp -Path $AppPath @params) } # Export only versions whose installer was saved or already present. foreach ($Version in $App) { $InstallerName = if ([System.Boolean]$Version.FileName) { $Version.FileName } else { Split-Path -Path $Version.URI -Leaf } $ExpectedInstallerDirectory = $AppPath foreach ($PropertyName in @("Product", "Track", "Channel", "Release", "Ring", "Version", "Language", "Architecture")) { if ([System.Boolean]$Version.$PropertyName) { $ExpectedInstallerDirectory = Join-Path -Path $ExpectedInstallerDirectory -ChildPath ([System.String]$Version.$PropertyName) } } $SavedInstaller = $Saved | Where-Object { if ($_.Name -ne $InstallerName) { return $false } $SavedDirectory = [System.IO.Path]::GetFullPath($_.DirectoryName) [System.String]::Equals( $SavedDirectory, [System.IO.Path]::GetFullPath($ExpectedInstallerDirectory), $PathComparison ) } | Select-Object -First 1 if ($null -ne $SavedInstaller) { Write-Verbose -Message "$($MyInvocation.MyCommand): Add path to object: $($SavedInstaller.FullName)" $Version | Add-Member -MemberType "NoteProperty" -Name "Path" -Value $SavedInstaller.FullName -Force $AppToExport += $Version } else { Write-Warning -Message "$($MyInvocation.MyCommand): No saved installer found for '$($Version.URI)'; version will not be added to the library." } } if ($AppToExport.Count -gt 0) { Export-EvergreenApp -InputObject $AppToExport -Path $(Join-Path -Path $AppPath -ChildPath "$($Application.Name).json") | Out-Null } } } # Get new list of install media files present in library following update $LibContentAfter = Get-ChildItem -Path $Path -File -Recurse -Exclude "*.json" | Select-Object FullName | Sort-Object FullName # Output details of library updates if ($null -eq $LibContentAfter) { Write-Warning -Message "$($MyInvocation.MyCommand): No media found in Evergreen Library" } elseif ($null -ne $LibContentBefore) { (Compare-Object $LibContentBefore $LibContentAfter -Property FullName -IncludeEqual | ForEach-Object { [PSCustomObject]@{ Installer = $_.FullName Status = $_.SideIndicator -replace "=>", "NEW" -replace "==", "UNCHANGED" -replace "<=", "DELETED" } }) } else { ($LibContentAfter | ForEach-Object { [PSCustomObject]@{ Installer = $_.FullName Status = "NEW" } }) } } else { $Msg = "$Path is not an Evergreen Library. Cannot find EvergreenLibrary.json. Create a library with New-EvergreenLibrary." throw [System.IO.FileNotFoundException]::New($Msg) } } else { $Msg = "Cannot use path $Path because it does not exist or is not a directory." throw [System.IO.DirectoryNotFoundException]::New($Msg) } } } |