Forum Discussion
Need help with powershell scripting
I am trying to run a powershell script from inside a .cmd file. I want the script to send an email with an attachment out to one or two users using my gmail account.
1. I have a problem in that I have to run the Install command line 1 and then can run the rest of the lines of code. Powershell has to be in admin mode. Have tries this command
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "D:\Monday.ps1"' -Verb RunAs}
2. I keep getting a user id/password problem even when I know the ID and password are correct
Here is the code I am using
--------------------------
Install-Module -Name Send-MailKitMessage
using module Send-MailKitMessage;
$UseSecureConnectionIfAvailable = $true
$Credential = [System.Management.Automation.PSCredential]::new("email address removed for privacy reasons", (ConvertTo-SecureString -String "xxxxxx" -AsPlainText -Force));
$SMTPServer = "smtp.gmail.com";
$Port = 465
$From = [MimeKit.MailboxAddress]"email address removed for privacy reasons";
$RecipientList = [MimeKit.InternetAddressList]::new();
$RecipientList.Add([MimeKit.InternetAddress]"email address removed for privacy reasons");
$CCList = [MimeKit.InternetAddressList]::new();
$CCList.Add([MimeKit.InternetAddress]"email address removed for privacy reasons");
$Subject = [string]"Check Log File";
$TextBody = [string]"Hey, Please verify this log file from today's backup";
$AttachmentList = [System.Collections.Generic.List[string]]::new();
$AttachmentList.Add("D:MyBackup.log");
$Parameters = @{
"UseSecureConnectionIfAvailable" = $UseSecureConnectionIfAvailable
"Credential" = $Credential
"SMTPServer" = $SMTPServer
"Port" = $Port
"From" = $From
"RecipientList" = $RecipientList
"CCList" = $CCList
"BCCList" = $BCCList
"Subject" = $Subject
"TextBody" = $TextBody
"HTMLBody" = $HTMLBody
"AttachmentList" = $AttachmentList
};
Send-MailKitMessage @Parameters;
--------------------------------
- LeonPavesicSilver Contributor
Hi SLoventhal,
you can try the following options:
Run PowerShell as Administrator:
Ensure that the path to your PowerShell script (e.g., D:\Monday.ps1) is accurate. Use a command like the one below, adjusting the script path:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File \"<Your-Script-Path>\"' -Verb RunAs}"
Sending Email via Gmail:
For sending emails via Gmail, secure connections and proper authentication are necessary. Here's a changed script:
$EmailFrom = "email address removed for privacy reasons" $EmailTo = "email address removed for privacy reasons" $Subject = "Email subject" $Body = "Email body" $SMTPServer = "smtp.gmail.com" $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password"); $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Replace "username" and "password" with your Gmail credentials.
Note: If you have 2-step verification enabled on your Gmail account, you’ll need to generate and use an app password (https://support.google.com/accounts/answer/185833?hl=en). Also, you might need to allow less secure apps to access your Gmail account (https://support.google.com/accounts/answer/6010255?hl=en).
smtp - sending email with gmail using powershell - Stack Overflow
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn) - SLoventhalCopper ContributorBest response...