Powershell Pass SQL connection object

1.8k views Asked by At

I am trying to write a powershell function where I pass my connection object.

Many of the SMO examples do not show how to use an established connection object.

Are there any good examples that allow the passing of the connection object?

Thanks in advance.

1

There are 1 answers

0
Rahul On

Taken from This Post. Read the post for more information.

You can define a function like below

function getdata( $sql,$parameters=@{},$conn,$timeout=30,[switch]$help){   

}

Then can call it with a already constructed connection object like below

$conn=new-object data.sqlclient.sqlconnection "Server=servername;Integrated Security=True"
$conn.open()

Call the function

getdata 'select * from mytable' -conn $conn