Lily
Lily

Reputation: 215

Getting error while trying to get username in Powershell

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?

Upvotes: 1

Views: 93

Answers (1)

Mike Kennedy
Mike Kennedy

Reputation: 394

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

Upvotes: 1

Related Questions