This article will walk you through the process of setting up the MachineMetrics CSV Downloader. Through the use of a PowerShell script and the Windows Task Scheduler, you can take advantage of the powerful data provided by the MachineMetrics Production API without the need to write complex code. When you are done with this step by step guide, you will have automated the process of downloading the exact data you want on the schedule that is important to you.
Prerequisites
- A Windows PC or Server that is reliably on during the times that you need the report to run
- PowerShell installed
- Administrator privileges to configure Task Scheduler
Download the Script
Click here to download the mm-csv-exporter.ps1 script.
Obtain an API Key
The API Key is a crucial component required by the script to fetch data from MachineMetrics. Follow the steps below to generate an API Key:
- Log into your MachineMetrics account.
- Navigate to
Settings
and selectAPI Keys
. - Click on
Create API Key
. - In the scope section, select
reporting
. - After generating the new API key, make sure to copy it and store it securely. We do not save a copy of the key in its original form. If you lose it, you will need to generate a new one. The API key has the same level of access as a password.
The PowerShell Script
The mm-csv-exporter.ps1
script requires several arguments. Below are details of the arguments you need to provide:
- API_KEY: The API key you generated from MachineMetrics.
- OUT_PATH: The full path to the file where the CSV output should be written. This location will be watched by other tools for changes to pick up the new file when it gets updated.
- LOG_PATH: The full path to the file where the script logs will be written. This file gets appended after each run of the script, logging both successful runs and errors.
- CONFIG_PATH (Optional): The full path to the configuration file that includes the query for the data and the period of time to be queried.
Configuration File Format
The configuration file determines the data to be gathered and the period of time to be queried. Here's an example:
{
"ProductionQuery": {
"exactRange": false,
"data": [
{ "metric": "goodParts" },
{ "metric": "totalParts" }
],
"groupBy": [
{ "group": "day" },
{ "group": "machine" },
{ "group": "jobOperation" }
],
"filter": {}
},
"Days": 7
}
For more details on the options available for the ProductionQuery
property, refer to the Production API documentation.
Please note that start
, end
, startDay
, and endDay
should be left out of the ProductionQuery
property. These are automatically filled in by the script based on the value provided for the Days
property in the configuration file.
The next step isn't required. If you want to use the mm-csv-exporter.ps1 in your other projects, take it and use it as you see fit. It is a great starting point for many projects.
Configure Task Scheduler
To set up the script so it runs in an automated fashion, use the Task Scheduler in Windows. You can set this up to run the PowerShell script at specific intervals.
- Search for
Task Scheduler
in your Windows search bar and open the application. - Click on
Create Basic Task...
and fill in the task name and description. - In the
Trigger
section, define how often you want the task to run. - In the
Action
section, selectStart a program
. For the program/script field, browse and selectpowershell.exe
. See below for a detailed example of what to put in theAdd arguments
field. - Click on
Finish
to save the task. - You can configure the task to run more often than Daily by first saving the task with the
Daily
option selected, editing the task and configuring theRepeat task every:
section underAdvanced settings
.for a duration of
should be set toIndefinitely
.
Note: This should not be configured to run more frequently than once an hour.
Setting the Add Arguments field
Here is a detailed example of the value of that should be entered into the Add arguments
field.
-ExecutionPolicy Bypass -File "C:\path\to\mm-csv-exporter.ps1" -API_KEY "YOUR_API_KEY" -OUT_PATH "C:\path\to\output\output.csv" -LOG_PATH "C:\path\to\log\log.txt" -CONFIG_PATH "C:\path\to\config\config.json"
Replace C:\path\to\...
with your actual file paths. Make sure to include the quotation marks (" ") around each path, as they can contain spaces or special characters.
Conclusion
Now your script is set to run at the specified interval, downloading the data from MachineMetrics and saving it in the location you have specified. Configure your other tools to watch the output file for changes and use MachineMetrics data to drive other business systems!
Comments
0 comments
Please sign in to leave a comment.