From c7c212be37ed5795ce05802b9a238376670f3b5d Mon Sep 17 00:00:00 2001 From: JordanRitz Date: Tue, 29 Aug 2017 00:46:27 -0400 Subject: [PATCH] (GH-90) Update Get-ChocoInstalledPackage to Cache to ProgramData Get-ChocoInstalledPackage must run and parse choco list -lo for every package managed by DSC This commit ensures a cChoco folder in ProgramData and exports the custom object to Clixml, and will simply read it from file if last write time is less that 60 Seconds ago. --- .../cChocoPackageInstall.psm1 | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 91acfae..14cdabe 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -1,4 +1,4 @@ -# Copyright (c) 2017 Chocolatey Software, Inc. +# Copyright (c) 2017 Chocolatey Software, Inc. # Copyright (c) 2013 - 2017 Lawrence Gripper & original authors/contributors from https://github.com/chocolatey/cChoco # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -396,7 +396,32 @@ Function Upgrade-Package { } function Get-ChocoInstalledPackage { - Return (choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|") + $res = $null + $dateMinus = (Get-Date).AddSeconds('-60') + + $path = 'C:\ProgramData\cChoco' + $childPath = 'ChocoInstalled.xml' + $ChocoInstallList = Join-Path -Path $path -ChildPath $childPath + + $lastWriteTime = (Get-Item $ChocoInstallList -ErrorAction SilentlyContinue).LastWriteTime + + if ( $dateMinus -lt $lastWriteTime ) { + $res = Import-Clixml $ChocoInstallList + } + if (-not ( Test-Path $path )){ + New-Item -Path $path -ItemType Directory |Out-Null + } + if (-not $res){ + $res = choco list -lo | ForEach-Object { + $Obj = $_ -split '\s' + [pscustomobject]@{ + 'Name' = $Obj[0] + 'Version' = $Obj[1] + } + } + Export-Clixml -path $ChocoInstallList -InputObject $res + } + Return $res } Export-ModuleMember -Function *-TargetResource