Solved

Powershell

Posted on 2011-09-02
18
997 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
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.

 
LVL 53

Expert Comment

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

~bp
0
 
LVL 53

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Shrink automation 2 35
Exchange 2010 internal URLs 2 40
powershell question need assistance 10 26
invoke-command and passing local variable to remote 5 17
Synchronize a new Active Directory domain with an existing Office 365 tenant
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

12 Experts available now in Live!

Get 1:1 Help Now