Skip to content

Commit

Permalink
Add Remove-Packages function (#9)
Browse files Browse the repository at this point in the history
* add remove-packages and bump version

* remove on branch main

* update docs
  • Loading branch information
w0 authored Jul 14, 2024
1 parent 4a29a6f commit ad6d833
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ on:
push:
tags:
- '*'
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
Expand Down
154 changes: 154 additions & 0 deletions docs/Remove-Packages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
---
external help file: OpenDriverTool-help.xml
Module Name: OpenDriverTool
online version:
schema: 2.0.0
---

# Remove-Packages

## SYNOPSIS
Removes old driver packages from your SCCM site.

## SYNTAX

```
Remove-Packages [-Make] <String> [-PackageType] <String> [-SiteCode] <String> [-SiteServerFQDN] <String>
[-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm] [<CommonParameters>]
```

## DESCRIPTION
{{ Fill in the Description }}

## EXAMPLES

### Example 1
```powershell
PS C:\> Remove-Packages -Make Dell -PackageType Driver -SiteCode CAS -SiteServerFQDN abc.contoso.com
```

Will remove all old driver packages from the specified sccm site. It leaves only the latest driver package.

## PARAMETERS

### -Confirm
Prompts you for confirmation before running the cmdlet.

```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Make
Manufacturer of the computer.
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Accepted values: Dell, Microsoft

Required: True
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -PackageType
Type of package to clean up and remove.
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Accepted values: Drivers, BIOS Update

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -SiteCode
Specify the sitecode of the confimgr site to connect to.
```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -SiteServerFQDN
Specify the site server fqdn to connect to.
```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -WhatIf
Shows what would happen if the cmdlet runs.
The cmdlet is not run.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -ProgressAction
{{ Fill ProgressAction Description }}
```yaml
Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## INPUTS
### None
## OUTPUTS
### System.Object
## NOTES
## RELATED LINKS
2 changes: 1 addition & 1 deletion source/OpenDriverTool.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'OpenDriverTool.psm1'

# Version number of this module.
ModuleVersion = '0.5.0'
ModuleVersion = '0.5.1'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
56 changes: 56 additions & 0 deletions source/public/Remove-Packages.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function Remove-Packages {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
param (
[Parameter(Mandatory)]
[ValidateSet('Dell', 'Microsoft')]
[string]
$Make,

[Parameter(Mandatory)]
[ValidateSet('Drivers', 'BIOS Update')]
[string]
$PackageType,

[Parameter(Mandatory)]
[string]
$SiteCode,

[Parameter(Mandatory)]
[string]
$SiteServerFQDN
)

$SearchString = '{0} - {1} *' -f $PackageType, $Make

Write-Debug -Message $SearchString

Connect-SCCM -SiteCode $SiteCode -SiteServerFQDN $SiteServerFQDN

Push-Location ('{0}:' -f $SiteCode)

$QueryResult = Get-CMPackage -Name $SearchString -Fast

$CMPackages = $QueryResult | Group-Object Name | Where-Object Count -gt 1

foreach ($Group in $CMPackages) {

$PkgsToDelete = $Group.Group | Sort-Object -Property SourceDate | Select-Object -First ($Group.Count - 1)

foreach ($Package in $PkgsToDelete) {
if ($PSCmdlet.ShouldProcess("$($Package.Name) Version: $($Package.Version)", "Remove Package")) {

Remove-CMPackage -InputObject $Package -Force

Pop-Location

if (Test-Path $Package.PkgSourcePath) {
Remove-Item -Path $Package.PkgSourcePath -Recurse -Force
}

Push-Location ('{0}:' -f $SiteCode)
}
}
}

Pop-Location
}

0 comments on commit ad6d833

Please sign in to comment.