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.
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 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
}
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();
}
I'm wanting to import a log file into sql table using powershell. Has anyone does this?
The log has some structure at the bottom but I would like to skip the top 13 rows.
Sample Log: https://lh6.googleusercontent.com/yFajtwkfT73JqojMiEZMz-_1cy6XQlHtqP5ibkJbfpM=w484-h228-p-no
Powershell script for SQL connection:
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=Server\Development;Database=Development;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "BULK INSERT Development..CUBE_LOG
FROM 'D:\POWERCUBE_SQL_LOG\result.txt'
WITH (CODEPAGE='RAW' ,FIELDTERMINATOR = '|',ROWTERMINATOR = '\n', DATAFILETYPE='widechar')"
$SqlCmd.Connection = $SqlConnection
$SqlConnection.Open()
$sqlCmd.ExecuteNonQuery()
$SqlConnection.Close()
Use the First Row Argument in bulk insert.
Reference : http://technet.microsoft.com/en-us/library/ms188365.aspx
WITH (CODEPAGE='RAW' ,FIELDTERMINATOR = '|',ROWTERMINATOR = '\n',FIRSTROW = 14,DATAFILETYPE='widechar')"