|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Tests if a database exists on a SQL Server Database Engine instance. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + This command tests if a database exists on a SQL Server Database Engine instance. |
| 7 | +
|
| 8 | + .PARAMETER ServerObject |
| 9 | + Specifies current server connection object. |
| 10 | +
|
| 11 | + .PARAMETER Name |
| 12 | + Specifies the name of the database to test for existence. |
| 13 | +
|
| 14 | + .PARAMETER Refresh |
| 15 | + Specifies that the **ServerObject**'s databases should be refreshed before |
| 16 | + testing the database existence. This is helpful when databases could have been |
| 17 | + modified outside of the **ServerObject**, for example through T-SQL. But |
| 18 | + on instances with a large amount of databases it might be better to make |
| 19 | + sure the **ServerObject** is recent enough. |
| 20 | +
|
| 21 | + .EXAMPLE |
| 22 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 23 | + $serverObject | Test-SqlDscIsDatabase -Name 'MyDatabase' |
| 24 | +
|
| 25 | + Tests if the database named **MyDatabase** exists on the instance. |
| 26 | +
|
| 27 | + .EXAMPLE |
| 28 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 29 | + Test-SqlDscIsDatabase -ServerObject $serverObject -Name 'MyDatabase' -Refresh |
| 30 | +
|
| 31 | + Tests if the database named **MyDatabase** exists on the instance, refreshing |
| 32 | + the server object's databases collection first. |
| 33 | +
|
| 34 | + .OUTPUTS |
| 35 | + `[System.Boolean]` |
| 36 | +
|
| 37 | + Returns `$true` if the target object is a database; otherwise, `$false`. |
| 38 | +
|
| 39 | + .INPUTS |
| 40 | + `[Microsoft.SqlServer.Management.Smo.Server]` |
| 41 | +
|
| 42 | + The server object can be provided via the pipeline to **ServerObject**. |
| 43 | +#> |
| 44 | +function Test-SqlDscIsDatabase |
| 45 | +{ |
| 46 | + [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.')] |
| 47 | + [OutputType([System.Boolean])] |
| 48 | + [CmdletBinding()] |
| 49 | + param |
| 50 | + ( |
| 51 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 52 | + [Microsoft.SqlServer.Management.Smo.Server] |
| 53 | + $ServerObject, |
| 54 | + |
| 55 | + [Parameter(Mandatory = $true)] |
| 56 | + [ValidateNotNullOrEmpty()] |
| 57 | + [System.String] |
| 58 | + $Name, |
| 59 | + |
| 60 | + [Parameter()] |
| 61 | + [System.Management.Automation.SwitchParameter] |
| 62 | + $Refresh |
| 63 | + ) |
| 64 | + |
| 65 | + process |
| 66 | + { |
| 67 | + Write-Verbose -Message ($script:localizedData.IsDatabase_Test -f $Name, $ServerObject.InstanceName) |
| 68 | + |
| 69 | + # Check if database exists using Get-SqlDscDatabase |
| 70 | + $sqlDatabaseObject = Get-SqlDscDatabase -ServerObject $ServerObject -Name $Name -Refresh:$Refresh -ErrorAction 'SilentlyContinue' |
| 71 | + |
| 72 | + if ($sqlDatabaseObject) |
| 73 | + { |
| 74 | + return $true |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + return $false |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments