# send_daily_report.ps1 # Sendet täglich um 0 Uhr eine HTML-Zusammenfassung aller Trades des Vortages. $python = "C:\Users\ah\AppData\Local\Programs\Python\Python312\python.exe" $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $pyScript = Join-Path $scriptDir "daily_summary.py" # ── Graph-Credentials aus oil_widget_config.ini ([graph]) ───────────────── # Secrets gehören nicht ins Skript — zentrale Config wie bei den API-Keys. $iniPath = Join-Path (Split-Path -Parent $scriptDir) "oil_widget_config.ini" $graph = @{} $inGraph = $false foreach ($line in Get-Content $iniPath -Encoding UTF8) { if ($line -match '^\s*\[(.+)\]') { $inGraph = ($Matches[1] -eq 'graph'); continue } if ($inGraph -and $line -match '^\s*(\w+)\s*=\s*(.+?)\s*$') { $graph[$Matches[1]] = $Matches[2] } } $tenantId = $graph['tenant_id'] $clientId = $graph['client_id'] $clientSecret = $graph['client_secret'] if (-not $tenantId -or -not $clientId -or -not $clientSecret) { Write-Error "Abschnitt [graph] in $iniPath fehlt oder ist unvollständig." exit 1 } # ── Zusammenfassung aus Python holen ──────────────────────────────────────── $yesterday = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd") # -join: Python gibt mehrere Zeilen aus → PowerShell macht ein String-Array daraus. # Ohne -join würde nur die erste Zeile () als Content ankommen → leere Mail. $htmlBody = (& $python $pyScript $yesterday 2>&1) -join "`n" if ([string]::IsNullOrWhiteSpace($htmlBody) -or $htmlBody -match "Traceback|Error:") { $htmlBody = "
Fehler beim Generieren des Reports für $yesterday.
$htmlBody" } $subject = "Oil Trading $((Get-Date).AddDays(-1).ToString('dd.MM.yyyy'))" # ── Microsoft Graph verbinden ──────────────────────────────────────────────── $secureSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential($clientId, $secureSecret) Connect-MgGraph -TenantId $tenantId -ClientSecretCredential $cred -NoWelcome # ── E-Mail senden ──────────────────────────────────────────────────────────── $params = @{ Message = @{ Subject = $subject Body = @{ ContentType = "HTML" Content = $htmlBody } ToRecipients = @( @{ EmailAddress = @{ Address = "axel@hocks.eu" } } ) From = @{ EmailAddress = @{ Address = "mailagent@hocks.eu" } } } } Send-MgUserMail -UserId "mailagent@hocks.eu" -BodyParameter $params Write-Output "Report gesendet: $subject"