Thisis part of a bigger script which finds DatabaseFiles on a SQL Server machine (multiple instances).
Following should just return all files for 3 instances.
The server is called V3000801 and there is one default instance + 2 named instances on there. It's ok with me if either default or named doesn't work I'll work around this alone (most likely create a flag and do default with another connection string).
$SqlCmd.ExecuteNonQuery() just returns -1 which does not make any sense for me.
Thanks for the help
for($i=0;$i -lt $instances.Length;$i++){
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
$Server= "V3000801\"+$instances[$i];
$SqlConnection.ConnectionString = "Server = $server ; Database = master; Integrated Security = sspi;trusted_connection=true";
$sqlQuery="SELECT physical_name FROM sys.master_files;";
Write-Host $SqlConnection.ConnectionString;
$SqlConnection.Open();
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
$SqlCmd.CommandText = $sqlQuery;
$SqlCmd.Connection = $SqlConnection;
$SqlCmd.ExecuteNonQuery();
$SqlConnection.Close();
}
the link posted by #jody contains some good information
try:
$dr= $SqlCmd.ExecuteReader()
while ($dr.Read())
{
$dr.GetValue(0)
}
$sqlconnection.Close()
Use the ExecuteReader function for selects. ExecuteNonQuery is used for operations that do not return any results such as inserts, updates and deletes.
Here is an example in .NET but it should be similar in PowerShell.
EDIT:
This code should work. I tried it out on my own environment (with a different server name).
for($i=0;$i -lt $instances.Length;$i++){
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
$Server= "V3000801\"+$instances[$i];
$SqlConnection.ConnectionString = "Server = $server ; Database = master; Integrated Security = sspi;trusted_connection=true";
$sqlQuery="SELECT physical_name FROM sys.master_files;";
Write-Host $SqlConnection.ConnectionString;
$SqlConnection.Open();
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
$SqlCmd.CommandText = $sqlQuery;
$SqlCmd.Connection = $SqlConnection;
$reader = $SqlCmd.ExecuteReader();
while ($reader.Read())
{
"pfad=" $reader["physical_name"];
};
$SqlConnection.Close();
}
Related
I need to check for general errors with my sql, like make sure it connects ok, make sure it returns with the query results ok, etc. I tried try/catch, but even though I know my login is wrong, it's not finding an error with the catch. I had found the try/catch idea at try catch.
How do I identify or catch when it doesn't logon to the db, or returns 0 results?
This is what I have so far:
function SQLQueryWriteToFile([string]$SQLquery, [string]$extractFile, [string]$facility)
{
#create sql connection to connect to the sql DB
try{
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Server=blah;Database=blah;User ID=blah;Password=blah" #sql connection
$sqlConnection.Open()
#Create a SqlCommand object to define the query
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandText = $SQLquery
$sqlCmd.Parameters.Add('#facility',$facility)
$sqlCmd.Connection = $sqlConnection
#create a SqlAdapter that actually does the work and manages everything
$sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$sqlAdapter.SelectCommand = $sqlCmd
$sqlAdapter.SelectCommand.CommandTimeout=300 #set timeout for query execution to 5 minutes (60x5=300)
#create an empty dataSet for the query to fill with its results
$dataSet = New-Object System.Data.DataSet
#execute the query and fill the dataSet (then disconnect)
$sqlAdapter.Fill($dataSet)
$sqlConnection.Close()
#dump the data to csv
$DataSet.Tables[0] | Export-Csv $extractFile #this may not be comma delimited...need to check
} #try
catch{
if($LASTEXITCODE -ne 0) #if there was an error, record it
{
Write-host $PSItem.ToString()
$global:ErrorStrings.Add("Exception: $LASTEXITCODE ; $PSItem.ToString() ")
}
else
{
$global:ErrorStrings.Add("Exception: $LASTEXITCODE ; $PSItem.ToString() ")
}
} #catch
}
I wrote a script whose purpose is to take all the files under a particular folder and in a loop to perform on the files SQL procedures in parallel (3 files at the same time).
I've seen some timeouts in the database but Powershell does not give me an error message about that. Is there a way to get errors about these timeouts?
I tried to deal with the problem by increasing the timeout value and yet the problem is not solved so I'll have to at least have a test on the subject or an error message.
I added the increased timeout to the script in the connection string and with: $Command.CommandTimeout =
param($fileName)
Import-Module sqlps -DisableNameChecking
$sqlserver = "SQLSERVERNAME"
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server=SQLSERVERNAME;integrated security = True;connection Timeout=60;"
$Connection.Open()
$Command = $Connection.CreateCommand()
$Command.CommandTimeout = 60
#Start procedure
$Command.CommandText = "EXEC [DBNAME].[dbo].[usp_procname] #FileName"
$Command.Parameters.AddWithValue("#FileName", $fileName)
$Command.Connection = $Connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $Command
$DataTable = New-Object System.Data.DataTable
$SqlAdapter.Fill($DataTable)
$Connection.Close()
I would really appreciate your help.
I want to write an application that talks to a database. The databases are created through phpmyadmin interface. I can talk to these fine through php. What I would like is to populate these databases using a powershell script.
How do I connect to the database ? How do I populate a database ? I can't seem to find any good starting points.
Here is a great place to start:
https://dbatools.io/
https://dbareports.io/
or you can look here as well:
https://www.powershellgallery.com/
Here's a function to handle this type of task
function Invoke-SQL
{
param (
[string]$server,
[string]$database,
[string]$Query
)
$connectionString = "Data Source=$server; " +
"Integrated Security=SSPI; " +
"Initial Catalog=$database"
$connection = new-object
system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($Query, $connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
# Use these to populate info #
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
# displays info #
$dataSet.Tables
}
Here's an example of updating the SQL Database
Invoke-SQL -server 'server' -database 'database' -Query "UPDATE [database].[dbo].[Local] SET Field1 = '$InfoForField1', Field2 = '$InfoForField2'"
You can do whatever you need using this method, as long as you know your SQL queries and how to populate the varaibles with the correct information that you need.
I have a PowerShell script that is querying my local SQL Server database. I know it can connect and talk to the database because if I change my connection information to be wrong, my try/catch block throws the expected error. I have a table called MaintMode with 2 columns in it: SiteName and Status.
When I run a SELECT SiteName, Status FROM dbo.MaintMode in SQL Server Management Studio, I get:
SiteName Status
MySite off
which is great. However, when I have my PowerShell script run that exact same query, it just returns 1 to the console. How can I get my PowerShell script to return the same output that I would see in SQL Server Management Studio?
My PowerShell code is as follows:
# Set up connection to the SQL Server
$SQLServer = "localhost\SQLEXPRESS"
$SQLDBName = "MaintSiteDB"
$uid = "removed"
$pwd = "removed"
#Specify the query to run
$SqlQuery = "SELECT SiteName, Status FROM dbo.MaintMode;"
#Build connection to the SQL Server
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database =
$SQLDBName; Integrated Security = false; User ID = $uid; Password = $pwd;"
#Connect to the SQL Server and run the query
Try
{
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$QueryResult = $SqlAdapter.Fill($DataSet)
}
catch
{
Write-Output "Failed to connect to $SQLServer. Please check your
connection parameters and try again."
Break
}
I am trying to create a script to automatically iterate through a text file of all our SQL Server instances and add each on if it doesn't already exist to the CMS. I want to try doing this through SMO instead of hardcoding sql strings in. Below is what I have so far but it doesn't seem to be working. Any help would be appreciated. Thanks.
Eventually I will add more If statements in to distribute the instances to certain groups but for now I'm just trying to get it to populate everything.
$CMSInstance = "cmsinstancename"
$ServersPath = "C:\Scripts\InPutFiles\servers.txt"
#Load SMO assemplies
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Management.RegisteredServers') | out-null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Management.Common') | out-null
$connectionString = "Data Source=$CMSINstance;Initial Catalog=master;Integrated Security=SSPI;"
$sqlConnection = new-object System.Data.SqlClient.SqlConnection($connectionString)
$conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection($sqlConnection)
$CMSStore = new-object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore($conn)
$CMSDBStore = $CMSStore.ServerGroups["DatabaseEngineServerGroup"]
$Servers = Get-Content $ServersPath;
foreach($Server in $Servers)
{
#Put this in loop to deal with duplicates in list itself
$AlreadyRegisteredServers = #()
$CMSDBStore.GetDescendantRegisteredServers()
$RegServerName = $Server.Name
$RegServerInstance = $Server.Instance
if($AlreadyRegisteredServers -notcontains $RegServerName)
{
Write-Host "Adding Server $RegServerName"
$NewServer = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer($CMSDBStore, "$RegServerName")
$NewServer.SecureConnectionString = "server=$RegServerInstance;integrated security=true"
$NewServer.ConnectionString = "server=$RegServerInstance;integrated security=true"
$NewServer.ServerName = "$RegServerInstance"
$NewServer.Create()
}
else
{
Write-Host "Server $RegServerName already exists - cannot add."
}
}
I cut your script down to just the basics and it works for me. I did have to change the connection command to work in my environment but other than that and registering a default instance of SQL Server there were no errors. Once I did a refresh of the CMS server the newly registered server was visible and accessible.
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Management.RegisteredServers') | Out-Null
$CMSInstance = 'CMS_ServerName'
$connectionString = "Data Source=$CMSInstance;Initial Catalog=master;Integrated Security=SSPI;"
$sqlConnection = new-object System.Data.SqlClient.SqlConnection($connectionString)
$conn = New-Object System.Data.SqlClient.SqlConnection("Server=$CMSInstance;Database=master;Integrated Security=True")
$CMSStore = new-object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore($conn)
$CMSDBStore = $CMSStore.ServerGroups["DatabaseEngineServerGroup"]
$RegServerName = 'ServerToRegister'
$RegServerInstance = $RegServerName
$NewServer = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer($CMSDBStore, "$RegServerName")
$NewServer.SecureConnectionString = "server=$RegServerInstance;integrated security=true"
$NewServer.ConnectionString = "server=$RegServerInstance;integrated security=true"
$NewServer.ServerName = "$RegServerInstance"
$NewServer.Create()