Friday, July 9, 2010

Stop/Start or Enable/Disable Service – PowerShell Script

You can use the following PowerShell script to Start, Stop, Enable or Disable Windows services on a remote machine. An input file can be used to provide the computer names.

image

Download

Updated Script - http://portal.sivarajan.com/2010/07/stopstart-or-enabledisable-service_26.html

14 comments:

Hi Santhosh,
I'm from Brazil and i need to start mom service in more than 300 machine after patched them.
I'm new in scripts so, if i save this line as txt file and a csv file. That's all i need?

Thanks

This is a PowerShell script so you need to rename the txt file extension to .PS1

Input.csv file contains all computer names.

Also, I have updated script on the following location. You can enter additional credentials if needed.

http://portal.sivarajan.com/2010/07/stopstart-or-enabledisable-service_26.html

Hi Siva,

This is Sunil from Toronto, need help in writing a script
1. Services to be stopped and upon completion need to create a file (services stopped) in a folder i point (Stop).
2. Also need to automatically start the services up on reading a file (Services started)in a folder i point (Start)

Is this possible? help me.

Take a look at the following updated script.

http://portal.sivarajan.com/2011/05/stop-start-disable-service.html

I must be doing something wrong when I use your scripts.
I changed the $service = "regsvc"
I removed the first two green lines since I'm waiting to start the service.
Also, when I first ran the script I received an error "Cannont validate argument on parameter 'ComputerName'. So I assumed I had to put ComputerName as the heading in the input.csv.

When I re-ran the script I get the following error "The RPC server is unavailable". (Line 9 char: 16)

Then I noticed you updated the script to add the credentials.
I still got the above error, plus "You cannot call a method on a null-valued expression"

So I must be doing something extremely wrong!

Please help.

Thanks

Yes. The header must be ComtuterName.

>> The RPC server is unavailable

Sounds like script can’t access the remote computer. Can you browse \\computer\admin$? If not, you won’t be able to manage this computer remotely.

How would I run this like a shortcut (want to place a shortcut in my desktop that runs this script)? If I double click it, it opens up in Notepad, only when I right click, then select Run with Powershell, than it runs. Also, it closes so fast that you cannot view any messages.

This comment has been removed by the author.

Hi Santhosh,

I am getting this error message while trying to execute the PS file. ( At C:\scripts\AutoStopServices.ps1:2 char:37
+ Import-CSV D:\Scripts\input.csv | % {
+ ~
Missing closing '}' in statement block or type definition.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndCurlyBrace
)

I have created input.csv file which has Computername as header and listed all the servers ip adddresses.

Plz advise what needs to be done to fix this error.

thanks for your help.

Please post your complete script here. I think you are missing one }

Please have a look at the script.

$service = "wuauserv"
Import-CSV D:\Scripts\input.csv | % {
$computer = $_.ComputerName
#$result = (gwmi win32_service -computername $computer -filter "name='$service'").stopservice()}
#
$result = (gwmi win32_service -computername $computer -filter "name='$service'").stopservice()
#$result = (gwmi win32_service -computername $computer -filter "name='$service'").ChangeStartMode("Disabled")
#$result = (gwmi win32_service -computername $computer -filter "name='$service'").ChangeStartMode("Automatic")

Input.csv contains computer IP addresses not Name

Input.csv file has header as following.

ComputerName
192.168.1.1
192.168.2.1

One more thing Santhosh, i have multiple services which needs to be stopped how would i add multiple services in this script?

I appreciate your response and helping people.

Regards,
Fawad

This comment has been removed by the author.

Do you have example of the .csv file? I tried to use Excel to create one and it doesn't seem work

I can't get script to work. Thanks in advance.

My CSV content is:

ComputerName
Computer1-pc-d

Thanks,
Tom

$service = "bbagent"
Import-CSV F:\Scripts\input.csv | % {
$computer = $_.ComputerName
$result = (gwmi win32_service -computername $computer -filter "name='$service'").stopservice()
$result = (gwmi win32_service -computername $computer -filter "name='$service'").ChangeStartMode("Disabled")}

Post a Comment

Popular Posts

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More