June 17, 2016 at 4:21 am
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
June 21, 2016 at 1:20 am
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!!!
June 23, 2016 at 5:51 am
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
June 23, 2016 at 6:25 am
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply