invoke-command with invoke-sql -> Problems with Instance Variable

48 Views Asked by At

I am writing a script for reading remote SQL instances.

I have a problem with the transfer of the ServerInstance Variable. I just can't find the error. Does anyone have an idea?

This Command is working:

invoke-command -computername computer1 -ScriptBlock { Invoke-Sqlcmd -ServerInstance servername\sqlexpress -Database master -Query "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition'), SERVERPROPERTY ('engineedition')"}

Result:

PSComputerName : computer1
RunspaceId     : 5393f2e0-715c-46c1-9e64-13642c2f031b
Column1        : 14.0.1000.169
Column2        : RTM
Column3        : Express Edition (64-bit)
Column4        : 4

This is not working:

$Instance="computer1\sqlexpress"
invoke-command -computername computer1 -ScriptBlock { Invoke-Sqlcmd -ServerInstance $Instance -Database master -Query "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition'), SERVERPROPERTY ('engineedition')"}

Error Message:

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)
enter code here

Many Thanks!

0

There are 0 best solutions below