Posielajte e-maily pomocou PowerShell
S Windows Powershell môžete posielať e-maily priamo zo systému. V zásade je to relatívne ľahké, ale v praxi často zlyháva, pretože sa stále musíte overiť u poskytovateľa pošty. Ukážeme vám, ako to funguje.
Posielajte maily pomocou PowerShell
Z bezpečnostných dôvodov nepovoľuje PowerShell heslá pre poštový server v skripte vo formáte obyčajného textu. Preto musíte tieto dáta uložiť externe zašifrované a integrovať ich pomocou triku.
- Na odoslanie pošty použite príkazový riadok „Send-Mailmessage“, ktorý podporuje všetky obvyklé parametre. Podrobnejšie informácie o tejto téme nájdete v časti „Zasielanie správ prostredníctvom e-mailu“.
- Aspoň potrebujete odosielateľa, príjemcu, predmet a obsah, ako aj server odchádzajúcej pošty.
- Ak server nie je explicitne prenesený, PowerShell použije obsah premennej „$ PSEmail Server“. Týmto spôsobom môžete uložiť tento server pre opakované doručenie vopred v skripte alebo ešte lepšie do svojho vlastného profilu PowerShell.
- Na tento účel otvorte súbor profilu pomocou »note pad.exe $ profile« a pridajte napríklad príkaz »$ PS EmailServer =" smtpmail.t-online.de "«. Ako hodnotu použite server zodpovedný za svoj vlastný e-mailový účet.
- Mnoho poštových serverov však vyžaduje prihlásenie s užívateľským menom a heslom. Aspoň prvý môže byť pri volaní odovzdaný s parametrom »poverenie«. Po spustení skript potom zobrazí prihlasovacie dialógové okno, do ktorého sa musí zadať chýbajúce heslo. To je však nepraktické a nevhodné pre úlohy skriptu v pozadí.
- Pretože heslo nemožno odovzdať priamo v skripte, musíte vždy vytvoriť požadovaný objekt PSCredential prečítaním hesla zo šifrovaného súboru. Ak chcete vytvoriť tento súbor, použite príkaz »(Get-Credential) .password | ConvertFrom-SecureString> password.txt «.
- V nasledujúcom dialógovom okne typického prihlásenia zadajte »Meno používateľa« a »Heslo« a potvrďte »OK«. PowerShell teraz šifruje heslo a uloží ho do súboru »heslo zadaného ako cieľ. txt ".
- Neskôr v skripte tam znova vyvolajte heslo a uložte ho ako premennú s »$ password = Get-Content password.txt | ConvertTo-SecureString. "
- Teraz vytvorte potrebný objekt poverenia príkazom »$ credential = New-Object System.Management.Automation.PSCredential" User name ", $ password«. Nakoniec prenesiete tento objekt cez »Send-Mailmessage« s parametrom »poverenie $ poverenie«.