Entra / Microsoft 365 · Compliance & audit
Send message about deleted stream videos
A script to show how to report videos waiting in the Stream recycle bin and send the report to an administrator account.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-ExchangeOnline
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
param([int] $LookbackDays = 30,[string] $StartDate = (Get-Date).AddDays(-$LookbackDays); $EndDate = (Get-Date),[string] $EndDate = (Get-Date))If (!$O365Cred) {$O365Cred = Get-Credential}# And that we're connected to Exchange OnlineTry { $OrgName = (Get-OrganizationConfig).Name }Catch {Write-Host "Your PowerShell session is not connected to Exchange Online."Write-Host "Please connect to Exchange Online using an administrative account and retry."Break }$HTMLReportFile = "c:\temp\StreamDeletedVideos.html"$Records = (Search-UnifiedAuditLog -Operations StreamDeleteVideo -StartDate $StartDate -EndDate $EndDate -ResultSize 2000)If ($Records.Count -eq 0) {Write-Host "No audit records for Stream video uploads found." }Else {Write-Host "Processing" $Records.Count "audit records..."$Report = [System.Collections.Generic.List[Object]]::new() # Create output file for report# Scan each audit record to extract informationForEach ($Rec in $Records) {$AuditData = ConvertFrom-Json $Rec.Auditdata$DaysElapsed = ($EndDate - (Get-Date($AuditData.CreationTime)))$ReportLine = [PSCustomObject] @{User = $AuditData.UserId"Video Name" = ($AuditData.OperationDetails | ConvertFrom-Json).NameAction = "Deleted Video""Deleted on" = Get-Date($AuditData.CreationTime) -format g"Days Since Deletion" = $DaysElapsed.Days"Days Remaining" = (30 - $DaysElapsed.Days)"Video Identifier" = $AuditData.EntityPath }$Report.Add($ReportLine) } }CLSWrite-Host "Report produced - emailing it to the Administrator account"# Create HTML Report$HtmlHeader ="<html><style>BODY{font-family: Arial; font-size: 8pt;}H1{font-size: 28px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}H2{font-size: 20px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}H3{font-size: 16px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}TABLE{border: 1px solid black; border-collapse: collapse; font-size: 8pt;}TH{border: 1px solid #969595; background: #dddddd; padding: 5px; color: #000000;}TD{border: 1px solid #969595; padding: 5px; }td.pass{background: #B7EB83;}td.warn{background: #FFF275;}td.fail{background: #FF2626; color: #ffffff;}td.info{background: #85D4FF;}</style><body><div align=center><p><h1><u>Stream Videos in Recycle Bin</h1></u></p><p><h2>Report Generated: " + $EndDate + "</h2></p><p><h3>The following videos are in the Stream recycle bin and will be deleted after 30 days.</h3></p></div>"# Add information about deleted videos$HtmlBody = $Report | Sort "Days Remaining" -Descending | ConvertTo-Html -Fragment$HtmlTail = "<p><b>Please review and make sure that it is OK to allow these videos to be deleted. After Stream removes videos from its Recycle Bin, the videos are no longer recoverable.</b></p>"$HtmlReport = $HtmlHeader + $HtmlBody + $HtmlTail$HtmlReport | Out-File $HtmlReportFile -Encoding UTF8# Set TLS[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12# Import into email body$HtmlMsg = Get-Content $HtmlReportFile$MsgFrom = $O365Cred.UserName ; $SmtpServer = "smtp.office365.com" ; $SmtpPort = '587'$MsgTo = ("Administrator@domain.com", "Tony.Redmond@domain.com") Change this to add your own set of addressees# Construct the message parameters and send it off...$MsgParam = @{To = $MsgToFrom = $MsgFromSubject = "Deleted Stream Videos in Recycle Bin"Body = $HtmlReportSmtpServer = $SmtpServerPort = $SmtpPortCredential = $O365Cred }Send-MailMessage @msgParam -UseSSL -BodyAsHTMLWrite-Host "All done. The output file is also available in" $HtmlReportFile
Parameters
ParameterDefaultNotes
-LookbackDays30How many days back to search Stream delete-video audit events.-StartDate(Get-Date).AddDays(-30)Start of the audit search window.-EndDate(Get-Date)End of the audit search window.Attribution
Author
Office365itpros