Exec sproc from Powershell - sql

I would like to execute a stored procedure from Powershell (v2) against a SQL Server 2008 database. Coming from using C# as my primary language, I'm doing it in that fashion. For example, when I need to run a sproc that doesn't return results, here is what I'm doing now:
$con = new-object System.Data.SqlClient.SqlConnection($connectionString)
$cmd = new-object System.Data.SqlClient.SqlCommand("exec MySproc", $con)
$con.Open()
$cmd.ExecuteNonQuery()
$cn.Close()
While perhaps TMTOWTDI, I'd like to know the best way.
I should mention that I'm already familiar with T-SQL and the System.Data namespace. This is really a question about Powershell.

For straight PowerShell I would go with code like you and Andomar have written. However if you are using the PowerShell Community Extensions there are some cmdlets for working with ADO e.g.:
$conn = 'Data Source=.\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI'
$ds = Invoke-AdoCommand -ProviderName SqlClient -ConnectionString $conn `
-CommandText 'Select * from Authors' -AsDataSet
$ds.Tables
au_id : 172-32-1176
au_lname : White
au_fname : Johnson
phone : 408 496-7223
address : 10932 Bigge Rd.
city : Menlo Park
state : CA
zip : 94025
contract : True
...

I have a Windows Server 2008 with PowerShell 2.0 and SQL Server 2008 and I have been able to use the Invoke-SqlCmd to execute sql against a database.
You'll need to add the snapins with these two commands:
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
After doing this the Invoke-sqlcms will be avaliable to your PowerShell session take a look at http://technet.microsoft.com/en-us/library/cc281720.aspx for examples of using the invoke-sqlcmd

ExecuteNonQuery() runs a stored procedure but does not ask for results. You'd have to use ExecuteReader() for a rowset, or ExecuteScalar() for one row with one value.
Here's an example from this nice tutorial:
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "..."
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "sp_helpdb"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]

Related

Get-Disk values are different in powershell and in sql database

Hi i'm completely new to this ,i want to store capacity in particular format. Mycode showing correct format but in SQL database its value is getting changed to default.
My code:
$SSDCapacity= Get-Disk | select Size
foreach($size in $SSDCapacity)
{
$variable = $size.Size.ToString()
}
$result = $variable.SubString(0,3)
$result
Above code shows output as 512 in powershell.
But when i store it in database its showing in default form #{Size=512110190590}
I have a insert query for SQL Part
$InsertQuery="INSERT INTO [$($Database)].[dbo].[$($name)]
([ComputerName],[Model],[SSDCapacity])
VALUES('$ComputerNameValue','$ModelValue','$result')
"
then i'm just calling above query for insert
#Insert into Table
$SqlCmd.CommandText = $InsertQuery
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
#Creating Dataset
$Datatable = New-Object "System.Data.Datatable"
$result = $SqlCmd.ExecuteNonQuery()
$conn.Close()
So how can i store in format powershell shows.
I might be missing out some silly things here.
Any help will be thankfull.
You could do that in one line using DBATOOLS
Install-Module dbatools -Scope CurrentUser #only once
Get-DbaDiskSpace|select ComputerName, Name, Capacity, Free|Write-DbaDataTable -SqlInstance $sql -Database TEMPDB -Table disk -AutoCreateTable

How to modify my code that works with an ACCESS database to work with an SQL database

I have a code running to import CSV files into a Access database. However now I have access to a SQL server and want to use the same code if possible, but no idea what to change to adapt the code
I have a set of running code which uses a connection string I add parameters and it works.
$connectionString="Provider=Microsoft.Ace.OLEDB.12.0; Data Source=$inputDatabase"
$conn = New-Object System.Data.OleDb.OleDbConnection($connectionString)
$conn.Open()
$cmd = $Conn.CreateCommand()
$cmd.CommandText = "INSERT INTO [Tabl1]([date],[zone],[location],[Plugin ID],[CVE],[CVSS],[Risk],[Host],[Protocol],[Port],[Name])
VALUES(#date, #zone, #location, #PluginID, #CVE, #CVSS, #Risk,#Host,#Protocol,#Port,#Name)"```
[void]$cmd.Parameters.Add('#date', [System.Data.OleDb.OleDbType]::DateTime)
[void]$cmd.Parameters.Add('#zone', [System.Data.OleDb.OleDbType]::VarChar)
both code snippets above show how I set up my connection string, and a quick example of how 2 values are set with their corresponding variable type, it is not the whole code . but the snippets I know I need to change to make it work with an SQL server, the main issue being that i dont know which $connectionstring to use with an SQL server, or for example how do I make it so the data source points to the SQL server network address and uses windows authentication.
And on the second code snippet, which type of format do I use, I knwo the variable types but I assume that if I change from ace.oledb.12 , i should change the oldebtype segment. The SQL server is on SQL 13.0.5426
You'll need to update your connection string. Assuming you're using Windows authentication:
$ConnectionString = 'Data Source={0};Initial Catalog={1};Integrated Security=SSPI' -f $ServerInstanceName, $DatabaseName
And you should use the built-in SQL Server client class, System.Net.SqlClient, instead of OLEDB classes.
$conn = New-Object System.Data.SqlClient.SqlConnection $connectionString
And you'll want to update your parameter types:
[void]$cmd.Parameters.Add('#date', [System.Data.SqlDbType]::DateTime)
[void]$cmd.Parameters.Add('#zone', [System.Data.SqlDbType]::VarChar)
Everything else should be more or less identical.
Here's a working PowerShell script that connects to an SQL Server database, fetches data, and fills a data table:
$SqlQuery = 'SELECT * FROM Student WHERE Student_Id = #student_id'
$ConnectionString = 'Data Source={0};Initial Catalog={1};Integrated Security=True' -f $SqlServer, $Database
$SqlConnection = New-Object -TypeName System.Data.SqlClient.SqlConnection -ArgumentList $ConnectionString
$SqlCommand = New-Object -TypeName System.Data.SqlClient.SqlCommand -ArgumentList $SqlQuery, $SqlConnection
$SqlCommand.Parameters.Add('#student_id', [System.Data.SqlDbType]::VarChar, 10).Value = $Student_Id
$DataTable = New-Object -TypeName System.Data.DataTable -ArgumentList 'Student'
try {
$SqlConnection.Open()
$SqlDataReader = $SqlCommand.ExecuteReader()
$DataTable.Load($SqlDataReader)
}
finally {
$SqlConnection.Close()
}
$DataTable

T-SQL file into powershell script

I have a txt file(load_check_run_bmu.sql) that contains the following sql code.
RESTORE DATABASE Address from disk='C:\dir\path\address.bmu'
Use Client
GO
Select f_name
From
cst.name
USE wage
GO
Exec sp_salary
Use Client
GO
Select f_name
From
cst.name
I then have a batch file that contains:
sqlcmd -S .\NorthWind -i"C:\scripts\load_check_run_bmu.sql"
What I need to do is be able to execute all of necessary SQL commands in a powershell script and eliminate the sql txt file and the sqlcmd batch file
I'm aware I would need the following code, but I am struggling on how to convert the T-SQL script into something usable for the $SqlCmd.CommandText variable
$sqlServer = "."
$sqlDBName = "NorthWind"
$sqlConnectionString ="Server = $sqlServer; Database = $sqlDBName; Integrated Security = True"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = $sqlConnectionString
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
**$SqlCmd.CommandText = [SQL Command(s)]<====( Need assistance populating this)**
$SqlCmd.Connection = $SqlConnection
$SqlCmd.Connection.Open()
$ReturnValue = $SqlCmd.ExecuteNonQuery()
$SqlCmd.Connection.Close()
Put the T-SQL .txt file into a string using double quotes " and take out the GO statements. You can also end each statement with ; to ensure queries do not get mixed up and the queries should run without failure:
$TSQLString = "RESTORE DATABASE Address from disk='C:\dir\path\address.bmu';
Use Client;
Select f_name
From
cst.name;
USE wage;
Exec sp_salary;
Use Client;
Select f_name
From
cst.name;"
Then use the string for your variable:
$SqlCmd.CommandText = $TSQLString
It also looks like you using multiple databases in the query above. If this is the case this approach might not work has you are connecting to a single database with the statement below.
$sqlConnectionString ="Server = $sqlServer; Database = $sqlDBName; Integrated Security=True"
To get round this you will need to open up a connection for each statement. So you might want to make a function that connects to a server\database and runs a given query e.g (Run-Sql would be the function you need to create).
$SQLQuery1 = "RESTORE DATABASE Address from disk='C:\dir\path\address.bmu';"
$SQLQuery2 = "Select f_name From cst.name;"
$SQLQuery3= "Exec sp_salary;"
$SQLQuery4 = "Select f_name From cst.name;"
# Run all statments
Run-SQL -query $SQLQuery1 -DB "NorthWind" -Server "."
Run-SQL -query $SQLQuery2 -DB "Client" -Server "."
Run-SQL -query $SQLQuery3 -DB "wage" -Server "."
Run-SQL -query $SQLQuery4 -DB "Client" -Server "."
Use invoke-SQLCMD
Whick is part of sqlps module

Larger databases (10GB+) left in "restore pending" state when Stored Proc is called from Powershell script

I have this powershell script that works great for database which are 5GB and smaller. However it has a problems when it tries to databases which are bigger. The problem is that when this script completes to run the larger databases are left in restore-pending mode.
The goal of this script is to copy the latest SQL BAK files from the previous night to a folder called test. Once the SQL BAK files are there the script proceed to go to the next step to restore each SQL BAK file to the SQL instance.
When this script runs for the smaller database it completes with no errors. However for the big databases it also completes with no errors but the database are left in restore pending mode.
$bak_path = "\\nas2\sqlbackups"
$yesterday = (Get-Date -hour 13 -Minute 0 -Second 0).AddDays(-1)
get-childitem -path $bak_path -Filter "SERVERXYZ*.bak" -Exclude *master*.**,*model*.**,*msdb*.** -File -recurse |
where {$_.CreationTime -gt $yesterday} | copy-item -Destination I:\test
do {
$bak_path = "I:\TEST"
Get-ChildItem -path $bak_path | Where-Object {$_.name -like "*SERVERXYZ*"} | select -last 1 |
rename-item -newname {"Database.bak"}
$Input = Get-ChildItem -path $bak_path Database.bak
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=SQLENTBAK;Database=test;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "dbo.restoredatabase"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
$FileName = "I:\TEST\Database.bak"
if (Test-Path $FileName) {
Remove-Item $FileName}
} while (Get-ChildItem I:\TEST\)
I would appreciative any insight to why this is happening.
Thanks
You didn't provide dbo.restoredatabase but I would expect that you are timing out when executing that stored procedure and not rolling back when your connection terminates, since the default timeout is 30 seconds.
You need to change the $SqlCmd.CommandTimeout to be more generous, for instance 300 for 5 minutes. The length that is sufficient is based on a lot of factors.

Script to use Windows Authentication to fetch data from SQL table and to write it back in .txt file

I am looking for some Power Shell script to fetch data from a SQL Server table using windows authentication and write it back to .txt file.
I am very new to Power Shell thing and never worked on this before. So if somebody could guide me in this effort.
Thanks to both of you guys. I am able to google and make a script working according to requirement.
Here it is
#Script parameters
param(
#Name of MS SQL Server instance
[parameter(Mandatory=$true,
HelpMessage="Specify the SQL Server name where will be run a T-SQL code",Position=0)]
[String]
[ValidateNotNullOrEmpty()]
$server = $(throw "sqlserver parameter is required."),
#Database name for execution context
[parameter(Mandatory=$true,
HelpMessage="Specify the context database name",Position=1)]
[String]
[ValidateNotNullOrEmpty()]
$dbname = $(throw "dbname parameter is required.")
)
$connectionString = “Server=$server;Database=$dbname;Integrated Security=True;”
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$query = “SELECT type,name FROM sys.tables"
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”
$table.Load($result)
$table | Out-File D:\test\swedes$dbname.txt
$connection.Close()
Now I am looking for a way to run this script on a remote SQL Server and save the results.
I am googling about this but still if someone knows a way to run this script using SSIS please help.