troubleshooting Question

How to try and catch in Powershell with oledb

Avatar of tech_ci_sherbe
tech_ci_sherbe asked on
.NET App ServersMicrosoft SQL Server 2005Powershell
3 Comments1 Solution1993 ViewsLast Modified:
Hello,

I was wondering how I could Try and Catch or Trap an error when using oledb in Powershell. I have a problem with the following line :

$command = New-Object System.Data.OleDb.OleDbCommand $sqlcommand,$connection

For some reasons, if I change manually the $sqlcommand to my actual SQL command the rest of the script works fine. If I print the value of $sqlcommand, I get the right thing (the actual SQL command I entered manually to make it work) so I know its value is OK.

Any idea how I could try and catch this ?
Function Global:SQL\Invoke-Command ([string] $DataSource, [string] $Database, [string] $SQLCommand, [System.Management.Automation.PsCredential] $Credential) {
# // Prepare the authentication information. By default, we pick Windows authentication
$authentication = Integrated Security=SSPI;
# // If the user supplies a credential, then they want SQL authentication
if($credential)
{
$plainCred = $credential.GetNetworkCredential()
$authentication =
(uid={0};pwd={1}; -f $plainCred.Username,$plainCred.Password)
}
# // Prepare the connection string out of the information they provide
$connectionString = Provider=sqloledb;  +
Data Source=$dataSource;  +
Initial Catalog=$database;  +
$authentication; 
# // Connect to the data source and open it
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand $sqlcommand,$connection
$connection.Open()
# // Fetch the results, and close the connection
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataset)
$connection.Close()
# // Return the dataset
Return $dataset
ASKER CERTIFIED SOLUTION
VeeNashi

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros