0

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.

Pradeep
  • 5,101
  • 14
  • 68
  • 140
  • Where did you run the command? – Joy Wang Dec 21 '20 at 07:20
  • 1
    `$SecurePassword = ConvertTo-SecureString "Demo123" -AsPlainText -Force` is probably not the line where the error occurred. Please show what you are actually doing and include the line in the question where the “*unable to convert type string to secure string*” does occur. – iRon Dec 21 '20 at 07:37

2 Answers2

1
$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
iRon
  • 20,463
  • 10
  • 53
  • 79
  • Thanks @Iron, Now I'm getting this error `System.Net.Mail.SmtpException: Unable to read data from the transport connection: net_io_connectionclosed.` – Pradeep Dec 21 '20 at 15:25
  • Did you check this [SmtpException: Unable to read data from the transport connection: net_io_connectionclosed](https://stackoverflow.com/a/20252948/1701026) answer? If that does help, I recommend you to open a new question. – iRon Dec 21 '20 at 15:46
  • I have checked the above link already but that is not useful. – Pradeep Dec 21 '20 at 15:57
0

I am able to run same command in Powershell:

enter image description here

My Powershell Version is 5.7.0.18831.

Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
  • I run the same command in local PowerShell, then it is working. But whenever I'm executing it from Azure DevOps release pipeline, then I'm getting the error. – Pradeep Dec 21 '20 at 14:55
  • 1
    Specify the powershell version in azure devops and try once. – Harshita Singh Dec 21 '20 at 16:13
  • Did you try it? – Harshita Singh Dec 22 '20 at 07:13
  • No @singhh-msft, The issue is, I have used `securestring` type instead of `string' type for the password parameter. In the release task, I'm trying to override the parameter values by reading it from variables section. There I'm getting the above error, but now it resolved. – Pradeep Dec 22 '20 at 09:01
  • Yes, that is wrong. But, you mentioned something else in your question. You should use correct dataType for any variable. – Harshita Singh Dec 22 '20 at 09:41