Solved

Start Windows Firewall With Script

Posted on 2010-11-09
14
746 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I save DHCP Scopes and Reservations in PowerShell? 18 91
Dentrix G4 1 104
Event ID: 2005 / Source: Microsoft-Windows-PerfNet 4 136
Downgrade From Domain to WorkGroup 3 113
This may not be a text book method to resolve VSS backup issues but it seemed to have worked on few of the Windows 2003 servers we had issues while performing a Volume Shadow Copy backup. If you have issues while performing a shadow copy backup usin…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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