Solved

Powershell

Posted on 2011-09-02
18
995 Views
Last Modified: 2012-05-12
I need a powershell or vbscript that can stop a service on remote computers.

Paul
0
Comment
Question by:pauledwardian
  • 10
  • 4
  • 2
  • +1
18 Comments
 
LVL 14

Expert Comment

by:athomsfere
ID: 36474401
You can use SC
sc \\machinename stop <serviceName>

Or type SC /? in a command prompt for more information
0
 

Author Comment

by:pauledwardian
ID: 36474423
is this a powershell command or batch file command?
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36474489
It's a little tough since "stop-service" doesn't support remote computers.  You can however achieve this through the WMI framework:

$Computer = "."
$ServiceName = "WSearch"
(gwmi -computername $Computer -class win32_service | ?{$_.name -eq "$servicename"}).stopservice()

HTH,

Dale Harris
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 36474497
SC is a built in windows command, can be executed in a BAT file.

~bp
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 36474504
And of course powershell can execute SC since it's a built in Windows command.

~bp
0
 

Author Comment

by:pauledwardian
ID: 36474542
Thanks a lot Dale. But it gave me this error:

You cannot call a method on a null-valued expression.
0
 

Author Comment

by:pauledwardian
ID: 36474560
I used the following code:

$Computer = "computername"
$ServiceName = "Windows Firewall/Internet Connection Sharing (ICS)"
(gwmi -computername $Computer -class win32_service | ?{$_.name -eq "$servicename"}).stopservice()
0
 

Author Comment

by:pauledwardian
ID: 36474581
but it gave me this error:

You cannot call a method on a null-valued expression.
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36474641
You can't use the name that the service uses for english.  You have to use the property called Service Name.  For example, my WindowsFirewall service is actually:
MpsSvc

So go to properties under that service, and it's the first listing.

For Computer Name did you actually enter in a computer name or did you just put "computername"?

Dale Harris
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:pauledwardian
ID: 36474698
You are the man Dale. One last request and I will be very thankful from you.
I have a list of computers in text file. Can this code read the text file and uses the computer names from that text file?
It is on C:\computernames.txt

Thanks again
Paul
0
 

Author Comment

by:pauledwardian
ID: 36474733
I think I got it. Am I correct Dale?

$Computer = gc "C:\Computers.txt"
$ServiceName = "SharedAccess"
(gwmi -computername $Computer -class win32_service | ?{$_.name -eq "$servicename"}).stopservice()
0
 

Author Comment

by:pauledwardian
ID: 36474745
Can I change the service starup to Manual as well? Please?
0
 
LVL 14

Expert Comment

by:athomsfere
ID: 36474798
This should do it, but I can't test it right now.

$Servicename = "SharedAccess"

Get-Content 'C:\Computers.txt' `
 | Foreach-Object {(gwmi -computername $Computer -class win32_service | ?{$_.name -eq "$Servicename"}).stopservice()}

Open in new window

0
 

Author Comment

by:pauledwardian
ID: 36474810
I got it but thank you anyway.

$Computer = gc "C:\Computers.txt"
$ServiceName = "SharedAccess"
(gwmi -computername $Computer -class win32_service | ?{$_.name -eq "$servicename"}).stopservice()
(gwmi -computername $Computer -class win32_service | ?{$_.name -eq "$servicename"}).changeStartMode("Manual")



Can someone help me to add an or statment that if the service in windows 7 is called firewall then it also changes that to stop as well?
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36474857
You almost got it, but it wouldn't have gone through all the computers in your list.

$Computers = gc "C:\Computers.txt"
$ServiceNames = "SharedAccess" "MpsSvc" #MpsSvc is Windows Firewall in Windows 7
Foreach ($Computer in $Computers){
foreach ($ServiceName in $ServiceNames){
$Service = gwmi -computername $Computer -class win32_service | ?{$_.name -eq "$servicename"}
$Service.changestartmode("Manual")
$Service.StopService()
}
}

This would work like this:

Two computers: localhost and .

Two services: SharedAccess and MpsSvc

First iteration:
localhost service SharedAccess is set to manual and stopped

Second iteration:
localhost service MpsSvc is set to manual and stopped.

Then it goes through it twice again with . computer

Does that work?

DH
0
 
LVL 16

Accepted Solution

by:
Dale Harris earned 500 total points
ID: 36474869
$Computers = gc "C:\Computers.txt"
$ServiceNames = "SharedAccess","MpsSvc" #MpsSvc is Windows Firewall in Windows 7
Foreach ($Computer in $Computers){
foreach ($ServiceName in $ServiceNames){
$Service = gwmi -computername $Computer -class win32_service | ?{$_.name -eq "$servicename"}
$Service.changestartmode("Manual")
$Service.StopService()
}
}

Forgot the comma between the services.

DH
0
 

Author Comment

by:pauledwardian
ID: 36474958
Fantastic!
0
 

Author Closing Comment

by:pauledwardian
ID: 36474965
You are the man
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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 …

932 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now