Home > Mobile >  Getting error while trying to get username in Powershell
Getting error while trying to get username in Powershell

Time:11-17

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?

CodePudding user response:

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