Invoke-SqlCmd2 by Chad Miller 22 months ago (modification of post by Chad Miller view diff)
View followups from Chad Miller and Jaans | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1791"></script>download | new post
Modeled after SQL Server 2008 Invoke-Sqlcmd, but fixes bug in QueryTimeout. Fixed minor issue closing connection.
- function Invoke-Sqlcmd2
- {
- param(
- [string]$ServerInstance,
- [string]$Database,
- [string]$Query,
- [Int32]$QueryTimeout=30
- )
- $conn=new-object System.Data.SqlClient.SQLConnection
- $conn.ConnectionString="Server={0};Database={1};Integrated Security=True" -f $ServerInstance,$Database
- $conn.Open()
- $cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn)
- $cmd.CommandTimeout=$QueryTimeout
- $ds=New-Object system.Data.DataSet
- $da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
- [void]$da.fill($ds)
- $conn.Close()
- $ds.Tables[0]
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository