Last active
May 23, 2023 12:25
-
-
Save lawndoc/14c6e720194bf47f32d81ed66701b9e3 to your computer and use it in GitHub Desktop.
Custom tabular function to enrich user info for each device in the results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Advanced Hunting custom function | |
// ------------------------------------ | |
// DeviceUsers() | |
// This function enriches a table with the users who use each device including full name, email, job title, etc. | |
// Example usage: | |
// ... | |
// | invoke DeviceUsers() | |
// ------------------------------------ | |
let DeviceUsers = (T:(DeviceName:string)) { | |
T | |
| join kind=inner (DeviceInfo | |
| distinct DeviceName, LoggedOnUsers | |
| project DeviceName, CurrentUsers = parse_json(LoggedOnUsers) | |
| mv-apply CurrentUsers on ( | |
project DeviceName, User = CurrentUsers.UserName, SID = CurrentUsers.Sid | |
) | |
| project DeviceName, Username = tostring(User), OnPremSid = tostring(SID) | |
| join IdentityInfo on OnPremSid | |
) on DeviceName | |
}; | |
DeviceUsers(Table); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example query that uses our custom DeviceUsers() function | |
// ------------------------------------- | |
DeviceNetworkConnections | |
| where RemoteUrl contains ".ru" | |
| summarize Count = count() by DeviceName | |
| invoke DeviceUsers() // <--- here is our custom tabular function, passing in the current table as a parameter | |
| extend FullName = strcat(GivenName, " ", Surname) | |
| project-reorder DeviceName, FullName, JobTitle, Count | |
| sort by Count desc |
You can still add this directly to your query for now, you just can't save it as a custom function
After doing more investigation and testing, I found that the updated query would work if Microsoft supported passing tables in as an argument to custom functions. Currently there is no option for table in the list of parameter types. If Microsoft would add that parameter type to their drop down menu, you would be able to make custom tabular functions and | invoke
them.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently this is a work in progress, Advanced Hunting doesn't seem to allow you to create functions that are supposed to be used with
| invoke