1

The documentation for Invoke-DbaQuery mentions many other similar functions

This function is a wrapper command around Invoke-DbaAsync, which in turn is based on Invoke-SqlCmd2. It was designed to be more convenient to use in a pipeline and to behave in a way consistent with the rest of our functions.

There are of course other ways to query SQL from PowerShell, e.g. Invoke-Sqlcmd.

What are the benefits of using Invoke-DbaQuery instead of Invoke-Sqlcmd, Invoke-Sqlcmd2, and Invoke-DbaAsync?

J. Mini
  • 1,161
  • 8
  • 32

1 Answers1

2
  1. One feature with Invoke-DbaQuery is, it accepts collection of instances for invoking a query. And, no need of explicit loop construct to execute a query or script file unlike invoke-sqlcmd does need.
    $instances = @('SqlInstance01', 'SqlInstance02')
    $sql = "select @@servername as InstanceName"
#Explicit for loop, without which it throws an exception
$instances | %{invoke-sqlcmd -ServerInstance $_ -Query $sql}

InstanceName
------------
SqlInstance01
SqlInstance02

Import-Module dbatools
#No explicit loop specified
Invoke-DbaQuery -SqlInstance $instances -Query $sql

InstanceName
------------
SqlInstance01
SqlInstance02

  1. There are bunch of other usecases it supports where as invoke-sqlcmd doesn't. Especially taking pipeline inputs, refer to examples section here Invoke-DbaQuery.
S.D.
  • 754
  • 5
  • 19