Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

DBEngine Access check

$
0
0
The below powershell code checks if we can access to a server and separates the good list from the bad list.How can also check and add to the same above code to check if wehave access to the database engines of these respective servers and add them into 2 more tables ConnDBEngines and NotConnDbEngines.$Server = "ServerA"$Database = "DatabaseA"$con = "server=$Server;database=$Database;Integrated Security=sspi"$cmd = "SELECT DISTINCT ServerName FROM dbo.Servers" $da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con) $dt = new-object System.Data.DataTable $da.fill($dt) | out-null foreach ($srv in $dt) { $ServerName = $srv.ServerName try { $ping = new-object System.Net.NetworkInformation.Ping $Reply = $ping.send($ServerName) $sql = "INSERT INTO dbo.ConnServers(ServerName) SELECT '$ServerName'" Invoke-SQLcmd -serverinstance $Server -database $Database -query $sql Write-Output "ping success $ServerName" } catch { $sql1 = "INSERT INTO NotConnServers(ServerName) SELECT '$ServerName'" Invoke-SQLcmd -serverinstance $Server -database $Database -query $sql1 Write-Output "can't connect to $servername" } }Thanks

Viewing all articles
Browse latest Browse all 467

Trending Articles