Page 1 of 2

Stupid Question? Cant execute method.

Posted: Mon Jul 14, 2008 11:19 am
by jeremyg
I've just starting playing with Powershell again after several months of having no free time.

Came across something I don't understand and I'm probably just missing something obvious.

First I type: $a = get-service spooler
Then I type: $a | get-member

I see all the members of the spooler service including the methods:Start Method System.Void Start(), System.Void Start(String[] args)Stop Method System.Void Stop()

Problem is that I cannot seem to execute those methods. I understand that there is a start-service command.....but I want to understand why I cannot call those methods from a command line if I have the object created.

Or maybe I can use them and I'm just using the wrong syntax?

jeremyg2008-07-14 18:19:43

Stupid Question? Cant execute method.

Posted: Mon Jul 14, 2008 11:19 am
by jeremyg
I've just starting playing with Powershell again after several months of having no free time.

Came across something I don't understand and I'm probably just missing something obvious.

First I type: $a = get-service spooler
Then I type: $a | get-member

I see all the members of the spooler service including the methods:Start Method System.Void Start(), System.Void Start(String[] args)Stop Method System.Void Stop()

Problem is that I cannot seem to execute those methods. I understand that there is a start-service command.....but I want to understand why I cannot call those methods from a command line if I have the object created.

Or maybe I can use them and I'm just using the wrong syntax?

jeremyg2008-07-14 18:19:43

Stupid Question? Cant execute method.

Posted: Tue Jul 15, 2008 1:23 am
by jhicks
Variables are always "point in time. Use the Start-Service or Stop-Service cmdlets. Both of these cmdlets have a -passthru parameter which will return an object. If you want an actual return value then you should use WMI and the win32_service class.

Code: Select all

PS C:>[wmi]$svc="localhostrootcimv2:Win32_Service.Name='wsearch'"
PS C:> $svc.stopservice()__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 5
You could always write a function that takes a service name and method if you wanted to simplify all the typing.

Stupid Question? Cant execute method.

Posted: Tue Jul 15, 2008 11:40 am
by jeremyg
ahhh that is disheartening. I'm trying to do everything possible with "native" powershell stuff....just for the learning process. Then again I suppose learning what *can't* be done is just as good.

Stupid Question? Cant execute method.

Posted: Wed Jul 16, 2008 1:50 am
by jhicks
But PowerShell is always interactive so you don't really need a live variable. There are other ways to shortcut if that's what you are after. What is it you are trying to accomplish?

Stupid Question? Cant execute method.

Posted: Wed Jul 16, 2008 4:58 am
by jhicks
Here's an alternative that might meet your needs. Change the service name as needed.$s={get-service wsearch}To see the service type:&$sbTo stop it:(&$sb).stop()or start it:(&$sb).start()This is the closest I think you can get to a "live" variable.

Stupid Question? Cant execute method.

Posted: Wed Jul 16, 2008 5:07 am
by jvierra
Jeff

That's a pretty neat way to accomplish that. I like it. Very simple. Very PoSh.

Stupid Question? Cant execute method.

Posted: Wed Jul 16, 2008 8:37 am
by jeremyg
Now THAT is interesting!!!

However I don't quite understand the addition of the & and b in "&sb".

Stupid Question? Cant execute method.

Posted: Wed Jul 16, 2008 10:38 pm
by dmerida
The & is a call operator which allows you to execute whatever follows it. I'm not sure what the b is though.

That is really neat though! Thanks for that little example, Jeff.

Stupid Question? Cant execute method.

Posted: Wed Jul 16, 2008 11:50 pm
by jhicks
I wasn't consistent in post. Sorry. I was testing with a variable $sb but started the post with $s. Omit the b and it will work just fine.$s={get-service wsearch}&$s(&$s).stop()(&$s).start()