Monitoring SQL service on a remote computer and start or stop a local service based on the result - powershell

I have a local service which interacts with a SQL database.
This service stays up when SQL database goes down.
What I need is a PowerShell script that checks the remote SQL service and based on the result it must start or stop the local service.
Any help would be highly appreciated

You can check if SQL server port is open on a remote host.
See this answer for details about how to do this How to check Network port access and display useful message?
The port number is depending on SQL server you're using. For example, for MS SQL default port is 1433 and for MySQL - 3306.

Related

AWS RDS Postgresql Pgadmin - Server doesn't listen

I followed the aws tutorial found here.
Everything went smoothly up until connecting to the postgresql instance via pgadmin.
I entered the appropriate user/pw info and copy/pasted the address of the db appropriately.
The port is indeed 5432 on my aws dashboard.
I am receiving the following error message:
Server doesn't listen
The server doesn't accept connections: the connection library reports
could not connect to server: Operation timed out Is the server running on host "my_database_name.some_stuff.us-west-2.rds.amazonaws.com" (52.10.228.18) and accepting TCP/IP connections on port 5432?
If you encounter this message, please check if the server you're trying to contact is actually running PostgreSQL on the given port. Test if you have network connectivity from your client to the server host using ping or equivalent tools. Is your network / VPN / SSH tunnel / firewall configured correctly?
For security reasons, PostgreSQL does not listen on all available IP addresses on the server machine initially. In order to access the server over the network, you need to enable listening on the address first.
For PostgreSQL servers starting with version 8.0, this is controlled using the "listen_addresses" parameter in the postgresql.conf file. Here, you can enter a list of IP addresses the server should listen on, or simply use '*' to listen on all available IP addresses. For earlier servers (Version 7.3 or 7.4), you'll need to set the "tcpip_socket" parameter to 'true'.
You can use the postgresql.conf editor that is built into pgAdmin III to edit the postgresql.conf configuration file. After changing this file, you need to restart the server process to make the setting effective.
If you double-checked your configuration but still get this error message, it's still unlikely that you encounter a fatal PostgreSQL misbehaviour. You probably have some low level network connectivity problems (e.g. firewall configuration). Please check this thoroughly before reporting a bug to the PostgreSQL community.
Step 1
You are getting the same dialog I was seeing above. Crap!
Step 2
Go to your RDS instances
Step 3
Go to your security groups
Step 4
If your account was like mine you see this text:
Your account does not support the EC2-Classic Platform in this region.
DB Security Groups are only needed when the EC2-Classic Platform is supported.
Instead, use VPC Security Groups to control access to your DB Instances.
Go to the EC2 Console to view and manage your VPC Security Groups.
For more information, see AWS Documentation on Supported Platforms and Using RDS in VPC.
Step 5 Go back and check your RDS security group name (RDS->instances right click your instance). You will see something like Security GroupsList of VPC Security Groups associated with this DB Instance.
You will see something like:
default (sg-********) ( active )
Step 6 In your VPC security groups find your sg-******** that matches your database. Right click that. Edit inbound/outbound rules to add postgresql.
Try to connect again.
This solved my problem.
If this does not solve your problem I am very sorry, but I hope this documentation brings me some debugging karma.
go to AWS services in security group click on the security group id . from the "actions" button click on "edit inbound roles" and then change the "source" to "my ip"

Connecting SSMS client to remote SQL Server instance failing with 'AnyAny' firewall rule

How can I connect my SSMS client to a remote SQL Server 2008 R2 instance using SQL browser AND with Windows firewall enabled?
I find that that with the firewall turned off, I can connect fine (without specifying the instance name). Now when I turn the firewall (domain profile) back on, my connection attempts time out. So, to begin my troubleshooting, I created an 'AnyAny' firewall rule however it still failed with a timeout error.
Naturally I can connect fine either way (firewall turned on/off) when I specify the instance name and port. SQL browser is running and listening on UDP 1433. SQL Server is running Windows 2008 R2.
What is getting blocked? And why won't a custom 'AnyAny' rule bypass it?
The information on this SQLCAT blog might be helpful to you. By default, the SQL Browser service runs on UDP port 1434. If you have a firewall configured on your SQL server, you should open up port 1434 for inbound connections from SSMS.
If you need help setting remote connections to your SQL Server, this guide is spot on.

Google Cloud SQL VM refusing connection

I have been stuck trying to figure out why my Cloud SQL VM is refusing my connection from my machine (whom ip address I have added as a subnet). I cann SSH into the VM but i cannot access the VM from a browser to make SQLs. I have scoured the internet for days trying to find a fix but i cannot seem to get pass this point. My apache listens to port 80. Also Id like to add that I have been connecting to my Mysql db for months through php and making sqls so I do not believe the problem is with apache. However if it is please point me to where i should be looking.
It sounds like you have MySQL running on a GCE VM, not an actual CloudSQL instance (that is a different service from GCE). Is that right?
If so, then if you are trying to connect from your local machine directly to the mysql instance, you are probably getting blocked by the firewall. Go to the networks tab (under Compute Engine) on the cloud console and see what firewall rules you have enabled. You might need to add one for 3306 or whatever port you are using.

Sql Server Times Out Twice - Connects on 3rd Attempt

I have a WinForms application installed on multiple PCs in an office, and a SQL Server 2012 Express database on the server to which the client application connects.
Each machine fails to connect on the first two attempts giving an error -
Timeout Expired: The timeout period elapsed prior to the completion of the operation or the server is not responding.
However, it always works on the 3rd attempt on all machines!
The server is SBS 2008, the machines are running Windows 7.
The issue was I had used a Named Instanced on SQL Server which by default uses dynamic ports. Hence each connection attempt used a different Port, and each time I was asking the server administrator to allow additional ports. The successful log ins where simply because the dynamic port chosen just so happened to be one previously allowed.
The answer was to use SQL Server Configuration Manager to remove the dynamic port setting and specify a single port to use for all connection attempts, and ensure firewalls etc had an exception for that particular port.

Specify port when calling WMI in powershell?

WMI queries run off port 135 (from what google tells me). Can you specify a different port when trying to do a WMI query?
Alternatively, can you use the system.managment.managementScope object and specify a port when connecting? Perhaps like this:
\\computername:port\root\cimv2\
If the answer to both of these is "no," then can you connect a different way and once connected, run a wmi query?
Reason for this question:
I have a mixed environment, and I need to query some servers behind a firewall. I cannot specify a timeout value for WMI queries, so I need a way to connect to these boxes without a new firewall rule.
WMI uses port 135 to negotiate, then the DCOM selects a random port between 1024 and 65535. Making it hard to for a fixed port. Not very friendly for firewalls...
As a comment from Jeff, said above you have to tell the machine you are connecting to, to limit the port it uses.
In windows server 2003 you could not limit the port which was chosen, so for those I run my script on that server and then push the results back on known port. My script uses the standard SQL port as all information is stored on the database.
For windows server 2008 R2 you can limit the port it uses, this can be done by running the following on each server;
At the command prompt, type winmgmt -standalonehost
Stop the WMI service by typing the command net stop "Windows Management Instrumentation"
Restart the WMI service again in a new service host
by typing net start "Windows Management Instrumentation"
Establish a new port number for the WMI service by typing netsh firewall add
portopening TCP 24158 WMIFixedPort
Steps and more information can be found here (as Jeff gave):
Setting Up a Fixed Port for WMI
Its not the best solution but the only one I know of, I have been building scripts for a large server environment for a long time now. The way I have always handled it is to have a script in each location (DMZ etc) and then push the data back on a known port.
I hope you find this useful.