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.
This topic is 15 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jeremyg
Posts: 23
Last visit: Mon Mar 18, 2024 6:07 am

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: 23
Last visit: Mon Mar 18, 2024 6:07 am

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
Last visit: Mon Oct 19, 2015 9:21 am

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: 23
Last visit: Mon Mar 18, 2024 6:07 am

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
Last visit: Mon Oct 19, 2015 9:21 am

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
Last visit: Mon Oct 19, 2015 9:21 am

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 23
Last visit: Mon Mar 18, 2024 6:07 am

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
Last visit: Fri Oct 30, 2009 5:38 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
Last visit: Mon Oct 19, 2015 9:21 am

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()
This topic is 15 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked