Mongodb 使用PowerShell插入新的Mongo行
我有一个PowerShell脚本,整天都在困扰我。我终于到了可以得到一个收藏的地步,但是我得到了一个我无法理解的错误Mongodb 使用PowerShell插入新的Mongo行,mongodb,powershell,Mongodb,Powershell,我有一个PowerShell脚本,整天都在困扰我。我终于到了可以得到一个收藏的地步,但是我得到了一个我无法理解的错误 function Get-MongoDBCollection { Param( $database, $CollectionName, $settings = $null, #[MongoDB.Driver.MongoCollectionSetting] $returnType = [PSOBJECT] ) $method = $dat
function Get-MongoDBCollection {
Param(
$database,
$CollectionName,
$settings = $null, #[MongoDB.Driver.MongoCollectionSetting]
$returnType = [PSOBJECT]
)
$method = $database.GetType().GetMethod('GetCollection')
$gericMethod = $method.MakeGenericMethod($returnType)
$gericMethod.Invoke($database,[object[]]($CollectionName,$settings))
}
$dbName = "MyDatabaseName"
$collectionName = "MyCollectionName"
try {
add-type -path 'C:\Program Files\MongoDB\Drivers\System.Runtime.InteropServices.RuntimeInformation.4.0.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll'
Add-Type -Path "C:\Program Files\MongoDB\Drivers\MongoDB.Bson.2.6.0\lib\net45\MongoDB.Bson.dll"
add-type -path "C:\Program Files\MongoDB\Drivers\DnsClient.1.0.7\lib\net45\DnsClient.dll";
Add-Type -path "C:\Program Files\MongoDB\Drivers\MongoDB.Driver.Core.2.6.0\lib\net45\MongoDb.Driver.Core.dll"
Add-Type -Path "C:\Program Files\MongoDB\Drivers\MongoDB.Driver.2.6.0\lib\net45\MongoDB.Driver.dll"
}
catch {
$_;
$_.Exception.LoaderExceptions
}
$connectionString = "mongodb://localhost:27018";
$mongoClient = new-object MongoDb.Driver.MongoClient($connectionString);
$mongoDatabase = $mongoclient.GetDatabase($dbName)
$mongoDatabase.GetCollection($collectionname)
$collection = Get-MongoDBCollection $mongodatabase "SharePoint" -returnType ([MongoDB.Bson.BsonDocument]);
$datafile = Get-Content -Raw -Path "D:\datafiles\86fba866-77ed-4f40-4637-08d57d2e25b4.json" #`| ConvertFrom-Json
[MongoDB.Bson.BsonDocument] $doc = [MongoDB.Bson.BsonDocument]::Parse($datafile);
$x = $collection.InsertOne($doc)
脚本获取包含JSON字符串的文件的内容,并将其转换为BsonDocument,然后尝试插入它。我得到以下错误
Argument types do not match
At line:1 char:1
+ $collection.InsertOneAsync($doc)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
我做错了什么?这是一块硬饼干!基本上,powershell不支持泛型。。它支持他们,但以一种复杂而难以理解的方式 几天前,我在另一篇堆栈溢出文章中发现了这个代码段,但现在找不到。一开始这是一次令人不快的搜索/跋涉。原著作者的片段,我希望我能给予适当的信任,如下所示:
###
# Usage:
# $Collection = Get-MongoDBCollection $database 'collectionName'
# or
# $Collection = Get-MongoDBCollection $database 'collectionName' -returnType ([MongoDB.Bson.BsonDocument])
function Get-MongoDBCollection {
Param(
$database,
$CollectionName,
$settings = $null, #[MongoDB.Driver.MongoCollectionSetting]
$returnType = [PSOBJECT]
)
$method = $database.GetType().GetMethod('GetCollection')
$gericMethod = $method.MakeGenericMethod($returnType)
$gericMethod.Invoke($database,[object[]]($CollectionName,$settings))
}
下面是我在上下文中的完整用法。我还必须自己加载DnsClient.dll依赖项。在整个过程中,按照惯例,powershell模块未能提供有用的错误
PS:这是业余的powershell,只使用.PS,因为我需要连接Office365!没有最佳实践的承诺
#########
# Globals
##
$mongoDbDriverPath = "Z:\Work\mb-rule-watch\lib\net45"
$dbName = "mbRules"
$collectionName = "Mailboxes"
#########
# Load o365 credentials/modules
##
Import-Module MsOnline
if( ! $credential ){
Write-Host "Requesting Credentials - Use o365 Admin Account"
$credential = Get-Credential
Connect-MsolService -Credential $credential
}
# Prep remote session connection
if( ! $session ){
$session = New-PSSession `
-ConfigurationName Microsoft.Exchange `
-ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-Credential $credential `
-Authentication Basic `
-AllowRedirection
# import commands from Microsoft Exchange Server shell
Import-PSSession $session
}
###########
# Functions
##
###
# Usage:
# $Collection = Get-MongoDBCollection $database 'collectionName'
# or
# $Collection = Get-MongoDBCollection $database 'collectionName' -returnType ([MongoDB.Bson.BsonDocument])
function Get-MongoDBCollection {
Param(
$database,
$CollectionName,
$settings = $null, #[MongoDB.Driver.MongoCollectionSetting]
$returnType = [PSOBJECT]
)
$method = $database.GetType().GetMethod('GetCollection')
$gericMethod = $method.MakeGenericMethod($returnType)
$gericMethod.Invoke($database,[object[]]($CollectionName,$settings))
}
###########
# MAIN
##
try
{
# Load mongo driver
Add-Type -Path "$($mongoDbDriverPath)\DnsClient.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.Core.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.dll"
# Connect to mongo
$client = new-object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://localhost"
# Get DB handle
[MongoDB.Driver.IMongoDatabase] $db = $client.GetDatabase( $dbName );
# Aquire Collection handle with brute force generic hacks Via a PS god on stackoverflow.
$collection = Get-MongoDBCollection $db $collectionName -returnType ([MongoDB.Bson.BsonDocument])
#foreach( $mbx in $( Get-Mailbox -ResultSize Unlimited -identity example_user_id ) ){
foreach( $mbx in $( Get-Mailbox -ResultSize Unlimited ) ){
$identityStr = $mbx.identity
$rules = Get-InboxRule -Mailbox $identityStr
# convert some huge ints (>Mongo Int64) to strings
foreach( $rule in $rules ){
$rule.RuleIdentity = "" + $rule.RuleIdentity + ""
}
# Json Stringify
$rules_json = ConvertTo-Json $rules
# If the mailbox had rules
if( $rules_json ){
write-host( "Inserting rules for: " + $identityStr )
# Cache results to FS this time.
echo $rules_json > var\rules\$identityStr.rules.json
try{
# Type convert/parse our json string
$document = new-object -TypeName MongoDB.Bson.BsonDocument
$document = [MongoDb.Bson.BsonDocument]::Parse( '{ "_username":"' + $identityStr + '","rules": ' + $rules_json + '}' );
# Insert the JSON document
$collection.InsertOne( $document )
} catch {
Write-Host "JSON parse or mongo insert failure"
foreach( $x IN $_.Exception ){
foreach( $msg IN $x ){
Write-Error $msg
}
}
}
}
}
}
catch
{
Write-Host "Script errors occured"
if( $_.Exception.LoaderExceptions ){
Write-Host "!!Dependency Loads Failed!!"
foreach( $msg IN $_.Exception.LoaderExceptions ){
Write-Error $msg
}
} else {
foreach( $x IN $_.Exception ){
foreach( $msg IN $x ){
Write-Error $msg
}
}
}
}