2017-07-10 8 views
3

は、私はすでに次のコードで試してみました:MongoDBとPowerShellを接続するには?

$mongoDbDriverPath = ' 
C:\Mongodb\net45\' 
$mongoServer = 'localhost:27017' 

Add-Type -Path "$($mongoDbDriverPath)MongoDB.Bson.dll" 
Add-Type -Path "$($mongoDbDriverPath)MongoDB.Driver.dll" 
$databaseName = "test" 
$collectionName = "sample" 
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://localhost:27017" 
$server = $client.GetServer() 
$database = $server.GetDatabase($databaseName) 
$collection = $database.GetCollection($collectionName) 
Write-Host $server,$database,$collection 
$query = [MongoDB.Driver.Builders.Query]::EQ("Name", "sample") 

$results = $collection.Find($query) 
$results 

が、それはいくつかのエラーを示しています

新オブジェクト:例外呼び出し "1" 引数(複数可)と ".ctor":「でしたSystem.Runtime.InteropServices.RuntimeInformation、Version = 4.0.0.0、 `Culture = neutral、PublicKeyToken = b03f5f7f11d50a3a 'またはその依存関係の1つをロードできません。指定されたファイルが見つかりません。 Dで
:PowerShellの\のtask1.ps1で\ Users \ユーザーXXXXXX \デスクトップ\ MongoDBは:8文字:11

どのように私はこのエラーを克服するのですか?

+1

あなたは.NET 4.0 CLR(PowerShellの3.0以降)上で動作するのPowerShellのバージョンを使用していることを確認してください。 '$ PSVersionTable'の出力をあなたの質問に投稿してください –

+0

PSVersion:5.0.10586.962 CLRVersion:4.0.30319.42000 私は上記のバージョンpowershellを使用しています –

+0

解決方法はありますか?私は同じ問題を抱えています。 –

答えて

0

TL; DR:

だから、ちょうどこのアセンブリを見つけるとMongoDriverと同じフォルダに追加する必要があります。私はビジュアルスタジオのコンソールアプリケーションを作成し、それをNugetのドライバを追加して見つけました。 interopアセンブリはpackagesディレクトリにあります。

https://www.nuget.org/packages/mongocsharpdriver/2.5.0/

研究:

ここで概説としてログの融合を有効にする:

https://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx

今、あなたはリクのようなものが表示されるはずですMongoClient

Add-Type -Path "C:\temp\Mongo\MongoDB.Bson.dll" 
Add-Type -Path "C:\Temp\Mongo\MongoDB.Driver.dll" 
$mongoClient = New-Object MongoDB.Driver.MongoClient 

を初期化しますフュージョンログのE:

*** Assembly Binder Log Entry (12/29/2017 @ 1:50:26 PM) *** 

The operation failed. 
Bind result: hr = 0x80070002. The system cannot find the file specified. 

Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll 
Running under executable C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 
--- A detailed error log follows. 

=== Pre-bind state information === 
LOG: DisplayName = System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
(Fully-specified) 
LOG: Appbase = file:///C:/Windows/System32/WindowsPowerShell/v1.0/ 
LOG: Initial PrivatePath = NULL 
LOG: Dynamic Base = NULL 
LOG: Cache Base = NULL 
LOG: AppName = powershell.exe 
Calling assembly : MongoDB.Driver.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=null. 
=== 
LOG: This bind starts in LoadFrom load context. 
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load(). 
LOG: No application configuration file found. 
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config. 
LOG: Publisher policy file is not found. 
LOG: Post-policy reference: System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
LOG: GAC Lookup was unsuccessful. 
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation.DLL. 
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.DLL. 
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation.EXE. 
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.EXE. 
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation.DLL. 
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.DLL. 
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation.EXE. 
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.EXE. 
LOG: All probing URLs attempted and failed. 

だから誤差に基づいて、それはアセンブリMongoDB.Driver.Coreをロードしていますし、例外がスローされている場所つまり。

Reflected Mongo Assembly このアセンブリを見つけて、MongoDriverと同じフォルダに追加するだけです。私はビジュアルスタジオのコンソールアプリケーションを作成し、それをNugetのドライバを追加して見つけました。 interopアセンブリはpackagesディレクトリにあります。

https://www.nuget.org/packages/mongocsharpdriver/2.5.0/