Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.


Go Back   Vista Forums > Misc Newsgroups > PowerShell

RB

Vista - Pipeline stop-service with -force

Reply
 
07-04-2007   #1
Julian Bowker


 
 

Pipeline stop-service with -force

The following works fine if the -force switch is not used, but fails with
the -force switch. I suspect that I need to place the service name parameter
between the stop-service and the -force but I can't ascertain the syntax?

get-service | where { $_.status -eq "running" -and $_.name -eq "MyService"}
| stop-service -force

Regards

Julian



My System SpecsSystem Spec
07-04-2007   #2
Brandon Shell


 
 

Re: Pipeline stop-service with -force

Hmmm.. Works for me
25# get-service | where { $_.status -eq "running" -and $_.name -eq
"WSearch"} | stop-service -force
WARNING: Waiting for service 'Windows Search (WSearch)' to finish
stopping...
WARNING: Waiting for service 'Windows Search (WSearch)' to finish
stopping...
26#

"Julian Bowker" <marblesteps@removethis.btinternet.com> wrote in message
news:uvVO$NovHHA.1360@TK2MSFTNGP02.phx.gbl...
> The following works fine if the -force switch is not used, but fails with
> the -force switch. I suspect that I need to place the service name
> parameter between the stop-service and the -force but I can't ascertain
> the syntax?
>
> get-service | where { $_.status -eq "running" -and $_.name -eq
> "MyService"} | stop-service -force
>
> Regards
>
> Julian
>


My System SpecsSystem Spec
07-04-2007   #3
dreeschkind


 
 

RE: Pipeline stop-service with -force

"Julian Bowker" wrote:

> The following works fine if the -force switch is not used, but fails with
> the -force switch.


Any error message?

> I suspect that I need to place the service name parameter
> between the stop-service and the -force but I can't ascertain the syntax?


This might be an alternative:

get-service | where { $_.status -eq "running" -and $_.name -eq "MyService"}
| foreach-object {stop-service -name $_.name -force}

--
greetings
dreeschkind
My System SpecsSystem Spec
07-05-2007   #4
Jacques Barathon [MS]


 
 

Re: Pipeline stop-service with -force

"dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
news:F95D4610-4245-49CB-BE4C-7009E5CF9ECD@microsoft.com...
....
> This might be an alternative:
>
> get-service | where { $_.status -eq "running" -and $_.name -eq
> "MyService"}
> | foreach-object {stop-service -name $_.name -force}


By the way, you will notice that you cannot just pass $_ to stop-service in
your foreach block. I just opened a bug on Connect about it:
https://connect.microsoft.com/feedba...6152&SiteID=99

Jacques

My System SpecsSystem Spec
07-05-2007   #5
klumsy@xtra.co.nz


 
 

Re: Pipeline stop-service with -force


> By the way, you will notice that you cannot just pass $_ to stop-service in
> your foreach block. I just opened a bug on Connect about it:https://connect.microsoft.com/feedba...x?FeedbackID=2...
>
> Jacques


too late for me to experiement but did you try

foreach-object {stop-service -name $($_.name) -force}

it could be not that you can't pass $_ but that if its an expression
as a parameter then it might have to be executed as such.. not sure.



My System SpecsSystem Spec
07-05-2007   #6
Jacques Barathon [MS]


 
 

Re: Pipeline stop-service with -force

<klumsy@xtra.co.nz> wrote in message
news:1183619090.985263.217500@g37g2000prf.googlegroups.com...
>
>> By the way, you will notice that you cannot just pass $_ to stop-service
>> in
>> your foreach block. I just opened a bug on Connect about
>> it:https://connect.microsoft.com/feedba...x?FeedbackID=2...
>>
>> Jacques

>
> too late for me to experiement but did you try
>
> foreach-object {stop-service -name $($_.name) -force}
>
> it could be not that you can't pass $_ but that if its an expression
> as a parameter then it might have to be executed as such.. not sure.


My point was that you *have to* use $_.name instead of $_ directly as you
would do with another cmdlet eg copy-item: "dir *.txt | % {copy-item $_}"
works without having to specify $_.name or $_.path or whatever property of
the FileInfo object.

It seems to me that stop-service does not support it because it accepts
pipeline input by value only. If it was supporting pipeline input by
property name it would know what to do with the ServiceController object
instead of only expecting a string as the name. As a workaround you can
explicitly specify the -inputobject parameter:

get-service ws* | foreach {stop-service -inputobject $_}

I may be wrong about the root cause of the problem, but whatever is causing
it I thought the issue was worth raising it as a bug.

Jacques


My System SpecsSystem Spec
07-05-2007   #7
dreeschkind


 
 

Re: Pipeline stop-service with -force

Correct, it would be better if it stop-service (and probably all *-service)
cmdlets accepted $_ directly.

I also noticed that the ToString method of a service produces a different
result than the ToString method of a process (i.e. it does not contain the
name). This seems to be a .NET framework inconsistency, though.

PS> (get-service wuauserv).ToString()
System.ServiceProcess.ServiceController

PS> (get-process explorer).ToString()
System.Diagnostics.Process (EXPLORER)

--
greetings
dreeschkind

"Jacques Barathon [MS]" wrote:

> <klumsy@xtra.co.nz> wrote in message
> news:1183619090.985263.217500@g37g2000prf.googlegroups.com...
> >
> >> By the way, you will notice that you cannot just pass $_ to stop-service
> >> in
> >> your foreach block. I just opened a bug on Connect about
> >> it:https://connect.microsoft.com/feedba...x?FeedbackID=2...
> >>
> >> Jacques

> >
> > too late for me to experiement but did you try
> >
> > foreach-object {stop-service -name $($_.name) -force}
> >
> > it could be not that you can't pass $_ but that if its an expression
> > as a parameter then it might have to be executed as such.. not sure.

>
> My point was that you *have to* use $_.name instead of $_ directly as you
> would do with another cmdlet eg copy-item: "dir *.txt | % {copy-item $_}"
> works without having to specify $_.name or $_.path or whatever property of
> the FileInfo object.
>
> It seems to me that stop-service does not support it because it accepts
> pipeline input by value only. If it was supporting pipeline input by
> property name it would know what to do with the ServiceController object
> instead of only expecting a string as the name. As a workaround you can
> explicitly specify the -inputobject parameter:
>
> get-service ws* | foreach {stop-service -inputobject $_}
>
> I may be wrong about the root cause of the problem, but whatever is causing
> it I thought the issue was worth raising it as a bug.
>
> Jacques
>
>
>

My System SpecsSystem Spec
07-05-2007   #8
Julian Bowker


 
 

Re: Pipeline stop-service with -force

I now suspect the problem lies elsewhere, running the following gives the
error as shown below. The fact that JBoss is running within a JVM may be
complicating things. Strangely, the service does stop even tho' it throws
the error. My original syntax works correctly as do the suggested
alternatives when run against other Windows services.

Regards

Julian

PS C:\PSscripts> get-service | where { $_.status -eq "running" -and
$_.name -eq
"JBoss"} | foreach-object {stop-service -name $_.name -force}
Stop-Service : Service 'JBoss (JBoss)' cannot be stopped due to the
following e
rror: Cannot stop JBoss service on computer '.'.
At line:1 char:103
+ get-service | where { $_.status -eq "running" -and $_.name -eq "JBoss"} |
for
each-object {stop-service <<<< -name $_.name -force}
PS C:\PSscripts>


"Julian Bowker" <marblesteps@removethis.btinternet.com> wrote in message
news:uvVO$NovHHA.1360@TK2MSFTNGP02.phx.gbl...
> The following works fine if the -force switch is not used, but fails with
> the -force switch. I suspect that I need to place the service name
> parameter between the stop-service and the -force but I can't ascertain
> the syntax?
>
> get-service | where { $_.status -eq "running" -and $_.name -eq
> "MyService"} | stop-service -force
>
> Regards
>
> Julian
>



My System SpecsSystem Spec
07-05-2007   #9
dreeschkind


 
 

Re: Pipeline stop-service with -force

First of all, I think the following command does the same and is easier to
type.
You can specify the service name as a parameter of get-service. Stop-Service
will ignore services that are already stopped, so you don't need to use where
at all.

PS> get-service JBoss | stop-service -force

You can also use aliases and short parameter names:

PS> gsv JBoss | spsv -f


Regarding your JBoss error message, does stopping the service work using the
Management Console?
You might as well try the psservice.exe command line tool from sysinternals.

--
greetings
dreeschkind

"Julian Bowker" wrote:

> I now suspect the problem lies elsewhere, running the following gives the
> error as shown below. The fact that JBoss is running within a JVM may be
> complicating things. Strangely, the service does stop even tho' it throws
> the error. My original syntax works correctly as do the suggested
> alternatives when run against other Windows services.
>
> Regards
>
> Julian
>
> PS C:\PSscripts> get-service | where { $_.status -eq "running" -and
> $_.name -eq
> "JBoss"} | foreach-object {stop-service -name $_.name -force}
> Stop-Service : Service 'JBoss (JBoss)' cannot be stopped due to the
> following e
> rror: Cannot stop JBoss service on computer '.'.
> At line:1 char:103
> + get-service | where { $_.status -eq "running" -and $_.name -eq "JBoss"} |
> for
> each-object {stop-service <<<< -name $_.name -force}
> PS C:\PSscripts>
>
>
> "Julian Bowker" <marblesteps@removethis.btinternet.com> wrote in message
> news:uvVO$NovHHA.1360@TK2MSFTNGP02.phx.gbl...
> > The following works fine if the -force switch is not used, but fails with
> > the -force switch. I suspect that I need to place the service name
> > parameter between the stop-service and the -force but I can't ascertain
> > the syntax?
> >
> > get-service | where { $_.status -eq "running" -and $_.name -eq
> > "MyService"} | stop-service -force
> >
> > Regards
> >
> > Julian
> >

>
>
>

My System SpecsSystem Spec
07-05-2007   #10
Keith Hill


 
 

Re: Pipeline stop-service with -force

"Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
news:OC2IsRuvHHA.3356@TK2MSFTNGP03.phx.gbl...
>If it was supporting pipeline input by property name it would know what to
>do with the ServiceController object instead of only expecting a string as
>the name.


+1 for that enhancement request. Have you filed one yet? I'll vote for it.
:-)

--
Keith

My System SpecsSystem Spec
Reply

RB


Thread Tools


Similar Threads for: Pipeline stop-service with -force
Thread Forum
Possible Fix: Microsoft Force Feedback 2 Joystick and the self center force disabling Vista hardware & devices
how to stop service on vista General Discussion
How can I force a LockWorkstation from a Windows Service on Vista? Vista General
Stop-Service cmdlet PowerShell
How is it possible to execute start-service and stop-service through a Web-interface? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd