Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in RPA by (12.7k points)

I am trying to grab emails from Exchange using Powershell in UI Path. When trying to return the items, I get the following error:

Throw: Unable to cast object of type 'Microsoft.Exchange.WebServices.Data.GetItemResponse' to type 'System.String'.

Even when I change the TypeArgument in UI Path. Currently, I am using the Invoke power shell activity but I get the same issues using the RunPowershellScript activity. Below is a screenshot of my sequence in UI Path, my parameters, and my PowerShell script, Thank you!

Ui path Screenshot

UI Path Parameters

Param(

[parameter()]

[string]$mailbox,

[parameter()]

[string]$password

)

try{

#https://forum.uipath.com/t/get-argument-from-an-process-with-exception-in-reframework/22537/4

function test-password(){

                $Creds = new-object System.Net.networkCredential -ArgumentList $mailbox, $password

                $mailFolder = "Inbox"

                $itemView = 3

                #[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll")

                $ExSer = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)

                $ExSer.Credentials = ($Creds)

                $ExSer.Url = new-object Uri("https://outlook.office365.com/EWS/Exchange.asmx")

                $ExSer.AutodiscoverUrl($mailbox, {$true})

                $setMailFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($ExSer,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)

                $logs2 += "Successfully Connected to mailbox $mailbox"

                $iv = new-object Microsoft.Exchange.WebServices.Data.ItemView($itemView)

                $CustomFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And)

                $ifIsRead = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::IsRead,$false)

                $ifFrom = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::From,"[email protected]")

                $CustomFilter.add($ifIsRead)

                $CustomFilter.add($ifFrom)

                $filteredEmails = $ExSer.FindItems($setMailFolder.Id,$CustomFilter, $iv)

                $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)

                $psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;

                #add-type "Microsoft.Exchange.WebServices.Data.GetItemResponse"

                $ExSer.LoadPropertiesForItems($filteredEmails,$psPropertySet)

                $item = $filteredEmails.Items[0]

                return $mailbox, $item

    }               

   test-password

}

catch{

        Throw

}

I commented out the load dll as it seemed to work without it and was throwing a similar error when it hit it. The code seems to throw an error when it hits $Exser.LoadPropertyItems. I have also tried switching to Exchange 2007 etc. To clarify, when running purely PowerShell outside of UI Path, this code works just fine.

1 Answer

0 votes
by (29.5k points)

I think the issue is that its trying to load an object which is breaking it, Try saving it as a variable and then covert it within your script and then return whatever you need as the string then it should start working you can refer the code below.

Param(
[parameter()]
[string]$mailbox,

[parameter()]
[string]$password
)
try{
#https://forum.uipath.com/t/get-argument-from-an-process-with-exception-in-reframework/22537/4
function test-password(){

                $Creds = new-object System.Net.networkCredential -ArgumentList $mailbox, $password

                $mailFolder = "Inbox"
                $itemView = 50

               # Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
                #[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll")
                $ExSer = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
                $ExSer.Credentials = ($Creds)
                $ExSer.Url = new-object Uri("https://outlook.office365.com/EWS/Exchange.asmx")
                $ExSer.AutodiscoverUrl($mailbox, {$true})


                $setMailFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($ExSer,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
                $logs2 += "Successfully Connected to mailbox $mailbox"

                $iv = new-object Microsoft.Exchange.WebServices.Data.ItemView($itemView)

                $CustomFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And)

                $ifIsRead = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::IsRead,$true)
                $ifFrom = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::From,"[email protected]")

                $CustomFilter.add($ifIsRead)
                $CustomFilter.add($ifFrom)

                $filteredEmails = $ExSer.FindItems($setMailFolder.Id,$CustomFilter, $iv)

                $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
                $psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;
                #add-type "Microsoft.Exchange.WebServices.Data.GetItemResponse"
                $FilteredEmailitems = $ExSer.LoadPropertiesForItems($filteredEmails,$psPropertySet)


                $EmailBody = $FilteredEmailitems.Item(0) | Select-Object -ExpandProperty Item | Select-Object -ExpandProperty Body | Select-Object -ExpandProperty Text

                return $EmailBody
    }             


   test-password
}
catch{
        Throw
}

Related questions

0 votes
1 answer
asked Jul 12, 2019 in RPA by Abhishek_31 (12.7k points)
0 votes
1 answer

Browse Categories

...