I am running a powershell script for 'dropping few databases' that are not in sync in my secondary server. (please refer the code snippet below)
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True)] $DatabaseName
)
$serverInstanceP = "SQLServerTest"
Write-Host "Primary Server Instance is:"$serverInstanceP
#Validating whether the database name is empty string or `NULL` value
if (![string]::IsNullOrEmpty($DatabaseName)) {
#Validating the presence of any special charater mentioned in the database name except "_" (uderscore)
if ($DatabaseName -notmatch "[^a-zA-Z0-9_]") {
Write-Host "A valid database has been provided, which is not null, nor an empty string neither contain any special character"
}
else {
Write-Warning "Database name should not contain any special character"
}
}
else {
Write-Warning "Database name cannot be NULL"
}
Write-Host $DatabaseName "is the current database"
$syncQuery = "
:Connect SQLServerTest
USE [master]
GO
DROP DATABASE [$($DatabaseName)];
GO "
Invoke-Sqlcmd -ServerInstance $serverInstanceP -Database master -Query $syncQuery -QueryTimeout 1200
While running the above script it is giving me an error 'ParserError: (:) [Invoke-Sqlcmd], ParserException'.
Especially ':Connect SQLServerTest' Do I need to send my query through @' '@
But when I run the same query on the database server through SQLCMD, it is working fine.