Powershell Function SQL Stored Procedure with Parameters - sql

The error received is "The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlCommand objects." & "Procedure or function 'usp__SingleUpdateServerBackupPath' expects parameter '#decServerName', which
was not supplied."
PowerShell code:
Set-StrictMode -Version 1.0
function update-serverbackuppath {
param(
[Parameter(Mandatory=$True,ValueFromPipeLine=$True)][object[]]$inputobject
)
BEGIN {
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection
$connection.ConnectionString = "server=servername;database=database;trusted_connection=yes"
$connection.Open()
}
PROCESS {
$UpdateBackupPath = New-Object -TypeName System.Data.SqlClient.SqlCommand
$UpdateBackupPath.Connection = $connection
$UpdateBackupPath.CommandText = "usp__SingleUpdateServerBackupPath"
$UpdateBackupPath.Commandtype = [System.Data.Commandtype]::StoredProcedure
$ParamUpdateBackupPath = New-Object -TypeName System.Data.SqlClient.SqlParameter
$ParamUpdateBackupPath.ParameterName = "#decBackupPath"
$ParamUpdateBackupPath.SqlDbType = [System.Data.SqlDbType]::VarChar
$ParamUpdateBackupPath.Direction = [System.Data.ParameterDirection]::Input
$ParamUpdateBackupPath.Value = $inputobject.paths
$ParamUpdateBackupPathServerName = New-Object -TypeName System.Data.SqlClient.SqlCommand
$ParamUpdateBackupPathServerName.ParameterName = "#decServerName"
$ParamUpdateBackupPathServerName.SqlDbType = [System.Data.SqlDbType]::VarChar
$ParamUpdateBackupPathServerName.Direction = [System.Data.ParameterDirection]::Input
$ParamUpdateBackupPathServerName.Value = $inputobject.names
$UpdateBackupPath.Parameters.Add($ParamUpdateBackupPath)
$UpdateBackupPath.Parameters.Add($ParamUpdateBackupPathServerName)
$reader = New-Object -TypeName System.Data.SqlClient.SqlDataReader = $UpdateBackupPath.ExecuteReader()
}
END {
$connection.Close()
}
}
SQL Procedure:
Create Procedure usp__SingleUpdateServerBackupPath
(
#decBackupPath AS varchar(50),
#decServerName AS varchar(50)
)
AS
UPDATE BCKP
SET PTH = #decBackupPath
FROM BCKP
INNER JOIN SRVR
ON SRVR.ID = BCKP.FK_SRVR
WHERE SRVR.NM = #decServerName
CSV File Format
Import-Csv -Path C:\Bin\Repos\Backup.csv | C:\Bin\Scripts\update-serverbackuppath.ps1
Names Paths
Server1 \\fileshare\server_name

The Powershell code has several syntax errors, like referring to enums in erroneus a way:
# Incorrect form
[System.Data.Commandtype.StoredProcedure]
# Correct form for referring to enumeration
[System.Data.Commandtype]::StoredProcedure
Later on, there is an undeclared object which's member method is called:
# $command is not set, so ExecuteReader method is available
$reader = $command.ExecuteReader()
It is highly recommended to use strict mode in Powershell. It helps catching typos by preventing access to non-existing properties an uninitialized variables.
Edit
After the updated code, there are still two errors:
# This doesn't make sense. The variable should be SqlParameter, not SqlCommand
$ParamUpdateBackupPathServerName = New-Object -TypeName System.Data.SqlClient.SqlCommand
# Like so:
$ParamUpdateBackupPathServerName = New-Object -TypeName System.Data.SqlClient.SqlParameter
# This is nonsense syntax
$reader = New-Object -TypeName System.Data.SqlClient.SqlDataReader = $UpdateBackupPath.ExecuteReader()
# Like so:
$reader = $UpdateBackupPath.ExecuteReader()

Related

How check for errors with powershell sql

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
}

Check if the sql command gets timeout through powershell

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.

PowerShell SQL query using here-string and variable expansion

How can I combine the query results for servers that are swSaturday = 1 and swSunday = 1 The [RebootTime] could be swSunBeginTime or swSatBeginTime and it also might not help I'm only trying to run this query on a list of machines in a text file.
Error received is Exception calling "Fill" with "1" argument(s): "Incorrect syntax near the keyword 'WHERE'." and the error goes away if WHERE swName = $($props.Server) is removed.
Function GetServer-RebootLookUp{
Begin{
$servers = GC D:\Scripts\reboots1.txt
$SQLServer = 'Server101'
$Database = 'Database101'
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $Database; Integrated Security = True"
}#End Begin
Process{
$servers | ForEach-Object{
$props = #{}
$props.Server = $_
$ServerQuery = #"
SELECT swName AS [Server], swRack AS [Rack], swEnvironment AS [Environment], swSunBeginTime AS [RebootTime], swSchedule as [Schedule]
FROM SW_SERVICE_LVL
INNER JOIN SW_SPECIALTY
ON swDiscount = swSpecialtyId
AND swEnvironment IN ( 'Cert', 'Test', 'Prod' )
AND swSchedule IN ( 'SCCM - Monthly' )
AND swGrpResp = 'MyGroup'
AND swRootObjectType = 'Server'
WHERE swSunday = 1
WHERE swName = $($props.Server)
"#
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $ServerQuery
$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]
}
}#end process
End{
$SqlConnection.Close()
}
}#End function!
GetServer-RebootLookUp
Your query has two WHERE clauses. This isn't valid SQL.
Change this:
WHERE swSunday = 1
WHERE swName = $($props.Server)
To this:
WHERE swSunday = 1
AND swName = $($props.Server)
Also, you'll probably need to ensure it's properly quoted as a literal value. In general however, you should use a parameterized query for this sort of thing.

Powershell - SQL Server - connectionstring in loop for multiple Instances

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();
}

sqlbulkcopycolumnmapping import csv to table issue

I am at a loss. I am trying to import columns from a csv into an existing data table. I believe the best way to do this is through column mapping using sqlbulkcopy but i just cant wrap my head around this. Im using the script below with the out-datatable function to do column mapping and even though it appears to insert the data, when i run a query i cannot find it. What am i doing wrong?
import-module .\functions.psm1
# Database variables
$sqlserver = "Db-test-dev\sql2008"
$database = "Employees"
#$table = "dbo.tblPersonal"
$csvfile = "C:\temp\cidb_test.csv"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.connectionstring = "Data Source=$sqlserver;Integrated Security=true;Initial Catalog=$database;"
$CSVDataTable = Import-Csv ‘$csvfile’ | Out-DataTable
$sqlBulkCopy = New-Object (“Data.SqlClient.SqlBulkCopy”) -ArgumentList $SqlConnection
$sqlBulkCopy.DestinationTableName = “dbo.tblpersonal”
$ColumnMap1 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping(1, 1)
$ColumnMap2 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping(2, 2)
#$ColumnMap3 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping(2, 3)
$sqlBulkCopy.ColumnMappings.Add($ColumnMap1)
$sqlBulkCopy.ColumnMappings.Add($ColumnMap2)
#$sqlBulkCopy.ColumnMappings.Add($ColumnMap3)
$SqlConnection.Open()
$sqlBulkCopy.WriteToServer($CSVDataTable)
$SqlConnection.Close()
Added column mapping using bulkcopy
$ColumnMap1 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('SSN','SSN')
$ColumnMap2 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('LastName', 'LastName')
$ColumnMap3 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('MiddleName','MiddleName')
$ColumnMap4 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('FirstName','FirstName')
$ColumnMap5 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('PreferredName','PreferredName')
$ColumnMap6 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('WorkPhone','WorkPhone')
$ColumnMap7 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('Email','Email')
$ColumnMap8 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('EmpType','EmpType')
$ColumnMap9 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('HireDate', 'HireDate')
$ColumnMap10 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('ACID','ACID')
$ColumnMap11 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('OPID','OPID')
$ColumnMap12 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('Floor','Floor')
$ColumnMap13 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('Department','Department')
$ColumnMap14 = New-Object System.Data.SqlClient.SqlBulkCopyColumnMapping('Division','Division')
$BulkCopy.ColumnMappings.Add($ColumnMap1)
$BulkCopy.ColumnMappings.Add($ColumnMap2)
$BulkCopy.ColumnMappings.Add($ColumnMap3)
$BulkCopy.ColumnMappings.Add($ColumnMap4)
$BulkCopy.ColumnMappings.Add($ColumnMap5)
$BulkCopy.ColumnMappings.Add($ColumnMap6)
$BulkCopy.ColumnMappings.Add($ColumnMap7)
$BulkCopy.ColumnMappings.Add($ColumnMap8)
$BulkCopy.ColumnMappings.Add($ColumnMap9)
$BulkCopy.ColumnMappings.Add($ColumnMap10)
$BulkCopy.ColumnMappings.Add($ColumnMap11)
$BulkCopy.ColumnMappings.Add($ColumnMap12)
$BulkCopy.ColumnMappings.Add($ColumnMap13)
$BulkCopy.ColumnMappings.Add($ColumnMap14)