functions/set-d365sdpcleanup.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

<#
    .SYNOPSIS
        Set the cleanup retention period
         
    .DESCRIPTION
        Sets the configured retention period before updates are deleted
         
    .PARAMETER NumberOfDays
        Number of days that deployable software packages should remain on the server
         
    .EXAMPLE
        PS C:\> Set-D365SDPCleanUp -NumberOfDays 10
         
        This will set the retention period to 10 days inside the the registry
         
        The cmdlet REQUIRES elevated permissions to run, otherwise it will fail
         
    .NOTES
        This cmdlet is based on the findings from Alex Kwitny (@AlexOnDAX)
         
        See his blog for more info:
        http://www.alexondax.com/2018/04/msdyn365fo-how-to-adjust-your.html
         
        Author: Mötz Jensen (@Splaxi)
         
#>

function Set-D365SDPCleanUp {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    param (
        [int] $NumberOfDays = 30
    )

    if (-not ($Script:IsAdminRuntime)) {
        Write-PSFMessage -Level Host -Message "It seems that you ran this cmdlet <c='em'>non-elevated</c>. Making changes to the registry requires you to run this cmdlet from an elevated console. Please exit the current console and start a new with `"Run As Administrator`""
        Stop-PSFFunction -Message "Stopping because of missing parameters"
        return
    }

    Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Dynamics\Deployment" -Name "CutoffDaysForCleanup" -Type STRING -Value "$NumberOfDays" -Force
}