Helou!
I have created a .PS1 script that removes a workstation from WithSecure.
However, my script is not working. Could someone tell me what is wrong with my code?
I am getting an “Authentication Failed” error. What is the purpose of the Client ID and Secret? Should they be used here as well?
# Vastuuhenkilö Julle
$host.ui.RawUI.WindowTitle = "WithSecure Poisto"
# Define API basics
$apiBaseUrl = "https://eu1.psb.fsapi.com/mp/v1"
$apiKey = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
# Fetch authentication token
$authHeaders = @{
'x-api-key' = $apiKey
'Content-Type' = 'application/x-www-form-urlencoded'
}
try {
# Ask the user for the computer name
$ComputerName = Read-Host 'Enter the computer name'
# Fetch device details
$headers = @{
'x-api-key' = $apiKey
}
$deviceResponse = Invoke-RestMethod -Method Get -Uri "$apiBaseUrl/devices" -Headers $headers
$device = $deviceResponse.items | Where-Object { $_.name -eq $ComputerName }
# Report the result
if ($device) {
$deviceId = $device.id
Write-Host "Computer found WithSecure: $ComputerName"
$deleteResponse = Invoke-RestMethod -Method Delete -Uri "$apiBaseUrl/devices/$deviceId" -Headers $headers
Write-Host "Computer $ComputerName removed from WithSecure"
} else {
Write-Host "Computer not found WithSecure"
}
} catch {
if ($_.Exception.Response.StatusCode -eq 401) {
Write-Host "Authentication failed"
} else {
Write-Host "An error occurred: $_"
}
}