Skip to main content

Extracting Data with PowerShell

PowerShell is a versatile scripting language and automation framework that can be a powerful tool for extracting data from MindKey using the Connector API. In this guide, we'll walk you through the process of setting up PowerShell for data extraction and provide sample scripts to get you started.

Prerequisites

Before you begin, make sure you have the following prerequisites in place:

  1. PowerShell Installed: Ensure that you have PowerShell installed on your system. You can download PowerShell from the official Microsoft website.

  2. MindKey Connector API Access: Obtain an API key from MindKey via Administration -> Integration -> Connector Access Control. Store this API key in a secure location as it is necessary for authentication.

Step-by-Step Guide

Follow these steps to extract data from MindKey using PowerShell:

Step 1: Set up Authentication

To access the MindKey Connector API, you'll need to authenticate using your API key. You can add the API key to the HTTP Authorization header with the scheme ApiKey-v2.

Step 2: Construct the API Request

You'll use the Invoke-RestMethod cmdlet in PowerShell to make HTTP requests. Construct your API request URL by specifying the base URL and any query options you need.

For example, to retrieve a list of employees, your URL might look like:

$baseUrl = "https://connector.mindkey.com/odata/v1/Employee"

Step 3: Send the Request

Use the Invoke-RestMethod cmdlet to send your API request. Here's an example:

$response = Invoke-RestMethod -Uri $baseUrl -Headers @{"Authorization" = "ApiKey-v2 YOUR-API-KEY"}

Step 4: Process the Response

The response contains the data you've requested. You can use PowerShell to process and analyze the data as needed.

Sample Scripts

Below are some sample PowerShell scripts for common data extraction tasks. You can adapt these scripts to your specific requirements.

Script 1: Retrieve All Employees

This script retrieves a list of all employees from MindKey.

# Set up your API key and base URL
$apiKey = "YOUR-API-KEY"
$baseUrl = "https://connector.mindkey.com/odata/v1/Employee"

# Send the API request and store the response
$response = Invoke-RestMethod -Uri $baseUrl -Headers @{"Authorization" = "ApiKey-v2 $apiKey"}
# Process $response as needed

Script 2: Export Employees with an Equipment to CSV

This script retrieves a list of employees who have an equipment and exports the data to csv.

# Set up your API key and base URL
$apiKey = "YOUR-API-KEY"
$baseUrl = "https://connector.mindkey.com/odata/v1/Employee"

# Construct the API request with the $filter option (if needed)
$filter = "`$filter=equipments/any(eq: eq ne null)"
$apiUrl = "$baseUrl`?$filter"

# Send the API request and store the response
$response = Invoke-RestMethod -Uri $apiUrl -Headers @{"Authorization" = "ApiKey-v2 $apiKey"}

# Define the file path for the CSV export
$csvFilePath = "ExportedData.csv"

# Export the response data to a CSV file
$response.value | Export-Csv -Path $csvFilePath -NoTypeInformation

# Inform the user about the successful export
Write-Host "Data exported to $csvFilePath"