title: Invoke-SQLcmd not working inside Invoke-command
body: <p>I am currently running a <code>Invoke-SQLcmd</code> inside a <code>Invoke-Command</code> script block and keep receiving a connection error, The <code>Invoke-SQLcmd</code> script works when run on its own but not once wrapped in the <code>Invoke-Command</code>.</p>
  
  <pre><code>Invoke-Command -ComputerName "MyserverFQDN" -Credential $Serviceaccount -ScriptBlock {
  Invoke-Sqlcmd -ServerInstance "SQLDBFQDN" -Database BizTalkMgmtDb -Query "
  USE BizTalkMgmtDb
  GO
  SELECT        bts_sendport.nvcName, bts_sendport_transport.nvcAddress, nPortStatus, bts_sendport_transport.dtFromTime, 
                bts_sendport_transport.bIsServiceWindow, bts_sendport_transport.dtToTime,
                adm_Adapter.Name As TransportTypeID, bts_application.nvcName AS Application
  FROM            bts_sendport With(NOLOCK) INNER JOIN
                           bts_sendport_transport ON bts_sendport.nID = bts_sendport_transport.nSendPortID 
                           INNER JOIN
                           bts_application ON bts_sendport.nApplicationID = bts_application.nID
                           INNER JOIN 
                           adm_Adapter ON bts_sendport_transport.nTransportTypeId = adm_Adapter.Id
  
  WHERE 
  nTransportTypeId IS NOT NULL
  
  "
  }
  </code></pre>
  
  <p>I then receive the following error:</p>
  
  <pre><code>A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL 
  Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
      + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
      + FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
      + PSComputerName        : "MyServerFQDN"     
  ParserError: (:) [Invoke-Sqlcmd], ParserException
      + CategoryInfo          : ParserError: (:) [Invoke-Sqlcmd], ParserException
      + FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
      + PSComputerName        : "MyserverFQDN"
  </code></pre>
  
  <p>Like I said I can run the SQLcmd from my server as the service account and it works fine but if I invoke this from my PC to call my server to use my service account to run the SQLcmd I get that error. Any Idea what happening or how to solve this?</p>