Solved

Powershell

Posted on 2011-09-02
18
1,004 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
[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
  • 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
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.

 
LVL 54

Expert Comment

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

~bp
0
 
LVL 54

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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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