Patch/Cmdlets/SQL/Test-NAVDatabaseOnSqlInstance.ps1
<#
.SYNOPSIS Verify that the database exists on provided SQL Server instance. .DESCRIPTION Verify that the database exists on provided SQL Server instance. .PARAMETER DatabaseServer Specifies the SQL Server database server. .PARAMETER DatabaseInstance Specifies the SQL Server instance. .PARAMETER DatabaseName Specifies the database name. #> function Test-NAVDatabaseOnSqlInstance { [CmdletBinding()] param ( [parameter(Mandatory=$true)] [string]$DatabaseServer = "localhost", [parameter(Mandatory=$false)] [string]$DatabaseInstance = "", [parameter(Mandatory=$true)] [string]$DatabaseName ) PROCESS { $CurrentLocation = Get-Location try { if($DatabaseServer -eq '.') { $DatabaseServer = "localhost" } $SqlServerInstance = Get-SqlServerInstance -DatabaseServer $DatabaseServer -DatabaseInstance $DatabaseInstance $databaseExistsQuery = Invoke-Sqlcmd "IF EXISTS (SELECT 1 FROM sys.databases WHERE name = '$DatabaseName') SELECT 1 AS res ELSE SELECT 0 AS res;" -ServerInstance $SqlServerInstance if($databaseExistsQuery.res -eq 0) { return $false } return $true } finally { Set-Location $CurrentLocation } } } Export-ModuleMember -Function Test-NAVDatabaseOnSqlInstance |