Изпращайте имейли с помощта на PowerShell
С Windows Powershell можете да изпращате имейли директно от системата. Това по принцип е сравнително просто, но на практика често се проваля, защото все пак трябва да се удостоверите с доставчика на поща. Ще ви покажем как работи.
Изпращайте писма с PowerShell
От съображения за сигурност PowerShell не позволява паролите за пощенския сървър да са в обикновен текст в скрипта. Следователно, трябва да запишете тези криптирани данни външно и да ги интегрирате с трик.
- За да изпращате поща, използвайте командата „Изпращане на имейл съобщение“, която поддържа всички обичайни параметри. Можете да намерите по-подробна информация за това с „Получаване на помощ за изпращане на пощенски съобщения“.
- Поне имате нужда от изпращач, получател, тема и съдържание, както и сървъра за изходяща поща.
- Ако сървърът не е изрично прехвърлен, PowerShell използва съдържанието на променливата "$ PSEmail Server". Така че можете да запазите този сървър за многократни съобщения предварително в сценария или още по-добре в собствения си PowerShell профил.
- За да направите това, отворете файла на профила, като използвате "note pad.exe $ profile" и добавете например командата "$ PS EmailServer =" smtpmail.t-online.de "". Използвайте сървъра, отговорен за вашия собствен пощенски акаунт като стойност.
- Въпреки това много пощенски сървъри изискват вход с потребителско име и парола. Поне първият може да бъде предаден с параметъра "акредитив", когато се извиква. Когато се изпълни, скриптът показва диалогов прозорец за вход, в който трябва да бъде въведена липсващата парола. Това обаче е непрактично и неподходящо за скрипт задачи на заден план.
- Тъй като паролата не може да бъде предадена директно в скрипта, трябва всеки път да създавате необходимия PSCredential обект, като четете паролата от криптиран файл. За да създадете този файл, използвайте командата »(Get-Credential) .password | ConvertFrom- SecureString> password.txt «.
- В следния типичен диалогов прозорец за влизане въведете »Потребителско име« и »Парола« и потвърдете с »ОК«. Сега PowerShell криптира паролата и я записва във файла »парола, посочена като цел. TXT ".
- По-късно в скрипта можете да извлечете паролата там и да я запишете като променлива с »$ password = Get-Content password.txt | ConvertTo-SecureString ".
- Сега създайте необходимия идентификационен обект с командата »$ poverilne = Нов обект System.Management.Automation.PSCredential„ Потребителско име ”, $ парола«. И накрая, вие прехвърляте този обект чрез „Изпращане на имейл съобщение“ с параметъра »кредитна стойност $ кредитен».