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.
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.
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.
Stupid Question? Cant execute method.
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
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.
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.
You could always write a function that takes a service name and method if you wanted to simplify all the typing.
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
Stupid Question? Cant execute method.
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.
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.
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.
Now THAT is interesting!!!
However I don't quite understand the addition of the & and b in "&sb".
However I don't quite understand the addition of the & and b in "&sb".
Stupid Question? Cant execute method.
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.
That is really neat though! Thanks for that little example, Jeff.
Stupid Question? Cant execute method.
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()