What is the best way to execute PowerShell graph command executed against Azure / Entra ID ?

EnterpriseArchitect 6,041 Reputation points
2024-05-27T14:14:48.94+00:00

What is the best way to execute the PowerShell graph command executed against Azure / Entra ID ?

$date = (Get-Date -Format "yyyy-MM-dd") 2Get-MgRiskDetection -All -Filter "ActivityDateTime ge $date and RiskLevel eq 'high'"

The report will be sent to my email address with the above script when there is a result.

Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Entra Microsoft Entra ID
Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. Givary-MSFT 35,621 Reputation points Microsoft Employee Moderator
    2024-05-31T08:08:46.1833333+00:00

    @EnterpriseArchitect To execute the PowerShell Graph command against Azure/Entra ID and send the report to your email address, you can follow these steps:

    1. Connect-MgGraph cmdlet. You will need to provide your credentials and consent to the necessary permissions.
    2. Run the command to get the risk detections with high risk level for the current date: $date = (Get-Date -Format "yyyy-MM-dd") $riskDetections = Get-MgRiskDetection -All -Filter "ActivityDateTime ge $date and RiskLevel eq 'high'"
    3. Check if the $riskDetections variable contains any results. If there are no results, you can exit the script. If there are results, you can continue with sending the report to your email address.
    4. Use the Send-MailMessage cmdlet to send the report to your email address. Here is an example:
    $emailFrom = "sender@example.com" 
    $emailTo = "recipient@example.com" 
    $emailSubject = "High Risk Detections Report" 
    $emailBody = "Please find attached the report for high risk detections for $date." $attachmentPath = "C:\Reports\HighRiskDetections.csv" 
    $smtpServer = "smtp.example.com" 
    $smtpPort = 587 
    $smtpCredential = Get-Credential
    
    $emailParams = @{ 
    From = $emailFrom 
    To = $emailTo 
    Subject = $emailSubject 
    Body = $emailBody 
    SmtpServer = $smtpServer 
    SmtpPort = $smtpPort 
    Credential = $smtpCredential 
    UseSsl = $true 
    Attachments = $attachmentPath 
    } 
    Send-MailMessage @emailParams
    

    In this example, the report is saved as a CSV file at C:\Reports\HighRiskDetections.csv. You will need to modify the $emailFrom, $emailTo, $smtpServer, $smtpPort, and $smtpCredential variables to match your email settings. You can also modify the email subject and body to your liking.

    Save the script as a .ps1 file and schedule it to run at a desired frequency using Azure Automation.

    Note: The above suggestion is AI-generated and appears to be valid upon review. Please test it in a test environment before deploying it to production.

    Let me know if you have any further questions, feel free to post back.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.