SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Script to loop through SQL Server Instances and pass Instance Name to Connection String


Script to loop through SQL Server Instances and pass Instance Name to Connection String

Author
Message
george.auckland 44813
george.auckland 44813
Valued Member
Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)

Group: General Forum Members
Points: 67 Visits: 86
I am trying to use a SQL Table to loop through SQL Instances and pass the Instance to a script:

$DestinationSQLServer = 'coaidera01\idera'
$DestinationDBName = 'CTM_Monitoring'
$DestinationQry = "select InstanceName from COA_SQLServerInstanceInfo WHERE InstanceName Like 'coaidera01\idera'"

$SQLSvr = get-SqlData -sqlserver $DestinationSQLServer -dbname $DestinationDBName -qry $DestinationQry

$SQLSvr | foreach {

$myserver = $_

$MySQL = new-object Microsoft.SqlServer.Management.Smo.Server $myserver;

------------------------------------------------------------------------------

I get this error: The following exception was thrown when trying to enumerate the collection: "Failed to connect to server System.Data.DataRow.".

Any Clues
Laerte Poltronieri...
Laerte Poltronieri Junior-367636
SSC Eights!
SSC Eights! (939 reputation)SSC Eights! (939 reputation)SSC Eights! (939 reputation)SSC Eights! (939 reputation)SSC Eights! (939 reputation)SSC Eights! (939 reputation)SSC Eights! (939 reputation)SSC Eights! (939 reputation)

Group: General Forum Members
Points: 939 Visits: 836
If you get-member in get-sqldata you will see that it returns a System.Data.DataRow and the properties related to your query. This is what you need to use :

In you query,the name of the Instance is InstanceName rigth ?

Change you script to :

$myserver = $_.InstanceName

and will work Smile

$hell your Experience !!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search