UIPath中的Powershell。抛出:无法将类型为“ Microsoft.Exchange.WebServices.Data.GetItemResponse”的对象转换为类型为“ System.String”的

时间:2018-12-04 13:38:44

标签: powershell invoke exchange-server-2010 uipath

我正在尝试使用UI Path中的Powershell从Exchange抓取电子邮件。尝试退货时,出现以下错误:

  

抛出:无法将类型为“ Microsoft.Exchange.WebServices.Data.GetItemResponse”的对象转换为类型为“ System.String”。

即使在UI路径中更改TypeArgument时也是如此。当前,我正在使用Invoke power shell活动,但是使用RunPowershellScript活动却遇到了相同的问题。下面是我在UI路径中的序列,我的参数和我的powershell脚本的屏幕截图,谢谢!

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,"filteremail@mycompany.com")

                $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
}

我注释掉了加载dll,因为它在没有加载dll的情况下仍然可以工作,并且在命中它时抛出了类似的错误。敲$ Exser.LoadPropertyItems时,该代码似乎引发错误。我还尝试过切换到Exchange 2007等。为澄清起见,当在UI Path外部运行纯Powershell时,此代码可以正常工作。

1 个答案:

答案 0 :(得分:0)

我发现了...。它试图加载一个对象,这打破了它。如果我将其保存为变量,然后在脚本中对其进行了转换,并以字符串的形式返回了我所需要的...。猜怎么着?它可以理解字符串。下面是我更新的Powershell。

 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,"emailtofilterby@mycompany.com")

                $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
}