I have been trying so hard to use PowerShell command invoke-sqlcmd
to run simple SQL queries on multiple SQL servers, where some are default instances servername
and some are named instances ServerName\\InstanceName
.
I have tried the following ways:
1: invoke-sqlcmd -ConnectionString $CN -Query $query ....
2: invoke-sqlcmd -Credential $adminSQL -Query $query ....
3: Start-Process powershell.exe -Credential $sysadminCred -ArgumentList "-NoExit", "-Command", "\$scriptBlock" ....
At first I was getting the typical error server not found....
Most of the time it was login failed because it keeps trying to use the WindowsAdmin
account because I am logged in to server as WindowsAdmin
, which has no DBA permission.
I hope you are successful or have done this already. Please share.