locked
Folder name/filename highest number filter to delete old version RRS feed

  • Question

  • Hello,

    I am working on a script to internally distribute Office updates so that not every single end-device has to update over the internet causing slow speed for others. For this, I am trying to make a script that filters out the older folder version and older .cab file so that only the latest version remains.

    In the above example the files with the old version name, so the folder 16.0.7369.2130 and v32_16.0.7369.2130.cab need to be removed.

    This number is always changing everytime a new update comes in so I can't use the static numbers for removal by script.. I hope you guys have an idea on how I can do this.

    Friday, July 7, 2017 12:44 PM

Answers

  • Hello,

    I am working on a script to internally distribute Office updates so that not every single end-device has to update over the internet causing slow speed for others. For this, I am trying to make a script that filters out the older folder version and older .cab file so that only the latest version remains.

    In the above example the files with the old version name, so the folder 16.0.7369.2130 and v32_16.0.7369.2130.cab need to be removed.

    This number is always changing everytime a new update comes in so I can't use the static numbers for removal by script.. I hope you guys have an idea on how I can do this.

    Thought i'd post my final result here for others:

    $ofs = '.'
    
    
    $Cabs = (Get-ChildItem \\path\DataTest\*.cab).Name
    $Folders = (Get-ChildItem \\path\DataTest\ -Directory)
    
    $CabsSplitArray = New-Object System.Collections.ArrayList
    $FoldersSplitArray = New-Object System.Collections.ArrayList
    
    
    $Cabs | % {
    		If ($_.toString().Split('.').Length -eq 5)
    		{
    			$CabsSplitArray.Add($_.toString().Split('.')) | Out-Null
    		}
    	}
    $Folders | % {
    		If ($_.toString().Split('.').Length -eq 4)
    		{
    			$FoldersSplitArray.Add($_.toString().Split('.')) | Out-Null
    		}
    	}
    
    $FolderToKeep = [String]($FoldersSplitArray | ? { $_[2] -eq ($FoldersSplitArray | % { $_[2] } | Measure-Object -Maximum).Maximum.toString() })
    
    $CabToKeep = [String]($CabsSplitArray | ? { $_[2] -eq ($CabsSplitArray | % { $_[2] } | Measure-Object -Maximum).Maximum.toString() })
    
    "Cab to keep: $CabToKeep"
    "Folder to keep: $FolderToKeep"

    And to e.g. remove all files except the latest versions that are filtered out ($CabToKeep and $FolderToKeep) you could do something like: 

    Get-ChildItem -Path \\path\DataTest | where { $_.Name -ne "$CabToKeep" -and $_.Name -ne "$FolderToKeep"} | Remove-Item -Recurse


    Tuesday, July 11, 2017 9:26 AM

All replies

  • You can start by posting your script.

    Consider using the version parser:

    [version]::Parse('15.1.23.1234')

    https://msdn.microsoft.com/en-us/library/system.version(v=vs.110).aspx


    \_(ツ)_/


    Friday, July 7, 2017 1:02 PM
  • ... or consider using Microsoft Windows Update Services!!  ;-)

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Friday, July 7, 2017 1:07 PM
  • I didn't really have any script yet for it, I started with the simple removal of the filename but then realised that the number will constantly be changing because new versions keep getting released, so I searched the forum here but didn't quite find anything useful, hence my question.
    Friday, July 7, 2017 1:08 PM
  • Windows Update Services does not distribute Office updates any more.  They are delivered directly to Office  using the Office Update

    \_(ツ)_/

    Friday, July 7, 2017 1:09 PM
  • ... or consider using Microsoft Windows Update Services!!  ;-)

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    We already are, but the problem is that alot of colleagues barely ever have their laptop on our local network. This is why we are creating a way where we use the deferred update channel to download the updates to one location, and spread out the latest version to all locations' NAS stations, which then distributes it to the laptops at the location, with the alternative that if for a certain period amount of time they havent been on the local network, internet updates are forced to prevent vulnerabilities, while maintaining as low as possible internet traffic to the datacenter.

    Friday, July 7, 2017 1:15 PM
  • Oh ... oooops ... I didn't notice that ... thanks ... maybe because we're using SCCM and there you can have both - Windows AND Office updates.

    So then ... consider using SCCM!  :-D


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Friday, July 7, 2017 1:15 PM
  • Yes - SCCM and WSUS distribute both.

    If users are not on the net then be sure Office is set to update automatically.  The will update and can do it incrementally.  Updates are designed to work on Laptops that are normally in transit or intermittently connected.


    \_(ツ)_/

    Friday, July 7, 2017 1:23 PM
  • Yes - SCCM and WSUS distribute both.

    If users are not on the net then be sure Office is set to update automatically.  The will update and can do it incrementally.  Updates are designed to work on Laptops that are normally in transit or intermittently connected.


    \_(ツ)_/

    The problem is, is that there is no default pattern in colleagues that are at work/not at work with their laptop. The inconsistency is great with the laptops being connected and not connected and so we want all laptops to have the script run a simple order check (through task scheduler that runs on startup) to first try and get them locally, and only if nessecary through internet. This way we keep them updated and we don't put unneeded load on the internet line.

    Therefore we first retrieve the updates on one server and from there distribute them to a NAS per office location so that IF a laptop is connected to the office its LAN network, it gets the updates from the NAS, preventing the load on the internet line.


    Friday, July 7, 2017 1:33 PM
  • So you are saying they are never connected to the Internet except when in the office?  Why do they need laptops?  IF they only work in the office they would be better off with desktops.

    In any case just use the version class to test version numbers.


    \_(ツ)_/

    Friday, July 7, 2017 1:37 PM
  • So you are saying they are never connected to the Internet except when in the office?  Why do they need laptops?  IF they only work in the office they would be better off with desktops.

    In any case just use the version class to test version numbers.


    \_(ツ)_/

    They are connected to the internet but our policies restrict downloading updates from the internet for users due to prevention of possible issues with some applications. And we also have thinclients in the office, you never heard me say we ONLY work with laptops.

    No offense, but I didn't ask for workspace advise, just needed some guidance for a version check and remove on filenames and .cab files in powershell..

    Friday, July 7, 2017 1:55 PM
  • No offense, but I didn't ask for workspace advise, just needed some guidance for a version check and remove on filenames and .cab files in powershell..

    Which is why I posted the reference to the "version" class.


    \_(ツ)_/

    Friday, July 7, 2017 2:08 PM
  • Hello,

    I am working on a script to internally distribute Office updates so that not every single end-device has to update over the internet causing slow speed for others. For this, I am trying to make a script that filters out the older folder version and older .cab file so that only the latest version remains.

    In the above example the files with the old version name, so the folder 16.0.7369.2130 and v32_16.0.7369.2130.cab need to be removed.

    This number is always changing everytime a new update comes in so I can't use the static numbers for removal by script.. I hope you guys have an idea on how I can do this.

    Thought i'd post my final result here for others:

    $ofs = '.'
    
    
    $Cabs = (Get-ChildItem \\path\DataTest\*.cab).Name
    $Folders = (Get-ChildItem \\path\DataTest\ -Directory)
    
    $CabsSplitArray = New-Object System.Collections.ArrayList
    $FoldersSplitArray = New-Object System.Collections.ArrayList
    
    
    $Cabs | % {
    		If ($_.toString().Split('.').Length -eq 5)
    		{
    			$CabsSplitArray.Add($_.toString().Split('.')) | Out-Null
    		}
    	}
    $Folders | % {
    		If ($_.toString().Split('.').Length -eq 4)
    		{
    			$FoldersSplitArray.Add($_.toString().Split('.')) | Out-Null
    		}
    	}
    
    $FolderToKeep = [String]($FoldersSplitArray | ? { $_[2] -eq ($FoldersSplitArray | % { $_[2] } | Measure-Object -Maximum).Maximum.toString() })
    
    $CabToKeep = [String]($CabsSplitArray | ? { $_[2] -eq ($CabsSplitArray | % { $_[2] } | Measure-Object -Maximum).Maximum.toString() })
    
    "Cab to keep: $CabToKeep"
    "Folder to keep: $FolderToKeep"

    And to e.g. remove all files except the latest versions that are filtered out ($CabToKeep and $FolderToKeep) you could do something like: 

    Get-ChildItem -Path \\path\DataTest | where { $_.Name -ne "$CabToKeep" -and $_.Name -ne "$FolderToKeep"} | Remove-Item -Recurse


    Tuesday, July 11, 2017 9:26 AM