Azure Sentinel MGMT Using PowerShell
Azure Sentinel MGMT Using PowerShell
using PowerShell
Kaido Järvemets
Updated: 21.09.2021
Summary
Most of the code examples include the $AzureSentinelWorkSpaceInfo variable. That's our hash table
where we have stored our resource group name and Log Analytics workspace name. In the below
code example, we are querying only one specific incident. As you see from the code block that we
need to specify the IncidentID parameter. By default, the Azure Sentinel portal doesn't show that
information, and you need to query that from the SecurityIncident table.
SecurityIncident table
Copy the value from the IncidentName column, and you should see the incident details with
PowerShell.
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$IncidentID = "499d8110-790e-43d9-a9d9-a15f0539fcf0"
Get-AzSentinelIncident @AzureSentinelWorkSpaceInfo -IncidentId $IncidentID
Output
Summary
Get-AzSentinelIncident cmdlet allows you to query all the incidents. Just run the cmdlet with your
environment information, and it should list all the incidents. If it is needed, you can do the filtering
based on the CreatedTimeUTC property.
Code example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelIncident @AzureSentinelWorkSpaceInfo
Output
Summary
In this example, we have selected only two different properties using the Select-Object cmdlet – Title
and CreatedTimeUTC and then sorting the results based on the CreatedTimeUTC property.
Code example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelIncident @AzureSentinelWorkSpaceInfo |
Select-Object -Property Title,CreatedTimeUTC |
Sort-Object -Property CreatedTimeUTC -Descending
Output
Summary
As you saw from the previous example, incident creation dates are in the UTC time zone. To convert
the dates into the local time zone, we need to add one additional function. I'm not the author of that
function, and it is taken from the ScriptingGuy blog.
Code example
Function Convert-UTCtoLocal
{
#Source - https://devblogs.microsoft.com/scripting/powertip-convert-from-utc-to-
my-local-time-zone/ PowerTip: Convert from UTC to my local time zone | Scripting
Blog (microsoft.com)
#Author - Thomas Rayner
Param(
[Parameter(Mandatory=$True)]
[String]$UTCTime
)
$LocalTime
}
$ProcessedIncidents = @()
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$IncidentDetails = [ORDERED]@{
IncidentID = $Incident.Name
CreatedTime = Convert-UTCtoLocal -UTCTime $Incident.CreatedTimeUTC
Title = $Incident.Title
Status = $Incident.Status
}
Summary
Changing the incident owner requires us to install the Azure AD PowerShell module. You can take the
incident owner information manually from the Azure AD portal too, but most likely, it would be easier
to use Azure AD PowerShell cmdlets for that. Run the Get-AzureADUser cmdlet and get the user
details. After that, you can use the New-AzSentinelIncidentOwner cmdlet to create the owner object.
Finally, run the Update-AzSentinelIncident command.
Code example
Connect-AzureAD
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$IncidentOwnerDetails = @{
AssignedTo = $AzureADUserDetails.DisplayName
Email = $AzureADUserDetails.Mail
Objectid = $AzureADUserDetails.ObjectId
UserPrincipalName = $AzureADUserDetails.UserPrincipalName
}
Output
Summary
Azure Sentinel allows us to add HTML based comments too. You can add tables or just formatted texts.
The first example uses HTML tags, and the second one is just a regular comment without any
formatting.
Code example 1
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$IncidentID = "499d8110-790e-43d9-a9d9-a15f0539fcf0"
Code example 2
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$IncidentID = "499d8110-790e-43d9-a9d9-a15f0539fcf0"
Summary
Code example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$IncidentID = "499d8110-790e-43d9-a9d9-a15f0539fcf0"
Get-AzSentinelIncidentComment @AzureSentinelWorkSpaceInfo -IncidentId $IncidentID
Output
Summary
New-AzSentinelIncident cmdlet allows you to create new incidents. The strange thing is that the data
source will be empty, and no investigation isn't available.
Code example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
New-AzSentinelIncident @AzureSentinelWorkSpaceInfo -Title "New incident from
PowerShell" -Description "We must investigate this ASAP" -Severity Low -Status
New
Output
Summary
Code example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$IncidentID = "499d8110-790e-43d9-a9d9-a15f0539fcf0"
Remove-AzSentinelIncident @AzureSentinelWorkSpaceInfo -IncidentId $IncidentID
Output
The Remove-AzSentinelIncident cmdlet should return "success" if the removal was successful.
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelAlertRule @AzureSentinelWorkSpaceInfo
Output
Summary
Azure Sentinel allows you to configure automated response actions to your analytics rules. Get-
AzSentinelAlertRuleAction lists the configured playbooks. Use the Get-AzSentinelAlertRule cmdlet to
get the AlertRuleID parameter value. Check the Name property.
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$AlertRuleId = "84d3a26d-1a32-4992-8c35-769cb2a98032"
Get-AzSentinelAlertRuleAction @AzureSentinelWorkSpaceInfo -AlertRuleId
$AlertRuleId
Output
Summary
In the previous example, we queried the configured playbook. Still, if you want more information
about the configured playbook, we need to execute the Get-AzLogicApp cmdlet. In the below code
example, I'm also using the Split-Path cmdlet. That gives me the configured playbook name.
If you have multiple playbooks configured under the Analytics rule, you need to change the code
slightly. Currently, the example assumes that you have only one playbook per the Analytics rule.
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$LogicAppsInfo = @{
ResourceGroupName = "RG-PROD-IT-LOGIC-APPS-WE"
}
$AlertRuleId = "84d3a26d-1a32-4992-8c35-769cb2a98032"
$AlertRuleAction = Get-AzSentinelAlertRuleAction @AzureSentinelWorkSpaceInfo -
AlertRuleId $AlertRuleId
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelAlertRuleTemplate @AzureSentinelWorkSpaceInfo
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Output
Summary
In this example, we have selected out only four properties - DisplayName, Status, CreatedDateUtc,
and Severity. Then we are sorting the results based on the Severity property.
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelAlertRuleTemplate @AzureSentinelWorkSpaceInfo |
Select-Object -Property DisplayName,Status,CreatedDateUtc,Severity |
Sort-Object -Property Severity -Descending
Output
The above code block should give you the following output:
Summary
This code example counts different rule types based on the Severity property. Interestingly, we have
15 rules without any Severity.
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelAlertRuleTemplate @AzureSentinelWorkSpaceInfo |
Group-Object -Property Severity
Output
Summary
The following code example lists all the Analytics rules, where the Data Source contains
"SecurityEvents". This example may be really handy when we are going to combine it with Update-
AzSentinelAlertRule or Update-AzSentinelAlertRuleAction cmdlet. It allows us to filter out specific
Analytics rules, and then we can enable all of them at once.
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelAlertRuleTemplate @AzureSentinelWorkSpaceInfo |
Where-Object {$PSItem.RequiredDataConnectors.ConnectorId -contains
"SecurityEvents"} |
Select-Object -Property DisplayName,Status,CreatedDateUtc,Severity,Name
,RequiredDataConnectors |
Sort-Object -Property Severity
Output
Summary
The good thing about Azure Sentinel is that Microsoft keeps adding new Analytics rules. This query
prints out all the rules that have been added in the last 60 days.
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$TimeRange = (Get-Date).AddDays(-60)
$TimeRange = (Get-Date).AddDays(-60)
Get-AzSentinelAlertRuleTemplate @AzureSentinelWorkSpaceInfo |
Where-Object {$PSItem.CreatedDateUtc -ge $TimeRange} |
Select-Object -Property DisplayName,CreatedDateUtc,Severity |
Sort-Object -Property CreatedDateUtc
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelAlertRuleTemplate @AzureSentinelWorkSpaceInfo |
Where-Object {$PSItem.Severity -eq "Low"} |
Select-Object -Property DisplayName,Severity
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelAlertRuleTemplate @AzureSentinelWorkSpaceInfo |
Group-Object -Property Kind |
Select-Object -Property Count,Name
Output
Summary
The New-AzSentinelAlertRule cmdlet creates a new Analytics rule. This example creates a new
"Scheduled" based Analytics rule. If you have your own custom rules, then it would be much easier
for you to import new rules.
Please remember that this is just a sample Analytics rule, and do not use it in production!
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$NewAnalyticsRuleData = @{
Scheduled = $True
Enabled = $True
Query = "Heartbeat
| summarize LastHeartbeat=max(TimeGenerated) by Computer
| where LastHeartbeat < ago(5m)
| extend HostCustomEntity = Computer"
Output
Summary
The New-AzSentinelAlertRule cmdlet does not allow us to add an automated response immediately,
but we can use the New-AzSentinelAlertRuleAction cmdlet for that activity. Before that, we need to
query our playbook information using the Get-AzLogicApp and Get-AzLogicAppTriggerCallbackUrl
cmdlets. We can then pass that information to the New-AzSentinelAlertRuleAction cmdlet. Then, we
should see the attached playbook under our Analytics rule.
In my case, all my Logic Apps are under one single resource group.
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$LogicAppsInfo = @{
ResourceGroupName = "RG-PROD-IT-LOGIC-APPS-WE"
Name = "Post-Message-Teams"
}
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$BookMarkQuery = @"
let AllWindowsServers =
Heartbeat
| where OSType == 'Windows' and OSType != "Linux"
| summarize arg_max(TimeGenerated, *) by SourceComputerId
| summarize makeset(Computer);
ProtectionStatus
| where Computer in (AllWindowsServers)
| sort by TimeGenerated desc
| summarize arg_max(TimeGenerated, *) by SourceComputerId
| summarize count() by TypeofProtection, AMProductVersion
"@
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelBookmark @AzureSentinelWorkSpaceInfo
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
$Notes = "Check out the Server1. Something seems wrong with that"
Update-AzSentinelBookmark @AzureSentinelWorkSpaceInfo -BookmarkId $BookMark.Name
-Note $Notes
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Output
Summary
Code Example
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
Get-AzSentinelDataConnector @AzureSentinelWorkSpaceInfo |
Select-Object -Property Kind,Name
Output
Summary
$AzureSentinelWorkSpaceInfo = @{
ResourceGroupName = "RG-PROD-IT-AZ-MANAGEMENT-TIER-0-WE"
WorkspaceName = "LF-TIER-0-LOG-ANALYTICS-WE"
}
New-AzSentinelDataConnector @AzureSentinelWorkSpaceInfo -AzureSecurityCenter -
SubscriptionId "%YOURSUBSCRIPTIONID%" -Alerts Enabled
Output