This topic contains 6 replies, has 5 voices, and was last updated by
August 13, 2018 at 2:13 pm #108338
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!
August 13, 2018 at 2:35 pm #108371ParticipantPoints: 1,086Rank: Community Hero
August 13, 2018 at 6:06 pm #108386
I have been searching but i could not find the way i like to be doing this 🙁
Anyone got an answer for me?
August 13, 2018 at 6:10 pm #108389
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?
August 13, 2018 at 6:21 pm #108391ParticipantPoints: 438Rank: 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).
August 13, 2018 at 6:31 pm #108394ParticipantPoints: 632Rank: 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 188.8.131.52 Microsoft.PowerShell.Management Cmdlet New-Service 184.108.40.206 Microsoft.PowerShell.Management Cmdlet Restart-Service 220.127.116.11 Microsoft.PowerShell.Management Cmdlet Resume-Service 18.104.22.168 Microsoft.PowerShell.Management Cmdlet Set-Service 22.214.171.124 Microsoft.PowerShell.Management Cmdlet Start-Service 126.96.36.199 Microsoft.PowerShell.Management Cmdlet Stop-Service 188.8.131.52 Microsoft.PowerShell.Management Cmdlet Suspend-Service 184.108.40.206 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
August 13, 2018 at 6:41 pm #108395ParticipantPoints: 698Rank: Major 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.