Skip to content

Instantly share code, notes, and snippets.

@tcartwright
Last active August 28, 2025 13:16
Show Gist options
  • Save tcartwright/8f69baa13e7576db994af3b4b44c2e2c to your computer and use it in GitHub Desktop.
Save tcartwright/8f69baa13e7576db994af3b4b44c2e2c to your computer and use it in GitHub Desktop.
SQL SERVER: Test connectivity to server from sql server
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
/************************************************************
** Change the value of the @server variable here
*************************************************************/
DECLARE @Server VARCHAR(255) = 'servername'
DECLARE @nslookup VARCHAR(500) = CONCAT('nslookup ', @Server),
@ping VARCHAR(500) = CONCAT('ping ', @Server),
@PSScript VARCHAR(8000) = CONCAT('powershell.exe -Command "try { Test-Connection -ComputerName \"', @Server, '\" -Count 2 } catch { Write-Host \"Error: $($_.Exception.Message)\" }"')
SELECT @nslookup AS [Command]
EXEC [sys].[xp_cmdshell] @nslookup
SELECT @ping AS [Command]
EXEC [sys].[xp_cmdshell] @ping
SELECT @PSScript AS [Command]
EXEC [sys].[xp_cmdshell] @PSScript
EXEC sp_configure 'xp_cmdshell', 0;
RECONFIGURE;
EXEC sp_configure 'show advanced options', 0;
RECONFIGURE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment