Stupid Question? Cant execute method.

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
jeremyg
Posts: 22
Joined: Sun Mar 23, 2008 2:04 pm

Stupid Question? Cant execute method.

Post 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

User avatar
jeremyg
Posts: 22
Joined: Sun Mar 23, 2008 2:04 pm

Stupid Question? Cant execute method.

Post 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

User avatar
jhicks
Posts: 1789
Joined: Sun Jan 21, 2007 11:31 pm

Stupid Question? Cant execute method.

Post 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.

User avatar
jeremyg
Posts: 22
Joined: Sun Mar 23, 2008 2:04 pm

Stupid Question? Cant execute method.

Post 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.

User avatar
jhicks
Posts: 1789
Joined: Sun Jan 21, 2007 11:31 pm

Stupid Question? Cant execute method.

Post 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?

User avatar
jhicks
Posts: 1789
Joined: Sun Jan 21, 2007 11:31 pm

Stupid Question? Cant execute method.

Post 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.

jvierra
Posts: 14578
Joined: Tue May 22, 2007 9:57 am
Answers: 1
Been upvoted: 1 time
Contact:

Stupid Question? Cant execute method.

Post by jvierra »

Jeff

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

User avatar
jeremyg
Posts: 22
Joined: Sun Mar 23, 2008 2:04 pm

Stupid Question? Cant execute method.

Post by jeremyg »

Now THAT is interesting!!!

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

User avatar
dmerida
Posts: 16
Joined: Wed Apr 23, 2008 1:02 am

Stupid Question? Cant execute method.

Post 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.

User avatar
jhicks
Posts: 1789
Joined: Sun Jan 21, 2007 11:31 pm

Stupid Question? Cant execute method.

Post 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()

Locked