r/PowerShell • u/LordLoss01 • Mar 13 '26
Run Live Response in PowerShell?
Is there any way to run Live Response using PowerShell? I tried following the below guide but it returns with a 401 error.
Running Microsoft Defender Live Response with PowerShell | by Grzegorz Berdzik | Medium
This is what I put for my query:
Connect-AzAccount$accessToken = Get-AzAccessToken -ResourceUrl "https://api.securitycenter.microsoft.com" -AsSecureString$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($accessToken.Token)$token = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
$body = @{
Commands = @(
@{
type = "RunScript"
params = @(
@{
key = "Thisismyscript.ps1"
value = "Thisismyscript.ps1"
})
})
Comment = "$LiveResponseReason"
}
$jsonBody = $body | ConvertTo-Json -Depth 50
$apiUrl = "https://api.securitycenter.microsoft.com/api/machines/833hdgd673hcbdj7dbb3dcbh7hfbfb38hdd/runLiveResponse"
Invoke-RestMethod -Uri $apiUrl -Method POST -Headers @{Authorization = "Bearer $token"; "Content-Type" = "application/json"} -Body $jsonBody
View all comments
Show parent comment
2
u/AdeelAutomates Mar 13 '26 edited Mar 13 '26
Bad Request what? it will tell you. if not... use catch and see under errorDetails.message
400 means something you sent is wrong. Could be your body is wrong, your uri, etc.