Grmmpf, es lag an der Datei, in der der secure string für das Passwort liegt. Powershell hat nicht gemeckert, ich habe es nicht bemerkt. Anbei der jetzt funktionierende Code:
Code
param([string]$To, [string]$Subject, [string]$Body)
Function Send {
Param (
[Parameter(Mandatory=$true)]
[String]$to,
[Parameter(Mandatory=$true)]
[String]$subject,
[Parameter(Mandatory=$true)]
[String]$body
)
$PSEmailServer = "mxf993.netcup.net"
$from="sendmail@my-lovely-domain.com"
# create credentials obj
$cred_str_path = $ENV:UserProfile+"\sendmail.txt"
if (Test-Path $cred_str_path -PathType leaf)
{
Write-Host "Credentials found in file path: "$cred_str_path
} else {
# if not done before, write the credentials to a secure string:
Write-Host "No credentials found in file path: "$cred_str_path", creating one ..."
# requires a manual entry of username and password:
(Get-Credential).password | ConvertFrom-SecureString > $cred_str_path
}
# Retrieve your pwd from credentials file and create credentials object.
$pw = Get-Content $cred_str_path | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential $from, $pw
$body += " `r`n `r`nSend from " + (Get-Content env:computername) + " at " + ([datetime]::now).tostring("dd.MM.yyyy HH:mm:ss.fff")
Send-MailMessage -To $to -Subject $subject -Body $body -From $from -Credential $cred -UseSsl
} #End Function Send-Mail
Alles anzeigen
Vielen Dank für Eure Unterstützung, das hat mir auf die richtige Spur geholfen!!
Viele Grüße,
Michel