Build-FilterEquation function #29
Replies: 4 comments 1 reply
-
Thanks for sharing this! Advanced filters was one area I struggled with how to handle eloquently as I want to make it as easy as possible for the user without them having to have prior knowledge of how they work but also still follow the same filter logic of regular filtering. I like the idea of a filter builder/wizard for those that want more of a guide approach. Let me review this during the next module update and see if we can improve upon the existing implementation without breaking backwards compatibility, perhaps something like a separate -FilterWizard switch or something might work that saves the filter to an output variable so you can reuse it in automation scenarios without the interaction being required. |
Beta Was this translation helpful? Give feedback.
-
I use this to build the filters in advance, just to help with the syntax and escape characters and getting the quotes in all the right places. I save sample filters off to a separate file then I can just add them, as is, or tweak them for a script's specific purpose, and use them in my "normal" scripts. Unfortunately, since it is a "prompting" function, adding -FilterWizard to a script would require the "normal" script to prompt the user for input every time they ran their script which depending on the use case could prove quite annoying. I figured it might make more sense to drop this utility in your SE module so that people could build filters without all the JSON related syntactical issues. |
Beta Was this translation helpful? Give feedback.
-
The thought was to provide the switch as an interactive option for Get-LMDevice for users that don't know how to format the filter properly or want help building it. The existing -Filter would remain, that way you could use either one and the output of the FilterWizard would be an additional Filter variable that can be used if the end user wanted to run that command through automation in the future without requiring the input. |
Beta Was this translation helpful? Give feedback.
-
A variant of this has been introduced into the latest version of the module 7.0 along with a full documentation refresh. Thanks for your help in making the module better for everyone! |
Beta Was this translation helpful? Give feedback.
-
I am sure this can be improved but I was tired of getting my escaped JSON filters wrong for advanced filters, so I created a utility function to assist in building filters for the Logic.Module functions. Thought I would share the attached code. Below is a few use cases for creating filters of each type. Also I added a complex filter for quickly finding all groups under a specified group. without having to recursively get the groups.
Build-FilterEquation.txt
EXAMPLES USE CASES BELOW:
########################## 'P' use case ##########################
PS C:\PS Scripts> Build-FilterEquation
Enter property name (or press Enter to finish): systemProperties
Enter operator (-eq, -ne, -gt, -lt, -contains, -notcontains): -eq
Press 'V' for a simple value, 'J' for a JSON object, or 'P' for JSON property name...
Enter JSON property 'name': system.hoststatus
Do you want to add another condition? (Enter -and, -or, or press Enter to finish):
systemProperties.name -eq "system.hoststatus"
PS C:\PS Scripts>
########################## 'J' use case ##########################
PS C:\PS Scripts> Get-LMDeviceGroup -Filter (Build-FilterEquation)
Enter property name (or press Enter to finish): autoProperties
Enter operator (-eq, -ne, -gt, -lt, -contains, -notcontains): -eq
Press 'V' for a simple value, 'J' for a JSON object, or 'P' for JSON property name...
Enter JSON property 'name': auto.productvendor
Enter JSON property 'value': Cisco
Do you want to add another condition? (Enter -and, -or, or press Enter to finish):
autoProperties -eq "{"name":"auto.productvendor","value":"Cisco"}"
PS C:\PS Scripts>
########################## 'V' use case ##########################
PS C:\PS Scripts> Get-LMDevice -Filter (Build-FilterEquation)
Enter property name (or press Enter to finish): deviceType
Enter operator (-eq, -ne, -gt, -lt, -contains, -notcontains): -eq
Press 'V' for a simple value, 'J' for a JSON object, or 'P' for JSON property name...
Enter value: 1
Do you want to add another condition? (Enter -and, -or, or press Enter to finish):
deviceType -eq "1"
PS C:\PS Scripts>
Filter to get all groups under a specified group folder
PS C:\Users\CFS9256.LPH> Get-LMDeviceGroup -Filter (Build-FilterEquation)
Enter property name (or press Enter to finish): fullPath
Enter operator (-eq, -ne, -gt, -lt, -contains, -notcontains): -eq
Press 'V' for a simple value, 'J' for a JSON object, or 'P' for JSON property name...: v
Enter value: /<2ndLevelGroup>
Do you want to add another condition? (Enter -and, -or, or press Enter to finish): -or
Enter property name (or press Enter to finish): fullPath
Enter operator (-eq, -ne, -gt, -lt, -contains, -notcontains): -eq
Press 'V' for a simple value, 'J' for a JSON object, or 'P' for JSON property name...: v
Enter value: /<2ndLevelGroup>/*
Do you want to add another condition? (Enter -and, -or, or press Enter to finish):
this is the filter created by the Build-FilterEquation above and applied replace <> with actual group names
fullPath -eq "/<2ndLevelGroup>" -or fullPath -eq "/<2ndLevelGroup>/*"
Beta Was this translation helpful? Give feedback.
All reactions