Set-UpdatesDate.ps1
|
function Set-UpdatesDate { <# .SYNOPSIS Inserts or updates the update date of a specified file in the database. .DESCRIPTION This function adds an entry for a given file name to the 'updates' table in the database. If the entry already exists, it updates the specified date column with the current timestamp. .PARAMETER ConnectionName The name of the SQL connection. This parameter is mandatory and must not be empty. .PARAMETER Filename The name of the file to update in the 'updates' table. This parameter is mandatory and must not be empty. .PARAMETER DateType Specifies whether to update the 'startdatum' or 'enddatum' column. Accepted values: 'Start', 'End'. .EXAMPLE Set-UpdatesDate -ConnectionName 'StrandmausDB' -Filename 'AzsharaLog.txt' -DateType 'Start' Updates the start date for the file 'AzsharaLog.txt' in the database 'StrandmausDB'. #> param ( [Parameter(Mandatory, Position = 0, HelpMessage = 'The name of the SQL connection.')] [ValidateNotNullOrEmpty()] [string]$ConnectionName, [Parameter(Mandatory, Position = 1, HelpMessage = 'The name of the file to update in the database.')] [ValidateNotNullOrEmpty()] [string]$Filename, [Parameter(Mandatory, Position = 2, HelpMessage = 'The type of date to update: Start or End.')] [ValidateSet('Start','End')] [string]$DateType ) $Date = Get-Date -Format 'yyyy.MM.dd HH:mm:ss' if ($DateType -eq 'Start') { $Column = 'date_start' } else { $Column = 'date_end' } $Query = "INSERT INTO updates (file, $Column) VALUES ('$Filename', '$Date') ON DUPLICATE KEY UPDATE $Column = '$Date';" $null = Invoke-SqlUpdate -ConnectionName $ConnectionName -Query $Query } |