Reputation: 31
The following PowerShell script snippet works fine on Windows 10 (PSv5.1) with latest .NET (4.8).
It fails on Windows 7 (PSV2.0) with a server response: "Authorization Required". However, if I replace $sendercreds.Password with the actual clear-text password, it works!!
$sendercreds = Get-Credential
$smtp = New-Object System.Net.Mail.SmtpClient
$to = New-Object System.Net.Mail.MailAddress("[email protected]")
$from = New-Object System.Net.Mail.MailAddress("[email protected]")
$msg = New-Object System.Net.Mail.MailMessage($from, $to)
$msg.subject = "Something I want to tell them"
$smtp.Host = "smtpserver.senderhost.com"
$smtp.EnableSsl = $True
$smtp.Port = 587
$smtp.Credentials = New-Object System.Net.NetworkCredential($sendercreds.UserName, $sendercreds.Password)
$smtp.Send($msg)
I updated the Win7 machine .NET from 4.6.1 to 4.8: no joy...
It appears to be something related to SecureString support in PS2.0, but I can't find anything on the net about this after HOURS of head-banging!!
Upvotes: 1
Views: 166
Reputation: 31
After a bit more searching, I finally landed on: https://www.pdq.com/blog/powershell-running-net-4-with-powershell-v2/. Turns out PSV2.0 won't use the latest .NET Framework you have installed UNLESS to tell it to with a .config file!!
The config (XML) files (powershell.exe.config and powershell_ise.exe.config) need to contain the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0.30319" />
<supportedRuntime version="v2.0.50727" />
</startup>
</configuration>
Upvotes: 1