Convert type string to Secure String - powershell

I have prepared the PowerShell script for sending emails using SendGrid. But while converting plain text password of SendGrid account into secure string, then I am getting the following error:
“unable to convert type string to secure string”
Sample Script:
$SecurePassword = ConvertTo-SecureString "Demo123" -AsPlainText -Force
So, can anyone suggest me how to resolve this issue.

$Username ="Name#azure.com"
$Password = ConvertTo-SecureString "Demo123" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Username, $Password
$SMTPServer = "smtp.sendgrid.net"
$EmailFrom = "from#mail.com"
$EmailTo = "to#mail.com"
$Subject = "SendGrid test"
$Body = "SendGrid testing successful"
Send-MailMessage -smtpServer $SMTPServer -Credential $credential -Usessl -Port 587 -from $EmailFrom -to $EmailTo -subject $Subject -Body $Body

I am able to run same command in Powershell:
My Powershell Version is 5.7.0.18831.

Related

Azure Devops Server Password variable parameter to powershell

I am passing a password in Azure Devops Server Variable to a powershell method. When the variable is marked as a ** in AD then the code does not work. But if I just pass the variable as plain string it works.
$password = ConvertTo-SecureString -String $passwordParam -AsPlainText -Force
$Credentials= New-Object System.Management.Automation.PSCredential ( $user, $password )
Send-MailMessage -From $emailAddressFrom -To $SendTo -Subject 'Test Message' -Body $body-SmtpServer $server -port $port -UseSsl -Credential $Credentials

Why does attachment do not send with this code?

Why does attachment do not send with this code? how do i remake this code to send email to many recipients?
$EmailTo = "vitaly9oleg#gmail.com"
$EmailFrom = "adm#forceauto.ru"
$Subject = "first letter"
$Body = "Text of the letter"
$SMTPServer = "mail.forceauto.ru"
$filenameAndPath = "C:\1.txt"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("adm#forceauto.ru", "xxxx");
$SMTPClient.Send($EmailFrom,$EmailTo,$Subject,$Body,$attachment)
I'm not sure how to handle your problem, but perhaps I can still help you with this:
In PowerShell starting from 3.0 you have a cmdlet called Send-MailMessage.
Send-MailMessage -From 'jesfer <jesfer#gmail.com>' -To 'tesrere<tesrere#gmail.com>' -Subject "first letter" -Body "Text of the letter" -SmtpServer "smtp.gmail.com" -UseSsl
Regarding the credentials, it's also a parameter. But using it I'd suggest to us pscredential ob.
Edit: with Credentials
$credentials = new-object Management.Automation.PSCredential “jesfer#gmail.com”, (“s57u5t4” | ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -From 'jesfer <jesfer#gmail.com>' -To 'tesrere<tesrere#gmail.com>' -Subject "first letter" -Body "Text of the letter" -SmtpServer "smtp.gmail.com" -UseSsl -Credential $credentials
Edit 2: with working port
$credentials = new-object Management.Automation.PSCredential “adm#forceauto.ru”, (“xxxxx” | ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -From 'adm <adm#forceauto.ru>' -To 'vitaly9oleg<vitaly9oleg#gmail.com>' -Subject "first letter" -Body "Text of the letter" -SmtpServer "mail.forceauto.ru" -Port 465 -UseSsl -Credential $credentials
Edit 3: with attachment
$credentials = new-object Management.Automation.PSCredential “adm#forceauto.ru”, (“xxxxx” | ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -From 'adm <adm#forceauto.ru>' -To 'vitaly9oleg<vitaly9oleg#gmail.com>' -Subject "first letter" -Body "Text of the letter" -Attachments "C:\1.txt" -SmtpServer "mail.forceauto.ru" -Port 465 -UseSsl -Credential $credentials

Send-Mailmessage timing out

I am looking to improve an old batch script and upgrade it to powershell, this is a robocopy batch script and i would like it to send a mail with the logfile attatched when it is finished. i managed to get the drive mappings and robocopy part sorted but im having some issues getting the send-mailmessage part to work
`$SourceFolder = "V:\"
$DestinationFolder = "Y:\"
$Dateandtime = Get-Date -format filedate
$password = XXXXXXXXX
$Subject = "Robocopy Results: Backup USA - NL"
$SMTPServer = "mailserver.domain.com"
$Sender = "backupusr#domain.com"
$Username = "backupusr"
$Recipients = "administrator#domain.com"
$Admin = "administrator#domain.com"
$SendEmail = $True
$IncludeAdmin = $True
$AsAttachment = $False
$Cred = new-object Management.Automation.PSCredential $Username, ($password
| ConvertTo-SecureString -AsPlainText -Force)`
This is the line that causes the script to timeout
Send-MailMessage -SmtpServer $SMTPServer -From $Sender -To $Recipients -Subject $Subject -Body "Robocopy results are attached." -DeliveryNotificationOption onFailure -UseSsl -Credential $Cred
this is the error i recieve
Send-MailMessage : The operation has timed out.
At C:\Scripts\bpcti-robocopy.ps1:113 char:1
+ Send-MailMessage -SmtpServer $SMTPServer -From $Sender -To $Recipient ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Any help would be greatly appreciated.
The -Credential parameter takes a PSCredential object. You can use Get-Credential to interactively create one using a username and password combination. More detail here.
Alternatively, as your password is already in plaintext in your script, you can use the process as described here and construct a new PSCredential.
# Convert the plaintext password into a secure string.
$SecurePassword = ConvertTo-SecureString "XXXXXXXXX" -AsPlainText -Force
# Create a new PSCredential using the username and secure string password.
$Cred = New-Object System.Management.Automation.PSCredential ("backupusr#domain.com", $SecurePassword)
Send-MailMessage -SmtpServer mailserver.domain.com -From backupusr#domain.com -To administrator#domain.com -Subject testing -Body "Robocopy results are attached." -DeliveryNotificationOption onFailure -Credential $Cred
Beware that the username should include the #domain.com.

Powershell to send mail

Error
New-Object : Cannot find an overload for "PSCredential" and the
argument count: "2". At D:\Scripts\gsend.ps1:12 char:15
Code
#Create Password File (Only need once)
#$Credential = Get-Credential
#$Credential.Password | ConvertFrom-SecureString | Set-Content "D:\scripts\gsendcred.txt"
#Send Email
$EncryptedCredential = "D:\scripts\gsendcred.txt"
$EmailUsername = "me#gmail.com"
$EncryptedPW = Get-Content "D:\scripts\gsendcred.txt"
$EncryptedCredential = ConvertTo-SecureString -String $EncryptedCredential -
AsPlainText -Force
$Credential = New-Object Management.Automation.PSCredential ($EmailUsername,
$EncryptedPW)
$EmailFrom = "me#gmail.com"
$EmailTo = "me#gmail.com"
$EmailSubject = "GSEND Test Subject"
$EmailBody = "Test Body"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = 587
$SMTPSsl = $true
I believe your issue is handling credentials. Here is how I handle the smtp credentials:
$smtpPwd = "password"
$smtpCredentialsUsername = "jdonnelly#xxxxx.com"
$smtpCredentialsPassword = ConvertTo-SecureString -String $smtpPwd -AsPlainText -Force
$Credentials = New-Object –TypeName System.Management.Automation.PSCredential
–ArgumentList $smtpCredentialsUsername, $smtpCredentialsPassword
Then:
$smtp.Credentials = $Credentials
Or if you are using Send-MailMessage store your email credentials locally by running this short script by itself beforehand:
Get-Credential | Export-Clixml C:\fso\myemailcreds.xml
It will prompt you to enter your credentials and then store them locally in (in this case) a folder called fso. Then in any Send-MailMessage cmdlet use the locally stored credentials as follows:
Send-MailMessage -To 'Recipient <recipient#yahoo.com>' -from 'John Donnelly <jdonnelly#xxx.com>'
-cc 'whoever <whoever#xxx.com>' -Subject $subject -Body $body
-BodyAsHtml -smtpserver "smtp.office365.com" -usessl
-Credential (Import-Clixml C:\fso\myemailcreds.xml) -Port 587
You Can use below Powershell script for send mail
Send-MailMessage -To "abc#abc.com" -From "def#abc.com" -Cc "gef#abc.com","hij#abc.com" -Subject Test Mail -BodyAsHtml $htmlbody -SmtpServer "mailhost.abc.com"
where $htmlbody is string variable that contain html.

Sending a email with powershell

I want to send a email with powershell. The script works fine if I type my credential in manualy. But I want to give the credential parameters within the script. My script looks like this:
$From = "test#yahoo.de"
$To = "test2#yahoo.de"
$Cc = "test#yahoo.de"
$Attachment = "C:\Users\test\test\test.ini"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.mail.yahoo.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (
$MyClearTextUsername=’test#yahoo.de’
$MyClearTextPassword=’test123’
$SecurePassword=Convertto-SecureString –String $MyClearTextPassword –AsPlainText –force
$MyCredentials=New-object System.Management.Automation.PSCredential $MyClearTextPassword,$SecurePassword) -Attachments $Attachment
Here's how you can create a credentials object:
$cred = ([pscredential]::new('test#yahoo.de',(ConvertTo-SecureString -String 'test123' -AsPlainText -Force)))
so in your case use:
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $cred -Attachments $Attachment
I see no point in trying to fit that into the Send-MailMessage, just create it before and reference it. easier to read.
If you're using Office365 to send email, you might want to try this:
# Sending an email from PowerShell 5.1 script through outlook.office365.com
#
# 1. Create an encrypted password file
# PS > Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File -FilePath <passwordfile>
# This will prompt you for a password, encrypt and save in <passwordfile>
# 2. Obtain Outlook Office365 SMTP server name.
# Go to your ISP and find the value of the MX record. For example <yourdomain>.mail.protection.outlook.com
# 3. If after running the script you get this error:
# Send-MailMessage : Mailbox unavailable. The server response was: 5.7.606 Access denied, banned sending IP [X.X.X.X].
# You will need to delist your IP by going here: https://sender.office.com/
# Note: Removing you IP from the block list could take up to 30 minutes.
#
$User = "<SMPT loging username>"
$PasswordFile = "<passwordfile>"
$SMTPCredentials=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString)
$EmailTo = "<to email address>"
$EmailFrom = "<from email address>"
$Subject = "<email subject>"
$Body = "<email body>"
$SMTPServer = "<Outlook STMP Server from MX record>"
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Port 25 -Credential $SMTPCredentials -UseSsl