Solved

Start Windows Firewall With Script

Posted on 2010-11-09
14
743 Views
Last Modified: 2012-05-10
Hi Guys,

I need to setup a scheduled task to check up on the windows firewall on my ISA server. Need to set this up as a temp solution, seeing that it seems to stop once or twice a week.

Can anyone give me any help with the script? basically it has to check if the windows firewall is started if not it should start it.

Help!

 thanks!
0
Comment
Question by:YOlanie_Visser
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 3

Expert Comment

by:sncameron
ID: 34099547
Use a batch file and the Net command:

net start MpsSvc

Would start the firewall service. Note that you need to run the batch file as an administrator in order to have access to service control.

You can start and stop any service using the same approach:
net start Service
net stop Service
net pause Service
net continue Service

To find the name of a service run the Services Manager (Start->Run->services.msc), right click on a service and choose Properties. The Service Name will be displayed at the top of the properties dialog.

0
 

Author Comment

by:YOlanie_Visser
ID: 34099559
Is there anyway to get it to check if its stopped before it starts it? I don't want restart it every hour.
0
 
LVL 15

Expert Comment

by:DonConsolio
ID: 34099573
check:
netsh firewall show opmode

start:
netsh firewall set opmode enable

stop:
netsh firewall set opmode disable

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:YOlanie_Visser
ID: 34099790
I assume the commands above are to enable it or disable it? I just need to start it if it has stopped...
it is going to be enabled be default, all it has to do is check if it is stopped..if yes...then start
0
 
LVL 3

Accepted Solution

by:
sncameron earned 500 total points
ID: 34099905
Using a batch file:

call wmic /locale:ms_409 service where (name="wsearch") get state /value | findstr State=Running
if %ErrorLevel% EQU 0 (
    echo "Service is running"
) else (
    echo "Service is stopped"
    # Not running, start it
    net start MpsSvc
)

Using Powershell (which is the approach that I would take if at all possible):

$serviceName = "ServiceName";
$serviceStatus = (get-service "$serviceName").Status;

if ($serviceStatus -eq "Running") {
    echo "Service is Running";
}
else {
    #Could be Stopped, Stopping, Paused, or even Starting...
    echo "Service is $serviceStatus";
    Start-Service $serviceName
    echo "Service is $serviceStatus";
}
0
 

Author Comment

by:YOlanie_Visser
ID: 34101232
Sorry guys I'm quite new to all this, if the service is " Microsoft Firewall" do i put it as fwsrv?
do i substitute only the service name?
Would i run this as a VBS?

$serviceName =" Microsoft Firewall";
$serviceStatus = (get-service "$serviceName").Status;

if ($serviceStatus -eq "Running") {
    echo "Service is Running";
}
else {
    #Could be Stopped, Stopping, Paused, or even Starting...
    echo "Service is $serviceStatus";
    Start-Service $serviceName
    echo "Service is $serviceStatus";
}
0
 
LVL 3

Expert Comment

by:sncameron
ID: 34101351
To find the name of a service run the Services Manager (Start->Run->services.msc), right click on a service and choose Properties. The Service Name will be displayed at the top of the properties dialog.

The Windows Firewall service is MpsSvc.

You need Windows Powershell installed to run the script (http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx):

$serviceName = "MpsSvc";
$serviceStatus = (get-service "$serviceName").Status;

if ($serviceStatus -eq "Running") {
    echo "Service is Running";
}
else {
    #Could be Stopped, Stopping, Paused, or even Starting...
    echo "Service is $serviceStatus";
    Start-Service $serviceName
    echo "Service is $serviceStatus";
} or even Starting...
    echo "Service is $serviceStatus";
    Start-Service $serviceName
    echo "Service is $serviceStatus";
}
0
 

Author Comment

by:YOlanie_Visser
ID: 34101759
(Please see the image below) its Microsoft firewall and not Windows firewall

if I had to run it as a batch file would it be the following:

call wmic /locale:ms_409 service where (name="wsearch") get state /value | findstr State=Running
if %ErrorLevel% EQU 0 (
    echo "Service is running"
) else (
    echo "Service is stopped"
    # Not running, start it
    net start Microsoft Firewall
)


fw.jpg
0
 
LVL 3

Assisted Solution

by:sncameron
sncameron earned 500 total points
ID: 34101853

call wmic /locale:ms_409 service where (name="fwsrv") get state /value | findstr State=Running
if %ErrorLevel% EQU 0 (
   echo "Service is running"
) else (
   echo "Service is stopped"
   # Not running, start it
   net start Microsoft Firewall
)
0
 
LVL 51

Expert Comment

by:Keith Alabaster
ID: 34106545
These are server services - have you edited the service properties and instructed the service to restart automatically?
0
 

Author Comment

by:YOlanie_Visser
ID: 34109247
I have but not quite sure it works. i set it to restart the service and to restart 1min after. if i stop the service to test it, it does not come back up...will it only work in case of failure?
0
 
LVL 3

Expert Comment

by:sncameron
ID: 34109256
Yes, it will only work if the service fails. It won't restart a clean shutdown of the service.
0
 
LVL 51

Expert Comment

by:Keith Alabaster
ID: 34109270
as you'd expect......

You'd be pretty miffed if you deliberately stopped the service manually and five seconds later it kept restarting itself.
0
 

Author Comment

by:YOlanie_Visser
ID: 34109441
thanks guys.

Will set everything up
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So you have two Windows Servers and you have a directory/folder/files on one that you'd like to mirror to the other?  You don't really want to deal with DFS or a 3rd party solution like Doubletake. You can use Robocopy from the Windows Server 200…
Many of us need to configure DHCP server(s) in their environment. We can do that simply via DHCP console on server or using MMC snap-in on each computer with Administrative Tools installed in a network. But what if we have to configure many DHCP ser…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question