Dynamically populate Custom Tool fields with PowerShell

You are here:

When delegating administrative tasks using Custom Tools, you may need to query or modify information before the web form is presented to the user. The possibilities are limitless, but some examples of this might be:

  • Getting a list of available datastores before provisioning a VM
  • Enumerating a filtered list of shares on a remote host to allow a Help Desk person to grant access
  • Allow an application support tech to run more specific troubleshooting tools based on quick preliminary, real-time analysis
  • Show a list of tickets for a server from an external ticketing system, then force a valid ticket before performing an API action

Example with code

In the following example, we need a tool that will allow a user to see a real-time list of Windows services on a remote computer and then choose to view, start or stop one of those services. We’ll need to do the following:

  1. Create the custom fields needed to capture user input
  2. Create a preprocessor custom tool to query Windows services and dynamically populate a custom field
  3. Create the main custom tool that a delegated user will run to manage Windows services

First, create two ListBox custom fields called ServiceAction and ServiceListBox by going to Settings > Custom fields > New

For the ServiceAction field, add the following static lookup values.

For ServiceListBox, add a dummy lookup value in case the preprocessor script doesn’t grab the desired data.

Once added, you should see both custom fields listed like below.

Next, save the following PowerShell snippet as PreprocessorSample.ps1.

Add your new preprocessor script by going to Tools > Create Tool. Give it a name like “Preprocessor Example” and choose the Preprocessor category (this is important).

Be sure to add permissions for roles that will need to run this tool. You’ll also want to explicitly map credentials for it, as this will be the context it runs under.

Now, save the following PowerShell snippet as DynamicFieldSample.ps1.

This is the main custom tool that a delegated user will run. Add it to System Frontier by going to Tools > Create Tool. Name it “Dynamic Field Sample” and choose a meaningful category, but not Preprocessor.

Click on the Modify button under User Input Fields, select the two custom fields you added earlier, click Add then Done to return to the tool configuration screen.

Modify your Arguments section to capture the current hostname and user input fields in order to pass their values to the command-line of this tool.

Under Preprocessor, choose the “Preprocessor Sample” script that you added earlier. Only custom tools with a category of Preprocessor will appear in this list.

When you’re done, it should look similar to the screenshot below.

 

Let’s test it

Now, search for a computer in System Frontier, click on it and then click on the Tools tab. You should see the new Dynamic Field Sample tool. Click Open. Here’s what’s happening behind the scenes:

  1. The main tool calls the preprocessor tool, passing it information it needs like the list of target hostnames and custom fields.
  2. The preprocessor script performs it’s magic, updates all the data that was passed in a returns it to the main tool.
  3. The main tool then uses relevant data that was returned to populate custom fields before the web form is presented to the user.
    1. Notice the ServiceListBox field has the list of all Windows services that were returned by the preprocessor tool!
  4. The user makes choices or adds information, if needed and runs the tool.
  5. All values are passed to the command-line of your script and run under elevated credentials.

In this example, starting the SNMP Trap service successfully should return output to the web console like so:

If designed correctly, preprocessor scripts and custom fields can be reusable and modular. All this without the need to write HTML or wire up Windows forms controls.

Don’t forget, you can run custom tools on a list of hostnames, as long as they exist in SF, and the tool will automatically take advantage of multi-threading and parallel execution without additional code.

 

Views: 50