Using methods on objects

OMG I forgot how to use the methods of objects.

I piped get-service to get-member and i see it has a method called pause. So what i want to do is select an object from the service objects and use the pause method on it but i forgot how! Maybe there are more ways to do this, so please post even if this is answered if you know more ways of doing this. Thanks!

OMG, really?

Powershell Using methods on objects.

I have been searching but i could not find the way i like to be doing this :frowning:

Anyone got an answer for me?

I found one way. Surrounding the command in parentheses and using .method on it. But this does not work for all cmdlets it seems. Any other way you guys do this?

There’re a few ways.

$Var = Do-Thing
Do-Thing | ForEach-Object -MemberName Property
Do-Thing | ForEach-Object -MemberName Method [param]

Methods always require parentheses to run, you can think of them like little scripts. Properties tend to be static values, and are referenced without parentheses. Methods can take parameters or arguments inside their parentheses.

If you call a method name without the parentheses, PowerShell will helpfully list its OverloadDefinitions – the different ways it can be called, with different sets of parameters (or none, sometimes).

The question is what are you trying to accomplish? If you look at what commands are available for services:

PS C:\Users\Rob> Get-Command "*-Service"

CommandType     Name                                               Version    Source                                                                                                                                   
-----------     ----                                               -------    ------                                                                                                                                   
Cmdlet          Get-Service                                  Microsoft.PowerShell.Management                                                                                                          
Cmdlet          New-Service                                  Microsoft.PowerShell.Management                                                                                                          
Cmdlet          Restart-Service                              Microsoft.PowerShell.Management                                                                                                          
Cmdlet          Resume-Service                               Microsoft.PowerShell.Management                                                                                                          
Cmdlet          Set-Service                                  Microsoft.PowerShell.Management                                                                                                          
Cmdlet          Start-Service                                Microsoft.PowerShell.Management                                                                                                          
Cmdlet          Stop-Service                                 Microsoft.PowerShell.Management                                                                                                          
Cmdlet          Suspend-Service                              Microsoft.PowerShell.Management                                                                                                          

For simplicity, you can see that you can pipe to Suspend rather then attempting to call a method:

Get-Service -Name SERVICENAME | Suspend-Service

This is the way to do it. But I had better luck with .Stop()

$a = get-service AdobeARMservice
Exception calling "Pause" with "0" argument(s): "Cannot pause AdobeARMservice service on computer '.'."
At line:1 char:1
+ $a.Pause()
+ ~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException