dackelblick
Goto Top

Attachments.Add Powershell

Hallo Zusammen!

Ich hab da ein Problem...

Folgender Ausschnitt führt zu einem Problem...

$server = "10.10.10.10"
$from = "user@irgendwie.com"
$subject = "TEST"
$template = "C:\Opt\PowerShellCMDs\TEST.html"
$body = get-Content $template

$att_pdf = New-Object System.Net.Mail.Attachment –ArgumentList "C:\Opt\PowerShellCMDs\TEST.pdf", "Application/Pdf"
$message = New-Object System.Net.Mail.MailMessage($from, $email, $subject, $body)

$message.Attachtments.Add($att_pdf)
$message.IsBodyHTML = $true


...das Problem

You cannot call a method on a null-valued expression.
At C:\Opt\PowerShellCMDs\TEST.ps1:44 char:28

back-to-top$message.Attachtments.Add <<<< ($att_pdf)

+ CategoryInfo : InvalidOperation: (Add:String) , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull


Kann ich zur Strings als Attachments mitgeben? Würde an sich keinen Sinn machen. Ich habe übrigens auch die Formen

$att_pdf = New-Object System.Net.Mail.Attachment –ArgumentList "C:\Opt\PowerShellCMDs\TEST.pdf", "Application/Pdf"

oder

$att_pdf = New-Object System.Net.Mail.Attachment –ArgumentList "C:\Opt\PowerShellCMDs\TEST.pdf", "Application/Octet"

oder

$att_pdf = New-Object System.Net.Mail.Attachment("C:\Opt\PowerShellCMDs\TEST.pdf")

oder

$att_file = "C:\Opt\PowerShellCMDs\TEST.pdf"
$att_pdf = New-Object System.Net.Mail.Attachment($att_file)

probiert.

Ergebnis ist immer das Gleiche.... Die Datei gibt es.

Jemand eine gute Idee?

DANKE!!!!!!!!!!!!

Content-Key: 152190

Url: https://administrator.de/contentid/152190

Printed on: April 25, 2024 at 00:04 o'clock

Member: dog
dog Oct 01, 2010 at 22:25:45 (UTC)
Goto Top
Ich hab das grade mal in PS1.0 ausprobiert.
Klappt bei mir problemlos.

PS C:\Dokumente und Einstellungen\Administrator> $mail = New-Object System.Net.Mail.MailMessage("xxx","yyy","Titel","Inhalt")  
PS C:\Dokumente und Einstellungen\Administrator> $mail


From                        : xxx
Sender                      :
ReplyTo                     :
To                          : {}
Bcc                         : {}
CC                          : {}
Priority                    : Normal
DeliveryNotificationOptions : None
Subject                     : Titel
SubjectEncoding             :
Headers                     : {}
Body                        : Inhalt
BodyEncoding                : System.Text.ASCIIEncoding
IsBodyHtml                  : False
Attachments                 : {}
AlternateViews              : {}



PS C:\Dokumente und Einstellungen\Administrator> $att = New-Object System.Net.Mail.Attachment -ArgumentList "C:\setup.log","application/octet-stream"  
PS C:\Dokumente und Einstellungen\Administrator> $att


Name               : setup.log
NameEncoding       :
ContentDisposition : attachment
ContentStream      : System.IO.FileStream
ContentId          : f78e16fd-fd83-4685-b2cd-a34d80bd2114
ContentType        : application/octet-stream; name=setup.log
TransferEncoding   : Base64



PS C:\Dokumente und Einstellungen\Administrator> $mail.Attachments.Add($att)
PS C:\Dokumente und Einstellungen\Administrator>
Member: Dackelblick
Dackelblick Oct 04, 2010 at 08:13:11 (UTC)
Goto Top
Ich teste es mit der Powershell_ISE. Gebe ich es auf der "Konsole" als Befehl ein bekomme ich die Infos wie Du - lasse ich das Script laufen (RUN) bekomme ich den Fehler.????

Korrektur:
$messages.Attachments.Add(...)

schlägt auch damit fehl...


Also in Codeschnipsel...

$body = get-Content "C:\Opt\PowerShellCMDs\TEST.html"
$file = "C:\Opt\PowerShellCMDs\TEST.pdf"
$msg = New-Object System.Net.Mail.MailMessage("vorname.nachname@vonwo.com", "vorname2.nachname2@irgendwo.com", "TEST", $body)
$att = New-Object System.Net.Mail.Attachment($file, "Application/Pdf")

Name : TEST.pdf
NameEncoding :
ContentDisposition : attachment
ContentStream : System.IO.FileStream
ContentId : 256abdcd-428c-416c-919f-88a970e9d856
ContentType : Application/Pdf; name=TEST.pdf
TransferEncoding : Base64

PS C:\WINDOWS\system32\windowspowershell\v1.0> $msg

From : vorname.nachname@vonwo.com
Sender :
ReplyTo :
To : {vorname2.nachname2@irgendwo.com}
Bcc : {}
CC : {}
Priority : Normal
DeliveryNotificationOptions : None
Subject : TEST
SubjectEncoding :
Headers : {}
Body : <!DOCTYPE HTML PUBLIC "-W3CDTD HTML 4.01//EN" >
<html> <head> <title>New Document</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head> <body>
...................
</body> </html>
BodyEncoding : System.Text.UTF8Encoding
IsBodyHtml : False
Attachments : {}
AlternateViews : {}


ABER!!!!!!!!!!!!!!

$msg.Attachtments.Add($att)

PS C:\WINDOWS\system32\windowspowershell\v1.0> $msg.Attachtments.Add($att)
You cannot call a method on a null-valued expression.
At line:1 char:22

back-to-top$msg.Attachtments.Add <<<< ($att)

+ CategoryInfo : InvalidOperation: (Add:String) , RuntimeExcept
ion
+ FullyQualifiedErrorId : InvokeMethodOnNull
Member: Dackelblick
Dackelblick Oct 04, 2010 at 09:03:52 (UTC)
Goto Top
Oje...

ein "t" war es...... *schäm"