Getting error while trying to get username in Powershell

Viewed 60

I am new to Powershell and I want to get all the usernames in a Datatable in Powershell. I am trying to execute the below two lines but I am getting the error below:

$query = "select * from sys.database_principals"
$databases = invoke-Sqlcmd -Query $query -ServerInstance "ServerName\dbName" -SuppressProviderContextWarning

Error:


 + ... databases = invoke-Sqlcmd -Query $query -ServerInstance 
+            
    + CategoryInfo          : NotSpecified: (:) [Invoke-Sqlcmd], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

Can anyone please help me out?

1 Answers

If you are running this on the same server as the Instance, Then Run it this way:

$query = "select * from sys.database_principals"
$databases = invoke-Sqlcmd -Query $query -SuppressProviderContextWarning
$databases
Related