Send e-mails ved hjælp af PowerShell
Med Windows Powershell kan du sende e-mails direkte fra systemet. Grundlæggende er dette relativt let, men i praksis mislykkes ofte, fordi du stadig skal autentificere dig med mailudbyderen. Vi viser dig, hvordan det fungerer.
Send mails med PowerShell
Af sikkerhedsmæssige årsager tillader PowerShell ikke adgangskoder til mailserveren at være i almindelig tekst i scriptet. Derfor skal du gemme disse data krypteret eksternt og integrere dem med et trick.
- For at sende mail skal du bruge kommandoen "Send-mailmessage", der understøtter alle de sædvanlige parametre. Du kan finde mere detaljerede oplysninger om dette med "Get-Help Send-Mailmessage".
- I det mindste har du brug for afsenderen, modtageren, emnet og indholdet samt den udgående postserver.
- Hvis serveren ikke er eksplicit overført, bruger PowerShell indholdet af variablen "$ PSEmail Server". På denne måde kan du gemme denne server til gentagne udsendelser på forhånd i scriptet eller endnu bedre i din egen PowerShell-profil.
- For at gøre dette skal du åbne profilfilen ved hjælp af »note pad.exe $ profile« og tilføje for eksempel kommandoen »$ PS EmailServer =" smtpmail.t-online.de "«. Brug den server, der er ansvarlig for din egen postkonto, som værdien.
- Imidlertid kræver mange mailserver et login med et brugernavn og en adgangskode. I det mindste kan førstnævnte passeres med parameteren »legitimationsoplysninger«, når det kaldes. Når det udføres, viser scriptet derefter en login-dialog, hvor den manglende adgangskode skal indtastes. Dette er imidlertid upraktisk og uegnet til scriptopgaver i baggrunden.
- Da adgangskoden ikke kan videregives direkte i scriptet, skal du oprette det krævede PSCredential-objekt hver gang ved at læse adgangskoden fra en krypteret fil. For at oprette denne fil skal du bruge kommandoen »(Get-Credential) .password | ConvertFrom- SecureString> password.txt «.
- I den følgende typiske login-dialog skal du indtaste »Brugernavn« og »Adgangskode« og bekræfte med »OK«. PowerShell krypterer nu adgangskoden og gemmer den i filen »adgangskode, der er angivet som målet. txt".
- Senere i scriptet skal du hente adgangskoden der igen og gemme den som en variabel med »$ password = Get-Content password.txt | ConvertTo-SecureString. "
- Opret nu det nødvendige legitimationsobjekt med kommandoen »$ credential = New-Object System.Management.Automation.PSCredential" Brugernavn ", $ password«. Endelig overfører du dette objekt via »Send-mailmessage« med parameteren »legitimation $ legitimationsoplysninger«.