Webtron
Webtron

Reputation: 473

PowerShell - Outlook Object does not support Read-Host

I am having some trouble with a PowerShell script that I made that I thought was working last week.

It takes a date the user types in and sets a calendar invite for it:

[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() 

Basically by manually putting the string on the $a.Start object it works correctly, but when you assign it using the variable $datetime it fail with this error message:

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

I am putting the correct date format in when it gets read from the Read-Host.

Upvotes: 0

Views: 147

Answers (1)

Avshalom
Avshalom

Reputation: 8889

You are trying to add a String object instead of a DateTime Object,

Replace this:

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

With this:

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

Then you can use:

$a.Start = $datetime 

Upvotes: 1

Related Questions