I'm working on a PowerShell script to run a query against multiple servers and databases were the idea is to dynamically add server and databases to an array and execute them.
Currently I'm stuck at the last part where everything is combined. I can add the servers but not the databases.
What I am trying to achieve: PowerShell script with MFP GUI to run a query against multiple MSSQL servers which all contain identical database (with different data) but the databases have different names like Sql_Data-Node1, SqlData-Node2, etc.
Problem I encounter: I managed to add the servers dynamically to an array and when I run a query I get the proper response. In this case I used the master database an I made it static (-database 'master'). When I try to do the same with the databases (add them to an array) I get an error:
Invoke-Sqlcmd : Cannot validate argument on parameter 'Database'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Users\master\Documents\MULTSCRIPT\MultiQueryV0.6.ps1:368 char:109
+ ... ame -Password $PassWord -ServerInstance $_[0] -Database $_[1] -Query ...
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Sqlcmd], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
My code:
#Create EMPTY ARRAY for Databases
$script:DBSet = New-Object System.Collections.ArrayList
#==========================================================================
$window.Master.add_Checked({
$script:Master = 'master' #Add IP to Variable
$script:DBSet.Add("$script:Master") #Add Variable to Array
[System.Object]$sender = $args[0]
[System.Windows.RoutedEventArgs]$e = $args[1]
})
$window.Master.add_Unchecked({
$script:Master = $null
$script:DBSet.Remove("$script:Master")
[System.Object]$sender = $args[0]
[System.Windows.RoutedEventArgs]$e = $args[1]
})
#==========================================================================
$window.DataNodes.add_Checked({
$script:DB01 = 'Database01'
$script:DBSet.Add("$script:DB01")
[System.Object]$sender = $args[0]
[System.Windows.RoutedEventArgs]$e = $args[1]
})
$window.DataNodes.add_Unchecked({
$script:DB01 = $null
$script:DBSet.Remove("$script:DB01")
[System.Object]$sender = $args[0]
[System.Windows.RoutedEventArgs]$e = $args[1]
})
#Create EMPTY ARRAY For Servers
$script:ServerAddress = New-Object System.Collections.ArrayList
#Add action to Checkbox====================================================
$window.DB00.add_Checked({
$script:SRV00 = '190.168.1.8' #Add IP to Variable
$script:ServerAddress.Add("$script:SRV00") #Add Variable to Array
[System.Object]$sender = $args[0]
[System.Windows.RoutedEventArgs]$e = $args[1]
})
$window.DB00.add_Unchecked({
$script:SRV00 = $null
$script:ServerAddress.Remove("$script:SRV00") #Remove Variable to Array
[System.Object]$sender = $args[0]
[System.Windows.RoutedEventArgs]$e = $args[1]
})
#==========================================================================
$window.DB01.add_Checked({
$script:SRV01 = '192.168.1.9'
$script:ServerAddress.Add("$script:SRV01")
[System.Object]$sender = $args[0]
[System.Windows.RoutedEventArgs]$e = $args[1]
})
$window.DB01.add_Unchecked({
$script:SRV01 = $null
$script:ServerAddress.Remove("$script:SRV01")
[System.Object]$sender = $args[0]
[System.Windows.RoutedEventArgs]$e = $args[1]
})
#Collect Credentials#======================================================
$credential = Get-Credential
$UserName = $credential.UserName.Replace('\','')
$PassWord = $credential.GetNetworkCredential().password
#Collect From Input Fields#================================================
$window.Button.add_Click({
$SQLQuery = $window.Query.Text.ToString()
$Server = $script:ServerAddress
$DatabaseSet = $script:DBSet
$instances = #( #($Server, $DatabaseSet) )
$instances | ForEach{
Invoke-Sqlcmd -AbortOnError `
-Username $UserName`
-Password $PassWord`
-ServerInstance $_[0]`
-Database $_[1]`
-Query $SQLQuery`
-QueryTimeout 30 |
Out-GridView -Title $_[0]
}
[System.Object]$sender = $args[0]
[System.Windows.RoutedEventArgs]$e = $args[1]
})
It seems that the following solution works.
Credit goes to:Mutiple Variables in Foreach Loop [Powershell]
$window.Button.add_Click(
{ $DataBase = $window.DataBase.Text.ToString()
$SQLQuery = $window.Query.Text.ToString()
$Server = $ServerAddress.getenumerator()
$Database = $DBSet.getenumerator()
while($Server.MoveNext() -and $Database.MoveNext()){
Invoke-Sqlcmd -AbortOnError -Username $UserName -Password $PassWord -ServerInstance $Server.Current -Database $Database.Current -Query $SQLQuery -QueryTimeout 30 | Out-GridView -Title $Server.Current}
Related
I am running 2 different sql query one after the other to get the details from database using Invoke-Sqlcmd but getting an exception.
The WriteObject and WriteError methods cannot be called from outside the overrides of the BeginProcessing, ProcessRecord, and EndProcessing methods, and they can only be called from within the same thread. Validate that the cmdlet makes these calls correctly
Script:
$ok = "true"
$serverName = $env:COMPUTERNAME
$query1 = "SELECT * FROM sys.server_audits WHERE name = 'Login'"
$query2 = "SELECT * FROM sys.dm_server_audit_status WHERE name = 'Login' AND status_desc = 'STARTED'"
try
{
Write-Host "Getting running Sql Server Instance (online)"
$sqlInstances = (Get-Service -Name MSSQL$* | Where-Object { $_.status -eq "Running" }).Name
$sqlInstancesCount = $sqlInstances.Count
Write-Host $eventID "$sqlInstancesCount Sql Server Instance found"
if($sqlInstancesCount)
{
foreach($item in $sqlInstances)
{
$count = 0
$instanceName = $item -replace "MSSQL\$", ""
$sqlInstanceFullname = Join-Path -Path $serverName -ChildPath "\" | Join-Path -ChildPath $instanceName
Write-Host "Sql Server instance fullname - $sqlInstanceFullname"
while($count -lt 3)
{
Write-Host "Clear existing connection."
[System.Data.SqlClient.SqlConnection]::ClearAllPools()
Write-Host "Executing query - $query1"
$result1 = (Invoke-Sqlcmd -Query $query1 -ServerInstance $sqlInstanceFullname)
Start-Sleep -Seconds 30
Write-Host "Clear existing connection."
[System.Data.SqlClient.SqlConnection]::ClearAllPools()
Write-Host "Executing query - $query2"
$result2 = (Invoke-Sqlcmd -Query $query2 -ServerInstance $sqlInstanceFullname)
if($null -eq $result1)
{
$message = "$serverName - The SQL Server [Login] does not exist."
$ok = "false"
$count = $count + 1
}
elseif($null -eq $result2)
{
$message = "$serverName - The SQL Server [Login] is disabled."
$ok = "false"
$count = $count + 1
}
else
{
$message = "SQL Login is enabled and running."
$count = 3
$ok = "true"
}
}
}
}
else
{
$ok = "false"
throw "$serverName - SQL Server Instances might be offline or not exist!"
}
}
catch
{
throw $_
}
I have tried enclosing Invoke-Sqlcmd in brackets but still getting same exception so is there a way to remove existing thread or close the connection before executing another query.
I have the below powershell script which runs from jenkins against windows server 2019 slave:
$sqlpackagepublish = Start-Process -FilePath sqlpackage.exe -ArgumentList '/Action:Publish','/SourceFile:"Database Services\bin\Release\Database Services.dacpac"',"/TargetConnectionString:""Data Source=${Env};Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False;Initial catalog=${Target}""","/p:BlockOnPossibleDataLoss=${Data_loss}" -wait -PassThru -Credential $Cred -RedirectStandardOutput sqlstdout.txt -RedirectStandardError sqlstderr.txt
$sqlpackagepublish.WaitForExit()
$sqlpackagepublish
if ($sqlpackagepublish.ExitCode -eq 0) {
Get-Content sqlstdout.txt
}
else {
echo "An error occurred"
Get-Content sqlstderr.txt
exit $sqlpackagepublish.ExitCode
}
But the deploy fails with no error in sqlstderr.txt and no info in jenkins log. any idea how I can debug it?
Update
based on the suggested answer below, I've tried both approaches:
1.
Remove a -PassThru parameter and read files' content.
So I changed my code the the below:
$sqlpackagepublish = Start-Process -FilePath sqlpackage.exe -ArgumentList '/Action:Publish','/SourceFile:"Database Services\bin\Release\Database Services.dacpac"',"/TargetConnectionString:""Data Source=${Env};Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False;Initial catalog=${Target}""","/p:BlockOnPossibleDataLoss=${Data_loss}" -wait -Credential $Cred
$sqlpackagepublish.WaitForExit()
$sqlpackagepublish
But now I'm getting:
You cannot call a method on a null-valued expression.
+ $sqlpackagepublish.WaitForExit()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
robust .NET'y way
In my original question, I had these lines:
$Username = $args[0]
$Password = $args[1]
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
$sqlpackagepublish = Start-Process -FilePath sqlpackage.exe -ArgumentList {args} -wait -PassThru -Credential $Cred
I didn't understand how to add it to your code
This is how Start-Process command was basically created. -PassThru switch redirects the output to an object ($sqlpackagepublish in this case).
More on Start-Process here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-5.1
There are few solutions.
Remove a -PassThru parameter and read files' content as you are doing it right now
Do it harder, but more robust .NET'y way:
function Start-ProcessExecution
{
param (
[string] $exe,
[string] $arguments,
[string] $user,
[string] $password
)
$startInfo = New-Object System.Diagnostics.ProcessStartInfo;
$pass = ConvertTo-SecureString -AsPlainText $password -Force
$startInfo.UserName = "$user";
$startInfo.Password = "$pass";
$startInfo.FileName = $exe;
$startInfo.Arguments = $arguments;
$startInfo.UseShellExecute = $false;
$startInfo.RedirectStandardOutput = $true;
$startInfo.RedirectStandardError = $true;
$process = New-Object System.Diagnostics.Process;
$process.StartInfo = $startInfo;
$process.Start() | Out-Null;
$output = $process.StandardOutput.ReadToEnd();
$err = $process.StandardError.ReadToEnd();
$process.WaitForExit();
$obj = [PSCustomObject]#{
ExitCode = $process.ExitCode
StdOut = $output
StdErr = $err
}
return $obj;
}
$exe = "sqlpackage.exe"
$arguments = [string]::Join(" ", "/Action:Publish", `
'/SourceFile:"Database Services\bin\Release\Database Services.dacpac"', `
'/TargetConnectionString:"Data Source=${Env};Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False;Initial catalog=${Target}"', `
'/p:BlockOnPossibleDataLoss=${Data_loss}')
$result = (Start-ProcessExecution -exe $exe -arguments $arguments -user $args[0] -password $args[1])
$ec = $result.ExitCode;
if ($ec -eq 0) {
Write-Host "STDOUT:`n$($result.StdOut)";
}
else {
Write-Host "STDERR:`n$($result.StdErr)";
}
Wrapper is basically there to better readability and is constructed to prevent the deadlock on reading huge outputs after the WaitForExit() has been called.
I have used the below code for a little while, mainly as a test to understand it a bit more. What I would really like to do now is improve it's speed by using bulk copy.
The code gets database and table information from a series of instances which are held in a text file on a server, it then adds the collected data via invoke into a table.
The issue is it sends the data for every database, and every table one at a time, if at all possible.
I have looked at a number of sites/blogs that give information on this, but they all seem to be not quite what I need.
$Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$today = Get-Date
$srvlist = #(Get-Content "c:\scripts\tablegrowth.TXT")
foreach ($server in $srvlist) {
$srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $server
$dbs = $srv.Databases
foreach ($db in $dbs) {
if ($db.IsAccessible) {
$name1 = $db.name
$size1 = $db.size
$dbspace1 = $db.DataSpaceUsage/1KB
$dbindexsp1 = $db.IndexSpaceUsage/1KB
$dbspaceavail1 = $db.SpaceAvailable/1KB
#Write-Host "dbname=" $name1 $size1 $dbspace1 $dbindexsp1 $dbspaceavail1 $db.IsAccessible
switch ($name1) {
'master' {}
'msdb' {}
'model' {}
'SSISDB' {}
'SSRSData' {}
'Northwind' {}
'tempdb' {}
default {
Invoke-Sqlcmd -ServerInstance "******" -Database "DBAMonitoring" -Query "INSERT INTO is_sql_databases VALUES ('$server','$name1','$today',$dbspace1,$dbspaceavail1,$dbindexsp1) "
foreach ( $tbl in $db.tables) {
$tname1 = $tbl.Name
$tindexsp1 = $tbl.IndexSpaceUsed/1KB
$trows1 = $tbl.RowCount
$tspace1 = $tbl.DataSpaceUsed/1KB
#Write-Host "tbl name=" $tname1 $tindexsp1 $trows $tspace1
if ($trows1 -gt 500) {
Invoke-Sqlcmd -ServerInstance "****" -Database "DBAMonitoring" - Query "INSERT INTO is_sql_tables VALUES ('$server','$name1','$tname1','$today',$trows1,$tspace1,$tindexsp1) "
} ####end RowCount > 0
} #####end Row loop
} ###### end Default
} #####end switch
} #####end IsAccessible
} #####end database loop
} ###end server loop
$Stopwatch.Stop()
$Stopwatch.Elapsed.TotalSeconds
$Stopwatch.Elapsed.TotalMinutes
One way is to load the rows into a DataTable and use .NET SqlBulkCopy directly in the script to bulk insert. Just change the DataTable column names and data types in the below example to match those of your actual table.
$dt = New-Object System.Data.DataTable;
[void]($dt.Columns.Add("server_name", [System.Type]::GetType("System.String")).MaxLength = 128)
[void]($dt.Columns.Add("name1", [System.Type]::GetType("System.String")).MaxLength = 128)
[void]($dt.Columns.Add("tname1", [System.Type]::GetType("System.String")).MaxLength = 128)
[void]($dt.Columns.Add("today", [System.Type]::GetType("System.DateTime")))
[void]($dt.Columns.Add("trows1", [System.Type]::GetType("System.Int64")))
[void]($dt.Columns.Add("tspace1", [System.Type]::GetType("System.Int64")))
[void]($dt.Columns.Add("tindexsp1", [System.Type]::GetType("System.Int64")))
$Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$today = Get-Date
$srvlist = #(Get-Content "c:\scripts\tablegrowth.TXT")
foreach ($server in $srvlist) {
$srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $server
$dbs = $srv.Databases
foreach ($db in $dbs) {
if ($db.IsAccessible) {
$name1 = $db.name
$size1 = $db.size
$dbspace1 = $db.DataSpaceUsage/1KB
$dbindexsp1 = $db.IndexSpaceUsage/1KB
$dbspaceavail1 = $db.SpaceAvailable/1KB
#Write-Host "dbname=" $name1 $size1 $dbspace1 $dbindexsp1 $dbspaceavail1 $db.IsAccessible
switch ($name1) {
'master' {}
'msdb' {}
'model' {}
'SSISDB' {}
'SSRSData' {}
'Northwind' {}
'tempdb' {}
default {
# Invoke-Sqlcmd -ServerInstance "******" -Database "DBAMonitoring" -Query "INSERT INTO is_sql_databases VALUES ('$server','$name1','$today',$dbspace1,$dbspaceavail1,$dbindexsp1) "
foreach ( $tbl in $db.tables) {
$tname1 = $tbl.Name
$tindexsp1 = $tbl.IndexSpaceUsed/1KB
$trows1 = $tbl.RowCount
$tspace1 = $tbl.DataSpaceUsed/1KB
#Write-Host "tbl name=" $tname1 $tindexsp1 $trows $tspace1
if ($trows1 -gt 500) {
# Invoke-Sqlcmd -ServerInstance "." -Database "tempdb" -Query "INSERT INTO is_sql_tables VALUES ('$server','$name1','$tname1','$today',$trows1,$tspace1,$tindexsp1) "
$row = $dt.NewRow()
$dt.Rows.Add($row)
$row["server_name"] = $server
$row["name1"] = $name1
$row["tname1"] = $tname1
$row["today"] = $today
$row["trows1"] = $trows1
$row["tindexsp1"] = $tindexsp1
} ####end RowCount > 0
} #####end Row loop
} ###### end Default
} #####end switch
} #####end IsAccessible
} #####end database loop
} ###end server loop
$bcp = New-Object System.Data.SqlClient.SqlBulkCopy("Data Source=******;Integrated Security=SSPI;Initial Catalog=DBAMonitoring ")
$bcp.DestinationTableName = "dbo.is_sql_tables"
$bcp.WriteToServer($dt);
$Stopwatch.Stop()
$Stopwatch.Elapsed.TotalSeconds
$Stopwatch.Elapsed.TotalMinutes
I want to copy database within the same server to have a test database using the under code but it works fine the first run and then an error occur .I think that was a problem of the name of the destination database because i change the name of destination it works also .How can I proceed to override the destination database without renaming the destination.
Import-Module SQLPS -DisableNameChecking
#your SQL Server Instance Name
$SQLInstanceName = "DESKTOP-444"
$Server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $SQLInstanceName
#provide your database name which you want to copy
$SourceDBName = "test"
#create SMO handle to your database
$SourceDB = $Server.Databases[$SourceDBName]
#create a database to hold the copy of your source database
$CopyDBName = "$($SourceDBName)_copy"
$CopyDB = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Database -ArgumentList $Server , $CopyDBName
$CopyDB.Create()
#Use SMO Transfer Class by specifying source database
#you can specify properties you want either brought over or excluded, when the copy happens
$ObjTransfer = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Transfer -ArgumentList $SourceDB
$ObjTransfer.CopyAllTables = $true
$ObjTransfer.Options.WithDependencies = $true
$ObjTransfer.Options.ContinueScriptingOnError = $true
$ObjTransfer.DestinationDatabase = $CopyDBName
$ObjTransfer.DestinationServer = $Server.Name
$ObjTransfer.DestinationLoginSecure = $true
$ObjTransfer.CopySchema = $true
#if you wish to just generate the copy script
#just script out the transfer
$ObjTransfer.ScriptTransfer()
#When you are ready to bring the data and schema over,
#you can use the TransferData method
$ObjTransfer.TransferData()
I was able to run your code multiple times without any issues. The following is the slightly cleaned-up version (structural changes):
Import-Module SQLPS -DisableNameChecking
$SQLInstanceName = "(local)"
$SourceDBName = "sandbox"
$CopyDBName = "${SourceDBName}_copy"
$Server = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' -ArgumentList $SQLInstanceName
$SourceDB = $Server.Databases[$SourceDBName]
$CopyDB = New-Object -TypeName 'Microsoft.SqlServer.Management.SMO.Database' -ArgumentList $Server , $CopyDBName
$CopyDB.Create()
$ObjTransfer = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Transfer -ArgumentList $SourceDB
$ObjTransfer.CopyAllTables = $true
$ObjTransfer.Options.WithDependencies = $true
$ObjTransfer.Options.ContinueScriptingOnError = $true
$ObjTransfer.DestinationDatabase = $CopyDBName
$ObjTransfer.DestinationServer = $Server.Name
$ObjTransfer.DestinationLoginSecure = $true
$ObjTransfer.CopySchema = $true
$ObjTransfer.ScriptTransfer()
$ObjTransfer.TransferData()
What error did you get?
The one thing I noticed. If the cloned database already exists, the script will fail. You should get an exception up around the $CopyDB.Create() statement and probably another one when you go to copy the objects to the cloned database.
I'd either drop the database if it exists, or abort script execution if it exists.
EDIT
I was told to use the module SqlServer instead of the module SQLPS, because the latter had long been deprecated. And immediately after I have made the change, I noticed that it was now possible to create databases from a Microsoft.SqlServer.Management.SMO.Transfer object, which I was not managing before. I don't understand why, and it might even be unrelated and I was just lucky. The SqlServer package can be installed through the following command:
Install-Module -Name SqlServer -AllowClobber
Thus I am updating my answer with the working code, which is more readable, more elegant and more performant than my previous answer (at the bottom of this post).
$SQLInstanceName = $env:servername
$SourceDBName = $env:databasename
$SQLUser = $env:adminlogin
$SQLPassword = $env:adminPassword
Import-Module SqlServer -DisableNameChecking
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null;
Function IsNullOrEmpty([string]$val){
if ($val -eq $null -or $val -eq '') { $true }
else{ $false }
}
If (IsNullOrEmpty($SQLInstanceName)) {
$SQLInstanceName = $args[0]
}
If (IsNullOrEmpty($SourceDBName)) {
$SourceDBName = $args[1]
}
If (IsNullOrEmpty($SQLUser)) {
$SQLUser = $args[2]
}
If (IsNullOrEmpty($SQLPassword)) {
$SQLPassword = $args[3]
}
Try {
$Server = New-Object Microsoft.SqlServer.Management.Smo.Server($SQLInstanceName)
$DestinationDBName = "${SourceDBName}.Staging"
$SQLSecurePassword = ConvertTo-SecureString $SQLPassword -AsPlainText -Force
$Server.ConnectionContext.LoginSecure = $false
$Server.ConnectionContext.set_Login($SQLUser)
$Server.ConnectionContext.set_SecurePassword($SQLSecurePassword)
$SourceDB = $Server.Databases[$SourceDBName]
$ObjTransfer = New-Object Microsoft.SqlServer.Management.SMO.Transfer ($SourceDB)
$CopyDB = New-Object Microsoft.SqlServer.Management.SMO.Database ($Server, $DestinationDBName)
$CopyDB.Create()
# $ObjTransfer.CopyData = $false - Uncomment this line so that data is not copied across
$ObjTransfer.CopySchema = $true
$ObjTransfer.CopyAllTables = $true
$ObjTransfer.CopyAllDatabaseTriggers = $true
$ObjTransfer.Options.WithDependencies = $true
$ObjTransfer.Options.ContinueScriptingOnError = $true
$ObjTransfer.DestinationDatabase = $DestinationDBName
$ObjTransfer.DestinationServer = $SQLInstanceName
$ObjTransfer.DestinationPassword = $SQLPassword
$ObjTransfer.DestinationLogin = $SQLUser
$ObjTransfer.DestinationLoginSecure = $false
$ObjTransfer.TransferData()
}
Catch [System.Exception] {
# $_ is set to the ErrorRecord of the exception
if ($_.Exception.InnerException) {
Write-Error $_.Exception.InnerException.Message
} else {
Write-Error $_.Exception.Message
}
if($Server.Databases.Name -like $DestinationDBName) {
Write-Host "Dropping cloned database..."
# Call drop-db.ps1 to delete the stagingDB
Invoke-Command { .\drop-db.ps1 $SQLInstanceName $DestinationDBName $SQLUser $SQLPassword }
}
}
Finally {
if($Server) {
$Server.ConnectionContext.Disconnect()
}
}
I was having a similar error implementing this. Tried literally everything, it just wouldn't work. What did work for me, was generating a script through the ScriptTransfer method, create the new database and then apply the script to the new database through Invoke-SqlCmd. The code I am sharing can be invoked locally, by passing 4 arguments to the script in the following order:
Server Name
Database Name
Login
Password
And it can also be used on a pipeline. I am using it on Azure DevOps by setting those 4 arguments through a group variable.
I am appending .Staging to the source database name, and that's the name I give to the new database. If something fails along the way, I delete the new database, in case it has already been created.
$SQLInstanceName = $env:servername
$SourceDBName = $env:databasename
$SQLUser = $env:adminlogin
$SQLPassword = $env:adminPassword
Import-Module SQLPS -DisableNameChecking
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null;
Function IsNullOrEmpty([string]$val){
if ($val -eq $null -or $val -eq '') { $true }
else{ $false }
}
If (IsNullOrEmpty($SQLInstanceName)) {
$SQLInstanceName = $args[0]
}
If (IsNullOrEmpty($SourceDBName)) {
$SourceDBName = $args[1]
}
If (IsNullOrEmpty($SQLUser)) {
$SQLUser = $args[2]
}
If (IsNullOrEmpty($SQLPassword)) {
$SQLPassword = $args[3]
}
Try {
$Server = New-Object Microsoft.SqlServer.Management.Smo.Server($SQLInstanceName)
}
Catch [System.Exception] {
# $_ is set to the ErrorRecord of the exception
if ($_.Exception.InnerException) {
Write-Error $_.Exception.InnerException.Message
} else {
Write-Error $_.Exception.Message
}
}
Finally {
Try {
$StagingDBName = "${SourceDBName}.Staging"
$SQLSecurePassword = ConvertTo-SecureString $SQLPassword -AsPlainText -Force
$Server.ConnectionContext.LoginSecure = $false
$Server.ConnectionContext.set_Login($SQLUser)
$Server.ConnectionContext.set_SecurePassword($SQLSecurePassword)
$CreationScriptOptions = New-Object Microsoft.SqlServer.Management.SMO.ScriptingOptions
$CreationScriptOptions.ExtendedProperties= $true
$CreationScriptOptions.DRIAll= $true
$CreationScriptOptions.Indexes= $true
$CreationScriptOptions.Triggers= $true $CreationScriptOptions.ScriptBatchTerminator = $true
$CreationScriptOptions.IncludeHeaders = $true;
$CreationScriptOptions.ToFileOnly = $true
$CreationScriptOptions.IncludeIfNotExists = $true
$SourceDB = $Server.Databases[$SourceDBName]
$ObjTransfer = New-Object Microsoft.SqlServer.Management.SMO.Transfer ($SourceDB)
$ObjTransfer.options=$CreationScriptOptions # tell the transfer object of our preferences
$FilePath = Join-Path $PSScriptRoot "$($StagingDBName).sql"
$ObjTransfer.Options.Filename = $FilePath;
$ObjTransfer.ScriptTransfer()
$CopyDB = New-Object Microsoft.SqlServer.Management.SMO.Database ($Server, $StagingDBName)
$CopyDB.Create()
$auth=#{UserName=$SQLUser;Password=$SQLPassword}
Invoke-SqlCmd -InputFile $FilePath -ServerInstance $Server -Database $StagingDBName #Auth -Verbose
}
Catch [System.Exception] {
# $_ is set to the ErrorRecord of the exception
if ($_.Exception.InnerException) {
Write-Error $_.Exception.InnerException.Message
} else {
Write-Error $_.Exception.Message
}
if($Server.Databases.Name -like $StagingDBName) {
Write-Host "Dropping staging database..."
$auth=#{UserName=$SQLUser;Password=$SQLPassword}
Invoke-SqlCmd -ServerInstance $Server #Auth `
-Query "IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name ='$($StagingDBName)') `
BEGIN `
ALTER DATABASE [$($StagingDBName)] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; `
DROP DATABASE [$($StagingDBName)]; `
END;" `
-Verbose
}
}
Finally {
$Server.ConnectionContext.Disconnect()
}
}
I am using PowerShell scripts. I have some senior
filter $server and $instance name from .txt file.
Use the $server and $instance in to the 2 function for connect to SQL Server
My .txt file code are as below
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
# Get our list of target servers from the local servers.txt file(Need
$servers = Get-Content 'mirroringserver.txt'
foreach ($prcs in $servers) {
# The first entry in the file is the machine name, the second is the instance name so separate them
#$srvc = $prcs.Split($prcs, "\r\n")
# $ServerName = $prcs
$srvc = $prcs.Split('\')
$servers = $srvc[0]
$instance = $srvc[1]
GetConnectionString $servers $instance
# Ping the machine to see if it's on the network
$results = gwmi -query "select StatusCode from Win32_PingStatus where Address = '$servers'"
$responds = $false
foreach ($result in $results) {
# If the machine responds break out of the result loop and indicate success
if ($result.StatusCode -eq 0) {
$responds = $true
break
}
}
if ($responds) {
# Check to see if a directory exists for this machine, if not create one
if (!(Test-Path -Path .\$servers)) {
New-Item .\$servers\ -Type Directory
}
# Get the server info in the first function and the instance info in the second
#mirroring $servers $instance
getInsertServerStatus $servers $instance
} else {
# Let the user know we couldn't connect to the server
Write-Output "$servers does not respond"
}
}
function GetConnectionString([string]$svr, [string]$inst) {
return "Server=$svr\$inst;Database=master;Integrated Security=True;"
}
This is my function 1:
function mirroring(
$svr,
$inst,
[string] $datastore,
[string] $datastore1,
[string] $datastore2,
[string] $datastore3,
[string] $datastore4,
[string] $datastore5,
[string] $datastore6,
[string] $datastore7
) {
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = GetConnectionString
$SqlConnection.Open()
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = " SELECT db_name(sd.[database_id])AS [Database Name]
,sd.mirroring_state AS [Mirror State Number]
,sd.mirroring_state_desc AS [Mirror State]
,sd.mirroring_partner_name AS [Partner Name]
,sd.mirroring_role_desc AS [Mirror Role]
,sd.mirroring_safety_level_desc AS [Safety Level]
,sd.mirroring_witness_name AS [Witness]
,sd.mirroring_connection_timeout AS [Timeout(sec)]
FROM sys.database_mirroring AS sd
WHERE mirroring_guid IS NOT null
ORDER BY [Database Name];"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0]
$SqlConnection.Close()
$datastore = $DataSet.Tables[0].Rows[0][0]
$datastore1 = $DataSet.Tables[0].Rows[0][1]
$datastore2 = $DataSet.Tables[0].Rows[0][2]
$datastore3 = $DataSet.Tables[0].Rows[0][3]
$datastore4 = $DataSet.Tables[0].Rows[0][4]
$datastore5 = $DataSet.Tables[0].Rows[0][5]
$datastore6 = $DataSet.Tables[0].Rows[0][6]
$datastore7 = $DataSet.Tables[0].Rows[0][7]
$script:ServerStatus1 = "DataBase Name:"+ $datastore+",Mirror State Number"+$datastore1+",Mirror State"+$datastore2+",Partner Name"+$datastore3+",Mirror Role"+$datastore4+",Safety Level"+$datastore5+",Witness"+$datastore6+",Timeout(In Sec)"+$datastore7
return $script:ServerStatus1
}
This is my function 2:
function getInsertServerStatus(
$svr1,
$inst2,
$ServerName = $svr1+"\"+$inst2,
$RemActonToBeTaken = 0,
$ServerStatus
) {
mirroring
$Script:ServerStatus1
$Script:ServerStatus= $ServerStatus1
Write-Host "ServerName=$ServerName"
Write-Host "InstanceName=$inst2"
Write-Host "ServerStatus=$ServerStatus1"
Write-Host "RemActonToBeTaken=$RemActonToBeTaken"
$SqlConnection1 = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection1.ConnectionString = "Server=$svr1\$inst2;Database=master;Integrated Security=True;"
$SqlConnection1.Open()
$SqlCmd1 = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd1.CommandText = "USP_RemedialActionDetails"
$SqlCmd1.Connection = $SqlConnection1
$SqlCmd1.CommandType = [System.Data.CommandType]::StoredProcedure
$InParameter1 = New-Object System.Data.SqlClient.SqlParameter;
$InParameter1 = $SqlCmd1.Parameters.Add("#ServerName" , [System.Data.SqlDbType]::String)
$InParameter1.Value = $ServerName
$InParameter1.Direction = [System.Data.ParameterDirection]"Input";
$InParameter2 = New-Object System.Data.SqlClient.SqlParameter;
$InParameter2=$SqlCmd1.Parameters.Add("#InstanceName" , [System.Data.SqlDbType]::String)
$InParameter2.Value = $inst2
$InParameter2.Direction = [System.Data.ParameterDirection]"Input";
$InParameter3 = New-Object System.Data.SqlClient.SqlParameter;
$InParameter3=$SqlCmd1.Parameters.Add("#ServerStatus" , [System.Data.SqlDbType]::String)
$InParameter3.Value = $ServerStatus1
$InParameter3.Direction = [System.Data.ParameterDirection]"Input";
$InParameter4 = New-Object System.Data.SqlClient.SqlParameter;
$InParameter4=$SqlCmd1.Parameters.Add("#RemActionToBeTaken" , [System.Data.SqlDbType]::String)
$InParameter4.Value = $RemActonToBeTaken
$InParameter4.Direction = [System.Data.ParameterDirection]"Input";
$result = $SqlCmd1.ExecuteNonQuery()
Write "result=$result"
$SqlConnection1.Close()
$SQLCmd1.Dispose() | Out-Null
}
I am calling function mirroring inside into function getInsertServerStatus.
Connection string into the function (mirroring) connection error because not found $server and $instance name.
There is some bugs in your code:
first, inside the function getInsertServerStatusyou you call mirroring without passing the parameters $svr $inst ....
it should be:
mirroring $svr1 $inst1 ... other parameters
second, Inside the function **mirroring** you call **GetConnectionString**
without passing parameters $servers $instance
it should be:
GetConnectionString $svr $inst
In the main program you call GetConnectionString $servers $instance and you don't pass the return value to a variable and has no effect in your code, remove this line
Modify your code with the suggested code.