title: Credential Parameter not working for Invoke-sqlcmd
body: <p>I am using the Invoke-sqlcmd cmdlet in Powershell to run an SQL query saved in a string against a remote server.</p>
  
  <p>I have included the credential parameter to ask for domain credentials to connect to the SQL instance.</p>
  
  <p>I've tested this without the credential parameter; instead running the Powershell window as the user, this works fine, so I am confident that permissions for the user are set correctly.</p>
  
  <p>However, I cannot get it to work with the credential parameter.</p>
  
  <pre><code>Import-Module -Name SqlServer -force
  
  function Correct-Servername {
      param(
          [string] $dataSource = ".\SQL",
          [string] $database = "MASTER",
          [string] $sqlCommand = "sp_dropserver @@servername DECLARE   @ServerName SQL_VARIANT = (SELECT serverproperty('Servername')),   @SQLQuery NVARCHAR(4000); SET @SQLQuery = 'sp_addserver ''' + CONVERT(NVARCHAR(100), @SERVERNAME) + ''', ''LOCAL''' PRINT @SQLQuery EXECUTE sp_executesql @SQLQuery"
        )
      $cred = Get-Credential
      Invoke-Sqlcmd -Query $sqlCommand -ServerInstance $dataSource -Database $database -Credential $cred
  
  }
  
  Correct-Servername | out-file -FilePath C:\Temp\correct_servername.log
  </code></pre>
  
  <p>This is the error i receive:</p>
  
  <pre><code>cmdlet Get-Credential at command pipeline position 1
  Supply values for the following parameters:
  Credential
  Invoke-Sqlcmd : Login failed for user 'domain\user'.
  At C:\Temp\Rename-Create-Replication-2.ps1:20 char:2
  +     Invoke-Sqlcmd -Query $sqlCommand -ServerInstance $dataSource -Dat ...
  +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
      + FullyQualifiedErrorId : SqlExceptionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
  
  Invoke-Sqlcmd :
  At C:\Temp\Rename-Create-Replication-2.ps1:20 char:2
  +     Invoke-Sqlcmd -Query $sqlCommand -ServerInstance $dataSource -Dat ...
  +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : ParserError: (:) [Invoke-Sqlcmd], ParserException
      + FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
  </code></pre>
  
  <p>Any assistance is appreciated.</p>