none
EWS and PowerShell script failing on Windows 7 while succeeding on Windows 2008 R2? RRS feed

  • Question

  • Hi,

    I have built a script to access my office 365 mailboxes using delegation rights admin has. The script is working fine at Windows 2008 R2 server, while failing on 2 Windows 7 machines with this error:

    Exception calling "FindItems" with "2" argument(s): "The request failed. The underlying connection was closed: An unexpected error occurred on a send."

    Can anyone help on this:

    This is the script:

    $dllpath = "C:\Program Files\Microsoft\Exchange Server\V14\webservice\Microsoft.Exchange.WebServices.dll"; $useAutoDiscovery=$true; $service = $null; $Exchange_Server="pod51015.outlook.com"; $Admin = "admin@domain.com"; $AdminPassword = "adminpassword"; $uri = "https://$Exchange_Server/ews/exchange.asmx"; $MailboxName = "user@domain.com"; ############################################### #Script starting here ############################################### Import-Module -Name $dllpath $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010) ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates ## Code From http://poshcode.org/624 ## Create a compilation environment $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider $Compiler=$Provider.CreateCompiler() $Params=New-Object System.CodeDom.Compiler.CompilerParameters $Params.GenerateExecutable=$False $Params.GenerateInMemory=$True $Params.IncludeDebugInformation=$False $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null $TASource=@' namespace Local.ToolkitExtensions.Net.CertificatePolicy{ public class TrustAll : System.Net.ICertificatePolicy { public TrustAll() { } public bool CheckValidationResult(System.Net.ServicePoint sp, System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem) { return true; } } } '@ $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource) $TAAssembly=$TAResults.CompiledAssembly ## We now create an instance of the TrustAll and attach it to the ServicePointManager $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll") [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll ## end code from http://poshcode.org/624 $service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials($Admin, $AdminPassword); if($useAutoDiscovery) { #CAS URL Option 1 Autodiscover $service.AutodiscoverUrl($Admin,{$true}) } else { # Change the URL to point to your cas server $service.Url= new-object Uri("https://$Exchange_Server/EWS/Exchange.asmx") }

    "Using CAS Server : " + $Service.url Write-host "Searching emails in Mailbox :" $MailboxName -foregroundcolor Magenta $RecieptTime = $null; $itemView = new-object Microsoft.Exchange.WebServices.Data.ItemView(1,0,[Microsoft.Exchange.WebServices.Data.OffsetBasePoint]::Beginning); $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties) $psPropertySet.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived) $itemView.PropertySet = $psPropertySet $inbox = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox, $MailboxName); Write-Host -ForegroundColor Magenta "Finding Items...."; $oFindItems = $service.FindItems($inbox,$itemView) if($oFindItems -ne $null -and $oFindItems.TotalCount -gt 0) { foreach ($Item in $oFindItems.Items) { $RecieptTime = $null if($Item.TryGetProperty([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived,[ref]$RecieptTime )-eq $true) { Write-Host -ForegroundColor Magenta "Reciept time: $RecieptTime "; } $psPropertySet =$null; $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties) $message =[Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind($service, $Item.Id, $psPropertySet); Write-Host -ForegroundColor Gray $message.From.Address;

    }

    }

    Write-Host -ForegroundColor Green "Script Ended";



    Regards, David Johnson

    Tuesday, April 16, 2013 3:55 PM

Answers

  • Hi,

    It was so easy in the end, to just run set-ExecutionPolicy cmdlet like this 

     Set-ExecutionPolicy -ExecutionPolicy unrestricted

    and then script ran successfully on all win 7 machines.


    Regards, David Johnson

    • Marked as answer by Cyber Friend Tuesday, April 16, 2013 4:35 PM
    Tuesday, April 16, 2013 4:35 PM