I have written a powershell file that when initiated sends input select options based on files in a folder on my PC. Now, I want to just add an action for each option. How do I change this script to do so?
# Import the System.Net.WebClient namespace for making HTTP requests
using namespace System.Net
# Import the System.Web.Script.Serialization namespace for JSON serialization
using namespace System.Web.Script.Serialization
# Import the System.Text namespace for encoding JSON data
using namespace System.Text
# Create an empty array to store the formatted names
$formattedNames = @()
# Get the .bat files from the specified directory
$items = Get-ChildItem -Path "C:\signalRGB_backup\theme bats" | Where-Object { $_.Name -like "*.bat" -and $_.Name -notlike "restart-signal.bat" -and $_.Name -notlike "startup.bat" -and $_.Name -notlike "Previous.bat" }
# Enumerate the .bat files
foreach ($item in $items) {
# Replace hyphens and underscores with spaces, and capitalize the first letter of each word
$formattedName = (Get-Culture).TextInfo.ToTitleCase(($item.BaseName -replace '-', ' '))
$fileName = $item.Name
# Output the formatted filename
Write-Host "$formattedName | ($fileName)"
# Add the formatted name to the array
$formattedNames += $formattedName
}
# Convert the array of formatted names to JSON
$jsonOptions = $formattedNames | ConvertTo-Json -Compress
# Set up the REST API endpoint for Home Assistant
$baseUrl = "https://WEBSITE:PORT/api/services/input_select/set_options"
$AccessKey = "XXXX"
# Define the headers for the HTTP request
$headers = @{
"Authorization" = "Bearer $AccessKey";
"Content-Type" = "application/json";
}
# Define the payload for the HTTP request
$data = @{
"entity_id" = "input_select.signalrgb_themes_selection";
"options" = $jsonOptions | ConvertFrom-Json;
}
# Convert the payload to JSON
$jsonData = $data | ConvertTo-Json -Depth 1
# Make the HTTP request to update the options of the input_select entity
$response = Invoke-WebRequest -Uri $baseUrl -Method POST -Headers $headers -Body $jsonData
# Output the response
Write-Host "Response: $($response.StatusCode) $($response.StatusDescription)"