2017-09-26 58 views
0

我在使用JSON格式的变量($ report)中回顾VM利用率详细信息。MongoDB与Powershell

$报告看起来像:

{ 
    { 
    "VM_Name" : "VMtest1" 
    "Datastore":"KJo91" 
    }, 
    { 
    "VM_Name" : "VMtest2" 
    "Datastore":"KJo91" 
    },so on.... 
} 

现在我需要通过PowerShell来导入到MongoDB的这一点。 可能吗?或者我应该将它保存为Jsonfile并导入? 对于这两种情况都需要一个powershell命令。

预先感谢您。

回答

0

是的,我已经使用C#MongoDB驱动程序,我可以导入一个JSON文件(Sample1.json)到MongoDB,例如,

# Command Used 
$report = Get-VM #retrives the VM Details 
$report | ConvertTo-Json -Depth 1 | Out-File "Path" # Here a file is saved in the specified path and I can import the file to MongoDB using the below command: 
.\mongoimport.exe --db $dbName --collection $collectionName ./sample1.json --jsonArray 

而不是创建一个文件,我使用的是具有JSON格式的数据的变量:

# Command used 
$result = $report | ConvertTo-Json -Depth 1 

现在我必须导入$result变量(包含集合/文件)进入MongoDB。