Param(
    [string]
    $ComputerName,

    [switch]
    $WhatIf,

    [switch]
    $Quiet
)

$RemoveITDExpiredFilesAutoParams = @{

}

If ($PSBoundParameters.ContainsKey('ComputerName')) {
    Write-Verbose -Message "ComputerName parameter"
    $RemoveITDExpiredFilesAutoParams += @{ComputerName = $ComputerName }
}
If ($PSBoundParameters.ContainsKey('WhatIf')) {
    Write-Verbose -Message "WhatIf parameter"
    $RemoveITDExpiredFilesAutoParams += @{WhatIf = $true }
}

$StartTime = (Get-Date)

$FilesRemoved = Remove-ITDExpiredFilesAuto @RemoveITDExpiredFilesAutoParams -Verbose -Credential $Secret:itdsccmsrvcpiandgov

# get information for notes
$DiskBytesRecovered = ($FilesRemoved | Measure-Object -Sum length).Sum
$Notes = "PSComputerName~Length~FullName" + "`n"
$Notes += ForEach ($File in $FilesRemoved) {
    If ($File) {
        $File.PSComputerName + "~" + $File.Length + "~" + $File.FullName + "`n"
    }
}
$Notes += "$DiskBytesRecovered bytes of disk saved."
Write-Verbose -Message "Notes: `n$Notes" -Verbose

$EndTime = (Get-Date)

If ($PSBoundParameters.ContainsKey('Quiet') -and $Quiet -eq $true) {
    Write-Verbose -Message "Quiet mode enabled.  No ServiceNow interactions will be done." -Verbose
}
Else {
    Write-Verbose -Message "Quiet mode disabled.  ServiceNow CHG will be generated."
    # create std chg and close it
    New-ITDServiceNowSession Test -Credential $Secret:SNowVMCred
    $NewITDServiceNowChangeRequestParams = @{
        TemplateName              = 'NDIT-SPS-Server Add/Chg/Del'
        RequestedByUsername       = 'zmeier';
        Category                  = 'Systems Platforms - Systems';
        Subcategory               = 'Windows';
        Impact                    = 3;
        ShortDescription          = "Remove files flagged for expiration and cleanup - Remove-ITDExpiredFilesAuto_script - $UAJobId";
        Description               = "Remove files flagged for expiration and cleanup";
        Justification             = "Some files are generated on a recurring basis causing increase in disk space usage.  This automation removes specific file types from specified file paths that have been flagged for removal.";
        Implementation            = "PSUniversal execution";
        RiskImpactAnalysis        = "Low, files can be discovered before the removal";
        BackoutPlan               = "Restore from backup (if applicable)"
        TestPlan                  = "n/a"
        WhoIsImpacted             = "Windows System Administrators";
        StartTime                 = $StartTime
        EndTime                   = $EndTime;
        AssignmentGroup           = 'NDIT-Computer Systems Windows';
        ChangeManagerUsername     = 'khellman';
        ChangeCoordinatorUsername = 'gpgolberg';
        AssignedToUsername        = 'zmeier';
    }
    If ($PSBoundParameters.ContainsKey('WhatIf')) {
        $NewITDServiceNowChangeRequestParams.ShortDescription += " -WhatIf"
    }

    $CHG = New-ITDServiceNowChangeRequest @NewITDServiceNowChangeRequestParams

    Update-ITDServiceNowRecord -ItemType "Change Request" -Number $CHG.Number.Value -Values @{
        work_notes = $Notes;
    }

    Complete-ITDServiceNowChangeRequest -Number $CHG.Number.value -CloseCode "Successful" -CloseNotes "Files removed."
}

