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

EnterpriseArchitect 5,136 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.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,419 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,460 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,321 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
20,597 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Givary-MSFT 30,851 Reputation points Microsoft Employee
    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.