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

7 months, 1 week ago.

  • Author
  • #108338

    Points: 81
    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

    Points: 1,086
    Helping Hand
    Rank: Community Hero
    • #108386

      Points: 81
      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

    Points: 81
    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

    Points: 438
    Helping Hand
    Rank: Contributor

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

  • #108394

    Points: 632
    Helping Hand
    Rank: Major 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                                  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
  • #108395

    Points: 698
    Helping Hand
    Rank: Major Contributor

    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

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

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort