从命令行传递文件位置
Passing the file location from command line
下面是我的 VBScript 代码,用于发送带有附件的电子邮件。该文件位于我发送电子邮件时需要抓取的位置。
Set objMessage = CreateObject("CDO.Message")
Set args = WScript.Arguments
Set arg1 = args.Item(0)
objMessage.Subject = "Sample subject"
objMessage.From = "test@gmail.com"
objMessage.To = "test2@gmail.com"
objMessage.TextBody = "Please see the error logs attached with this email"
objMessage.AddAttachment ""&arg1&""
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "hostname"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
通过命令行运行这个脚本我使用:
>cscript sendemail.vbs D:\users\me\Desktop\readme.txt
当我 运行 时,我收到一条错误消息:
D:\Users\me\Desktop\sendemail.vbs(3, 1) Microsoft VBScript runtime error: Object required: '[string: "D:\Users\me\Desk"]'
有什么建议吗?
错误消息实际上说明了一切。虽然 Arguments
集合是一个对象,但它的第一个元素不是。它是一个字符串,在 VBScript 中是一种原始数据类型。 Set
关键字专用于将对象分配给变量。原始数据类型仅使用赋值运算符进行赋值。
改变这个:
Set arg1 = args.Item(0)
进入这个:
arg1 = args.Item(0)
错误将消失。
下面是我的 VBScript 代码,用于发送带有附件的电子邮件。该文件位于我发送电子邮件时需要抓取的位置。
Set objMessage = CreateObject("CDO.Message")
Set args = WScript.Arguments
Set arg1 = args.Item(0)
objMessage.Subject = "Sample subject"
objMessage.From = "test@gmail.com"
objMessage.To = "test2@gmail.com"
objMessage.TextBody = "Please see the error logs attached with this email"
objMessage.AddAttachment ""&arg1&""
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "hostname"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
通过命令行运行这个脚本我使用:
>cscript sendemail.vbs D:\users\me\Desktop\readme.txt
当我 运行 时,我收到一条错误消息:
D:\Users\me\Desktop\sendemail.vbs(3, 1) Microsoft VBScript runtime error: Object required: '[string: "D:\Users\me\Desk"]'
有什么建议吗?
错误消息实际上说明了一切。虽然 Arguments
集合是一个对象,但它的第一个元素不是。它是一个字符串,在 VBScript 中是一种原始数据类型。 Set
关键字专用于将对象分配给变量。原始数据类型仅使用赋值运算符进行赋值。
改变这个:
Set arg1 = args.Item(0)
进入这个:
arg1 = args.Item(0)
错误将消失。