PowerShell - Outlook对象不支持读取主机

时间:2015-09-08 20:57:56

标签: powershell outlook

我在使用我认为上周正在运行的PowerShell脚本时遇到了一些麻烦。

用户输入日期并为其设置日历邀请:

[string]$datetime = Read-Host -Prompt "Enter a Date and Time (1/23/45 9:05AM)"    

$olAppointmentItem = 1 

$o = new-object -comobject outlook.application 

#Each new calendar appointment must have the CreateItem and Save lines

$a = $o.CreateItem($olAppointmentItem) 

################ Broken #########################
#$a.Start = $datetime - fails
$a.Start = [string]"9/9/2015 15:00" # - works
####################################
$a.Duration = 30 
$a.Subject = $sql_sub
$a.Body = $sql_desc
$a.Location = $sql_loc
$a.ReminderMinutesBeforeStart = 15 
$a.ReminderSet = $True 

$result = $a.Save() 

基本上通过手动将字符串放在$a.Start对象上它可以正常工作,但是当您使用变量$datetime分配它时,它会失败并显示以下错误消息:

The object does not support this method. At C:\Users\jmasse\Desktop\PowerShell Projects\Ticket to Calendar Invite\Query SW from PS.ps1:76 char:2
+     $a.Start = $datetime
+     ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

我从Read-Host读取正确的日期格式。

0 个答案:

没有答案