Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell

Posted on 2011-09-02
18
Medium Priority
?
1,024 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
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

 
LVL 59

Expert Comment

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

~bp
0
 
LVL 59

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
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ā€¦
Screencast - Getting to Know the Pipeline

773 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