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
3
u/Modify- Mar 13 '26
HTTP 401 means Unauthorized. So the AccessToken you are providing does not have the right permission(s). Make sure your account has the right ones.
Pro tip, you can go to https://jwt.ms and decode the token to see which permissions you have.
1
u/Modify- Mar 13 '26 edited Mar 13 '26
Im on my phone so I can't test myself. The only thing that jumps out to me is the key, value.
In the example only the value is has the fullname?
Key: myscript Value: Myscript.ps1
Edit: Also the machineId has the right format, like a guid? When i'm stuck I open the networktools in the browser and perform the action there. That way you can see how the request is structured with actual values.
1
u/PinchesTheCrab Mar 14 '26
That blog says the key needs to be
ScriptNameand the value is the name of the script. Now that you've resolved the 401 issue I think it may work with the right key name.1
u/BlackV Mar 15 '26
p.s. formatting
it'll format it properly OR
Inline code block using backticks
`Single code line`inside normal textSee here for more detail
Thanks