|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Retrieves the current date and time from a SQL Server instance. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Retrieves the current date and time from a SQL Server instance using the |
| 7 | + specified T-SQL date/time function. This command helps eliminate clock-skew |
| 8 | + and timezone issues when coordinating time-sensitive operations between the |
| 9 | + client and SQL Server. |
| 10 | +
|
| 11 | + The command queries SQL Server using the server connection context, which |
| 12 | + does not require any database to be online. |
| 13 | +
|
| 14 | + .PARAMETER ServerObject |
| 15 | + Specifies current server connection object. |
| 16 | +
|
| 17 | + .PARAMETER DateTimeFunction |
| 18 | + Specifies which T-SQL date/time function to use for retrieving the date and time. |
| 19 | + Valid values are: |
| 20 | + - `SYSDATETIME` (default): Returns datetime2(7) with server local time |
| 21 | + - `SYSDATETIMEOFFSET`: Returns datetimeoffset(7) with server local time and timezone offset |
| 22 | + - `SYSUTCDATETIME`: Returns datetime2(7) with UTC time |
| 23 | + - `GETDATE`: Returns datetime with server local time |
| 24 | + - `GETUTCDATE`: Returns datetime with UTC time |
| 25 | +
|
| 26 | + .PARAMETER StatementTimeout |
| 27 | + Specifies the query StatementTimeout in seconds. Default 600 seconds (10 minutes). |
| 28 | +
|
| 29 | + .INPUTS |
| 30 | + `Microsoft.SqlServer.Management.Smo.Server` |
| 31 | +
|
| 32 | + Accepts input via the pipeline. |
| 33 | +
|
| 34 | + .OUTPUTS |
| 35 | + `System.DateTime` |
| 36 | +
|
| 37 | + Returns the current date and time from the SQL Server instance. |
| 38 | +
|
| 39 | + .EXAMPLE |
| 40 | + $serverObject = Connect-SqlDscDatabaseEngine |
| 41 | + Get-SqlDscDateTime -ServerObject $serverObject |
| 42 | +
|
| 43 | + Connects to the default instance and retrieves the current date and time |
| 44 | + using the default SYSDATETIME function. |
| 45 | +
|
| 46 | + .EXAMPLE |
| 47 | + $serverObject = Connect-SqlDscDatabaseEngine |
| 48 | + $serverObject | Get-SqlDscDateTime -DateTimeFunction 'SYSUTCDATETIME' |
| 49 | +
|
| 50 | + Connects to the default instance and retrieves the current UTC date and time |
| 51 | + from the SQL Server instance. |
| 52 | +
|
| 53 | + .EXAMPLE |
| 54 | + $serverObject = Connect-SqlDscDatabaseEngine |
| 55 | + $serverTime = Get-SqlDscDateTime -ServerObject $serverObject |
| 56 | + Restore-SqlDscDatabase -ServerObject $serverObject -Name 'MyDatabase' -StopAt $serverTime.AddHours(-1) |
| 57 | +
|
| 58 | + Demonstrates using the server's clock for a point-in-time restore operation, |
| 59 | + avoiding clock skew issues between client and server. |
| 60 | +#> |
| 61 | +function Get-SqlDscDateTime |
| 62 | +{ |
| 63 | + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')] |
| 64 | + [OutputType([System.DateTime])] |
| 65 | + [CmdletBinding()] |
| 66 | + param |
| 67 | + ( |
| 68 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 69 | + [Microsoft.SqlServer.Management.Smo.Server] |
| 70 | + $ServerObject, |
| 71 | + |
| 72 | + [Parameter()] |
| 73 | + [ValidateSet('SYSDATETIME', 'SYSDATETIMEOFFSET', 'SYSUTCDATETIME', 'GETDATE', 'GETUTCDATE')] |
| 74 | + [System.String] |
| 75 | + $DateTimeFunction = 'SYSDATETIME', |
| 76 | + |
| 77 | + [Parameter()] |
| 78 | + [ValidateNotNull()] |
| 79 | + [System.Int32] |
| 80 | + $StatementTimeout = 600 |
| 81 | + ) |
| 82 | + |
| 83 | + process |
| 84 | + { |
| 85 | + Write-Verbose -Message ( |
| 86 | + $script:localizedData.Get_SqlDscDateTime_RetrievingDateTime -f $DateTimeFunction |
| 87 | + ) |
| 88 | + |
| 89 | + $query = "SELECT $DateTimeFunction()" |
| 90 | + |
| 91 | + $invokeSqlDscScalarQueryParameters = @{ |
| 92 | + ServerObject = $ServerObject |
| 93 | + Query = $query |
| 94 | + StatementTimeout = $StatementTimeout |
| 95 | + ErrorAction = 'Stop' |
| 96 | + Verbose = $VerbosePreference |
| 97 | + } |
| 98 | + |
| 99 | + try |
| 100 | + { |
| 101 | + $result = Invoke-SqlDscScalarQuery @invokeSqlDscScalarQueryParameters |
| 102 | + |
| 103 | + # Convert the result to DateTime if it's a DateTimeOffset |
| 104 | + if ($result -is [System.DateTimeOffset]) |
| 105 | + { |
| 106 | + $result = $result.DateTime |
| 107 | + } |
| 108 | + |
| 109 | + return $result |
| 110 | + } |
| 111 | + catch |
| 112 | + { |
| 113 | + $writeErrorParameters = @{ |
| 114 | + Message = $script:localizedData.Get_SqlDscDateTime_FailedToRetrieve -f $DateTimeFunction, $_.Exception.Message |
| 115 | + Category = 'InvalidOperation' |
| 116 | + ErrorId = 'GSDD0001' # cSpell: disable-line |
| 117 | + TargetObject = $DateTimeFunction |
| 118 | + Exception = $_.Exception |
| 119 | + } |
| 120 | + |
| 121 | + Write-Error @writeErrorParameters |
| 122 | + |
| 123 | + return |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments