DBEngine Access check

  • 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 we

    have 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"

    }

    }

    Any suggestions plz

    Thanks

  • I am not sure what in your script isn't working. A little more detail please.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • The above script is working perfectly but it only checks the RDP access for a server and does not check if we have DB access.How I enhance the above code to check both the RDP access and also the Db access for the servers/db's and insert the DB accessible servers into tableA and Db non-accessible servers to TableB similar to the above code where RDP accessible servers are inserted to one table and the not accessible servers are inserted into another table.

    Hope I have made my request clear

    thanks

  • I think that all you need for various levels of checking can be found here: http://www.powershellmagazine.com/2014/07/21/using-powershell-to-discover-information-about-your-microsoft-sql-servers/[/url].

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply