JM1
JM1

Reputation: 1715

PowerShell - How to locate an object(s) properties and methods? (They are not in the help files)

I have a few questions about the following code.

Get-DBAAgentJob -SqlInstance *instancename* | Where-Object { $_.HasSchedule -Match "False" }| Out-GridView
  1. In the Where-Object, there is $.HasSchedule. What is '$.HasSchedule'? I looked in the help for Where-Object and online and I don't understand what that is. Is it a function?
  2. What does this syntax signify/do $_ ?
  3. What all can I filter for in Where-Object other than .HasSchedule? Also,where I can find out how to figure that out please? If it's not in help or books online or a google search, I'm not sure. My google search algorithm is probably not good enough to get me in the ballpark.

I'm curious what are all the things I can filter on in the Where-Object in this line of code. For example, instead of has schedule, if I wanted to look where the job is not enabled, is there a .NotEnabled?

Thanks for the help.

Upvotes: 0

Views: 682

Answers (2)

JM1
JM1

Reputation: 1715

In case someone finds this useful in the future, @Theo's answer was helpful in giving me the base understanding of my question.

I learned more on this today and I will post it to help others in the future.

To answer my question:

  • The .hasSchedule is one of the many properties of Get-DBAAgentJob.
  • The . 'dot'. "The most common way to get the values of the properties of an object is to use the dot method." Books Online (BOL)
  • "All Properties and Methods for a given object are called members... Help files for any given command do not tell you what kinds of objects, properties, and methods are available, the only way to tell is to use the Get-Member' cmdlet. Learning PowerShell Jonathan Hassall
  • This code will tell you all of the properties and methods for a member, in this case the one I was interested in learning more about.

    Get-DBAAgentJob -SqlInstance instancename | get-member
    

This shows me all the properties and methods available, including hasSchedule and isenabled

enter image description here

BOL: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_properties?view=powershell-7

Upvotes: 0

Theo
Theo

Reputation: 61148

The Where-Object clause is a way to filter objects returned from a cmdlet on a certain property. In your example, it is filtering objects on the HasSchedule property. The example's filter says this property needs to be False in order for the objects to get piped through the pipeline where the next cmdlet takes them as input.

It tests the objects using the $_ Automatic variable, which represents each object in sequence that is coming in from the Get-DbaAgentJob cmdlet.

Usually, to find out what an object would look like, you can simply google for it.
In this case, if you look for Get-DBAAgentJob, you will find this page, where you can look at the function itself. Here you can find what properties each returned object has:

ComputerName, InstanceName, SqlInstance, Name, Category, OwnerLoginName, IsEnabled, LastRunDate, DateCreated, HasSchedule, OperatorToEmail.

As you can see, there is a property IsEnabled, so you can filter on Not enabled with

Where-Object { -not $_.IsEnabled }

See: PowerShell Logical Operators

If you click the home page for dbatools you'll fine a section called docs where you can learn more.

Browse for free ebooks on PowerShell

Hope that helps

Upvotes: 2

Related Questions