Solved

Powershell

Posted on 2011-09-02
18
992 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 51

Expert Comment

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

~bp
0
 
LVL 51

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

18 Experts available now in Live!

Get 1:1 Help Now