Using methods on objects

Welcome Forums General PowerShell Q&A Using methods on objects

This topic contains 6 replies, has 5 voices, and was last updated by

js
 
Participant
4 months ago.

  • Author
    Posts
  • #108338

    Participant
    Points: 0
    Rank: Member

    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!

  • #108371

    Participant
    Points: 261
    Helping Hand
    Rank: Contributor
    • #108386

      Participant
      Points: 0
      Rank: Member

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

      Anyone got an answer for me?

  • #108389

    Participant
    Points: 0
    Rank: Member

    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?

  • #108391

    Participant
    Points: 290
    Helping Hand
    Rank: Contributor

    There're a few ways.

    #1
    $Var = Do-Thing
    $Var.Property
    $Var.Method([param])
    #2
    (Do-Thing).Property
    (Do-Thing).Method([param])
    #3
    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).

  • #108394

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    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                                        3.1.0.0    Microsoft.PowerShell.Management                                                                                                          
    Cmdlet          New-Service                                        3.1.0.0    Microsoft.PowerShell.Management                                                                                                          
    Cmdlet          Restart-Service                                    3.1.0.0    Microsoft.PowerShell.Management                                                                                                          
    Cmdlet          Resume-Service                                     3.1.0.0    Microsoft.PowerShell.Management                                                                                                          
    Cmdlet          Set-Service                                        3.1.0.0    Microsoft.PowerShell.Management                                                                                                          
    Cmdlet          Start-Service                                      3.1.0.0    Microsoft.PowerShell.Management                                                                                                          
    Cmdlet          Stop-Service                                       3.1.0.0    Microsoft.PowerShell.Management                                                                                                          
    Cmdlet          Suspend-Service                                    3.1.0.0    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
    
  • #108395
    js

    Participant
    Points: 319
    Helping Hand
    Rank: Contributor

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

    $a = get-service AdobeARMservice
    $a.Pause()
    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
    $a.Stop()

The topic ‘Using methods on objects’ is closed to new replies.