Powershell API Calls to Create Tags Failing with no Error Message

%3CLINGO-SUB%20id%3D%22lingo-sub-2062050%22%20slang%3D%22en-US%22%3EPowershell%20API%20Calls%20to%20Create%20Tags%20Failing%20with%20no%20Error%20Message%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2062050%22%20slang%3D%22en-US%22%3E%3CP%3EI%20created%20a%20PowerShell%20script%20that%20calls%20the%20Security%20Center%20Defender%20for%20Endpoint%20api%20to%20create%20a%20machine%20tag.%26nbsp%3B%20I%20am%20experiencing%20some%20irregular%20behavior.%26nbsp%3B%20I%20will%20make%20the%20call%2C%20and%20it%20will%20return%20a%20success%20code%20of%20200%20and%20my%20expectation%20is%20the%20tag%20has%20been%20created.%26nbsp%3B%20When%20I%20go%20into%20Security%20Center%20for%20Defender%20for%20Endpoint%2C%20I%20may%20not%20see%20the%20tag.%26nbsp%3B%20I%20could%20run%20the%20same%20script%20again%2C%20and%20the%20tag%20will%20be%20there.%26nbsp%3B%20I%20am%20making%20sure%20to%20stay%20within%20the%20API%20limitations%20of%20100%20calls%20per%20minute%2C%201500%20calls%20per%20hour.%26nbsp%3B%20I%20don't%20know%20what%20is%20wrong.%26nbsp%3B%20Any%20ideas%3F%26nbsp%3B%20Here%20is%20a%20snippet%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24myurl%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fapi-us.securitycenter.windows.com%2Fapi%2Fmachines%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fapi-us.securitycenter.windows.com%2Fapi%2Fmachines%2F%3C%2FA%3E%22%20%2B%20%24machine.id%20%2B%20%22%2Ftags%22%3C%2FP%3E%3CP%3E%24body%20%3D%3CBR%20%2F%3E%40%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%22Value%22%20%3D%20%24tag%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%E2%80%9CAction%E2%80%9D%20%3D%20%22Add%22%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%23%20Send%20the%20webrequest%20and%20get%20the%20results.%3CBR%20%2F%3E%24response%20%3D%20Invoke-WebRequest%20-Method%20Post%20-Uri%20%24myurl%20-Body%20(%24body%20%7C%20ConvertTo-Json)%20-Headers%20%24headers%20-ErrorAction%20Stop%3CBR%20%2F%3Eif%20(%24response.StatusCode%20-eq%20200)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3BWrite-Host%20%22Added%20tag%20%24tag%20to%20%22%20%24machine.computerDnsName%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2063634%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20API%20Calls%20to%20Create%20Tags%20Failing%20with%20no%20Error%20Message%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2063634%22%20slang%3D%22en-US%22%3EI%20tag%20a%20lot%20of%20machines%20with%20the%20API%20and%20haven't%20had%20any%20real%20issues.%3CBR%20%2F%3EIt%20might%20be%20possible%20that%20there%20is%20some%20delay%20between%20when%20you%20add%20it%20through%20the%20API%20and%20when%20it's%20visible%3F%3CBR%20%2F%3EHave%20you%20tried%20to%20check%20through%20the%20API%20is%20you%20see%20the%20tag%20there%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2068757%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20API%20Calls%20to%20Create%20Tags%20Failing%20with%20no%20Error%20Message%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2068757%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F186539%22%20target%3D%22_blank%22%3E%40Thijs%20Lecomte%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20do%20not%20see%20the%20tags%20even%20after%20waiting%20a%20day.%26nbsp%3B%20They%20are%20really%20not%20there.%26nbsp%3B%20The%20problem%20occurs%20both%20when%20adding%20and%20removing%20tags.%26nbsp%3B%20I%20get%20a%20200%20response%2C%20indicating%20everything%20was%20OK%2C%20but%20then%20the%20tag%20was%20not%20added%20or%20removed%20as%20expected.%26nbsp%3B%20I%20guess%20I%20could%20do%20another%20call%20to%20see%20if%20the%20tag%20is%20there%2C%20but%20it%20wastes%20a%20lot%20of%20API%20calls.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2081910%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20API%20Calls%20to%20Create%20Tags%20Failing%20with%20no%20Error%20Message%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2081910%22%20slang%3D%22en-US%22%3EIt%20should%20work.%20I%20would%20advise%20you%20to%20open%20a%20support%20case%20to%20check%20what's%20going%20on%3C%2FLINGO-BODY%3E
Occasional Contributor

I created a PowerShell script that calls the Security Center Defender for Endpoint api to create a machine tag.  I am experiencing some irregular behavior.  I will make the call, and it will return a success code of 200 and my expectation is the tag has been created.  When I go into Security Center for Defender for Endpoint, I may not see the tag.  I could run the same script again, and the tag will be there.  I am making sure to stay within the API limitations of 100 calls per minute, 1500 calls per hour.  I don't know what is wrong.  Any ideas?  Here is a snippet:

 

$myurl = "https://api-us.securitycenter.windows.com/api/machines/" + $machine.id + "/tags"

$body =
@{
   "Value" = $tag
    “Action” = "Add"
}

# Send the webrequest and get the results.
$response = Invoke-WebRequest -Method Post -Uri $myurl -Body ($body | ConvertTo-Json) -Headers $headers -ErrorAction Stop
if ($response.StatusCode -eq 200)
{
   Write-Host "Added tag $tag to " $machine.computerDnsName

}

7 Replies
I tag a lot of machines with the API and haven't had any real issues.
It might be possible that there is some delay between when you add it through the API and when it's visible?
Have you tried to check through the API is you see the tag there?

@Thijs Lecomte 

I do not see the tags even after waiting a day.  They are really not there.  The problem occurs both when adding and removing tags.  I get a 200 response, indicating everything was OK, but then the tag was not added or removed as expected.  I guess I could do another call to see if the tag is there, but it wastes a lot of API calls.

It should work. I would advise you to open a support case to check what's going on

@Thijs Lecomte I will be opening a support case, thank you.

Hi,
@bellinghamlady 

 

Thanks for sharing your question with us!

 

In general, you’ll need to take the following steps to use the APIs:

  • Create an AAD application
  • Get an access token using this application
  • Use the token to access Defender for Endpoint API

I believe the API endpoint may be expecting a token to be sent with each request.

 

You must also add an app registration on the Azure AD tenant and give it permissions to authenticate with the API endpoint.

 

Can you post a screenshot of the output in your IDE? If there is a 200 code coming back from the endpoint then it doesn't necessarily indicate an authentication issue of course. 

 

I'm currently writing an application which is doing a similar thing, here is how I'm doing it - you could try it this way and see if the behaviour changes:

 

$tenantId = '' ### Paste your tenant ID (Azure AD Tenant ID) here
$appId = '' ### Paste your Application ID here
$appSecret = '' ### Paste your Application key here

$authBody = [Ordered] @{
    resource = "$resourceAppIdUri"
    client_id = "$appId"
    client_secret = "$appSecret"
    grant_type = 'client_credentials'
}

Write-Host "Authenticating..."
$authResponse = Invoke-RestMethod -Method Post -Uri $oAuthUri -Body $authBody -ErrorAction Stop

#Setting up a new variable which is referencing the access token attribute inside the authResponse.
Write-Host "Generating Token..."
$token = $authResponse.access_token

#This is our token object which we include in the web request as a JSON construct.
$HeaderToken = @{ "Authorization" = "Bearer $token" }

#Now we have our token generated, we can create our request to change the tags and send it to the API endpoint

$url = 'https://api.securitycenter.windows.com/api/machines'
$JSONTAG = [Ordered] @{ Action = "Add" ; Value = "#######"} | ConvertTo-Json -Compress
$request = Invoke-WebRequest -Method POST -Uri $url -Header $HeaderToken -body $JSONTAG

 

You can also use the Microsoft authentication library to generate credentials for the user who is running the script, that requires an additional PowerShell module to be imported and a different authentication function in the script.

Within Azure AD, you also have various options to authenticate - I personally find the above method works well especially if I want to abstract the permissions in the token I'm generating from the user or service who is running the script, which can then be controlled in Azure AD.

Further documentation:
https://docs.microsoft.com/en-us/windows/security/threat-protection/microsoft-defender-atp/apis-intr...

Let us know if this helps at all and if there are any further questions,

Best regards,

@Emc  Thank you for your reply.  To be clear, I am actually adding and removing tags to a single machine one by one.  So, my URL looks more like:

 

$url = "https://api-us.securitycenter.windows.com/api/machines/" + $machine.id + "/tags"

 

For some reason, I have to use api-us in place of api.

 

I am getting a good token and most of the api calls return 200, successful.  Occasionally, I get a timeout error.  I am refreshing the token every 30 minutes, making sure I only do 100 calls per minute and 1500 per hour.  A significant number of calls, however, return a code of 200, successful, but then I do not see the tag added or removed.  It is totally random.  I can run the same script twice, and one time it will say the tag has been created, but it is not there, and the second time I run it and it says the tag is there, and it really is there.  Again, my problem is intermittent.  I tried adding a 2 second delay between each api call, still no luck.  I did try your slightly different syntax for the header and body, but it did not seem to make a difference.  It is very frustrating, because the failure to create and remove tags is totally random.

@bellinghamlady 

Ok interesting - thank you for the clarification.

 

The geo of the endpoint should be fine either way.

 

Interestingly, I also noticed some similar anomalies in my own requests., but I think my problem was slightly different from yours.

For example, I would have a web request inside a conditional statement to filter for a specific set of machines in a domain, and add a tag to them. However, seemingly randomly, machines would be left out of the expected dataset when compared to the manual export, even although all requests were within the same loop function  - that being said, all machines in my dataset always had their tag added successfully by the script, despite outliers being left out which could indicate some trouble with the API itself. I'm not sure, I need to debug this particular issue more.

Upon running the script multiple times, the outliers would eventually be picked up which further may exclude possibility the script itself is buggy.

 

For clarification, I was working with small sets of requests of less than 50 machines each time, so no possibility of reaching the request limit.