2017-12-01 15 views
0

例外$ service.LoadPropertiesForItems($結果、[Microsoft.Exch ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) []、MethodInvocationException + FullyQualifiedErrorId:ArgumentNullException例外呼び出しLoadPropertiesForItems

私は以下のスクリプトを実行し、このエラーを取得します。スクリプトのedは11行目のmailbox identityです。私はEWSについてよく知らないです。誰も助けることができますか?

add-pssnapin Microsoft.Exchange.Management.PowerShell.snapin 

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll" 
[void][Reflection.Assembly]::LoadFile($dllpath) 
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1) 
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() 
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">" 
$aceuser = [ADSI]$sidbind 
$service.AutodiscoverUrl($aceuser.mail.ToString()) 

$MailboxName = get-mailbox -Identity [email protected] 

$folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName.PrimarySmtpAddress.ToString()) 
$rootfolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folderidcnt) 


$offset = 0; 
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(10000, $offset) 

$response = $service.LoadPropertiesForItems($results, [Microsoft.Exchange.WebServices.Data.PropertySet]::FirstClassProperties) 

foreach ($mail in $results){ 

if ($mail.ToString() -eq "Microsoft.Exchange.WebServices.Data.EmailMessage") { 
    $mailSubject = $mail.Subject 
    $mailProps = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent) 
    $mail.Load($mailProps) 
    #TODO: clean up $mailSubject so it's filesystem friendly 
    $fStream = New-Object System.IO.FileStream("C:\Temp\$mailSubject.eml", [System.IO.FileMode]::Create) 
    $fStream.Write($mail.MimeContent.Content, 0, $mail.MimeContent.Content.Length) 
    $fStream.Close() 
} 
} 

ありがとうグレン。私は3つのメールをエクスポートしようとするメールボックスがあります。あなたのスクリプトは最初の電子メールをエクスポートし、これをスローします: New-Object: "2"引数を持つ ".ctor"を呼び出す例外: "パスに不正な文字があります。ラインで :26文字:28 + ... $ FSTREAM =新オブジェクトSystem.IO.FileStream( "C:\ Tempに\ $ mailSubject.eml ... + ~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 「閉じられたファイルにアクセスできません:はInvalidOperation:(:) [新オブジェクト]、MethodInvocationException + FullyQualifiedErrorId: "3" 引数(複数可)を "書く" を呼び出すConstructorInvokedThrowException、Microsoft.PowerShell.Commands.NewObjectCommand

例外。」 行:27文字:17 + ... $ fStream.Write($ mail.MimeContent.Content、0、$ mail.MimeCo ... + ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ egoryInfo:NotSpecified:(:) []、MethodInvocationException + FullyQualifiedErrorId:説明ObjectDisposedException

新オブジェクト:例外は "2" 引数(複数可)と ".ctor" を呼び出す: "指定されたパスのフォーマットがサポートされていない" 行:26文字:28 + ... $ FSTREAM =新オブジェクトSystem.IO.FileStream( "C:\ Tempに\ $ mailSubject.eml ... + ~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ InvalidOperation:(:) [New-Object]、MethodInvocationException + FullyQualifiedErrorId:ConstructorInvokedThrowException、Microsoft.PowerShell.Commands.NewObjectCommand

例外は、 "3" の引数(複数可)を "書く" を呼び出す: "閉じられたファイルにアクセスできません。" ラインで :27文字:17 + ... $ fStream.Write($ mail.MimeContent.Content、0、 $ mail.MimeCo ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) []、MethodInvocationException + FullyQualifiedErrorId:あなたがする必要はありません説明ObjectDisposedException

+0

'上のスクリプト - どこですか? –

+0

2番目のパラメータはPropertySetオブジェクトである必要があり、型を渡すように見えます。あなたはあなたのフルコードを投稿する必要があります –

+0

ありがとうグレン@グレン。ここに私のコードです: – akurbanyan

答えて

0

あなたがFindItemsコールを行方不明のすべて繰り返しでMimeContentをロードする呼び出しを行うため、LoadProperitesFromItemsを使用します。

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll" 
    [void][Reflection.Assembly]::LoadFile($dllpath) 
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1) 
    $windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() 
    $sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">" 
    $aceuser = [ADSI]$sidbind 
    $service.AutodiscoverUrl($aceuser.mail.ToString()) 

    $MailboxName = get-mailbox -Identity [email protected] 

    $folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName.PrimarySmtpAddress.ToString()) 
    $rootfolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folderidcnt) 

    $offset = 0; 
    $view = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000, $offset) 
    $results = $null 
    do{ 
     $results = $rootfolder.FindItems($view) 
     foreach ($mail in $results.Items){ 
      if ($mail -is [Microsoft.Exchange.WebServices.Data.EmailMessage]) 
      { 
       $mailSubject = $mail.Subject 
       $mailProps = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent) 
       $mail.Load($mailProps) 
       #TODO: clean up $mailSubject so it's filesystem friendly 
       $fStream = New-Object System.IO.FileStream("C:\Temp\$mailSubject.eml", [System.IO.FileMode]::Create) 
       $fStream.Write($mail.MimeContent.Content, 0, $mail.MimeContent.Content.Length) 
       $fStream.Close() 
      } 
     }  
     $view.Offset += $result.Items.Count  
    }while($results.MoreAvailable -eq $true) 

このコードは、受信トレイ内のすべてのアイテムをほぼページし、各メッセージをEMLファイルとしてダウンロードします。

+0

ありがとうグレン。私は3つのメールをエクスポートしようとするメールボックスがあります。あなたのスクリプトは、最初のメールを以下のようにエクスポートします: – akurbanyan

+0

こんにちは@Glenスケール – akurbanyan

+0

エラーを見れば、十分な固有のファイル名が生成されないからです。私は、これは有効であるか、またはユニークなファイル名ではないかもしれないので、件名を使用しないことをお勧めします。 fStream = New-Object System.IO.FileStream(( "C:\ Temp \" + [ Guid] :: NewGuid()。tostring()+ ".eml")、[System.IO.FileMode] :: Create) –

関連する問題