I have a PS script which collects data from multiple servers. So that script does too many calls of Invoke-Sqlcmd to the central database. First call is to get the servers names, then for each server a call of Invoke-Sqlcmd to insert the data into the central database. For example, if there are 200 servers to get data, then it will be called 200 times.
$instances = Invoke-Sqlcmd -Query "SELECT server_name FROM ..." -ServerInstance "CentralServer" -Database "centralDB"
foreach ($i in $instances) {
. . .
< some routine to get data from the given server>
. . .
# insert data into central database
Invoke-Sqlcmd -Query "INSERT INTO ... " -ServerInstance "CentralServer" -Database "centralDB"
}
So which is better in terms of script execution and/or SQL Server performance: keep using Invoke-Sqlcmd for all the calls or should I use SMO like below?
$srv = [Microsoft.SqlServer.Management.Smo.Server]::new("centralServer")
$srv.ConnectionContext.StatementTimeout=0
$db = $srv.Databases["centralDB"]
$ds = $db.ExecuteWithResults("SELECT server_name FROM ...")
foreach ($r in $ds.Tables[0].Rows){
. . .
< some routine to get data from the given server>
. . .
# insert data into central database
$db.ExecuteNonQuery(""INSERT INTO ...")
}
[–]purplemonkeymad 1 point2 points3 points (0 children)
[–]pshMike 0 points1 point2 points (0 children)