Buscar..


Observaciones

La parte más difícil es adjuntar un subdocumento al documento que aún no se ha creado. Si necesitamos que el subdocumento tenga el aspecto esperado, deberemos iterar con un bucle for de la matriz en una variable y usar $doc2.add("Key", "Value") lugar de usar la matriz actual de foreach con índice. Esto hará que el subdocumento en dos líneas, como se puede ver en las "Tags" = [MongoDB.Bson.BsonDocument] $doc2 .

MongoDB con controlador C # 1.7 utilizando PowerShell

Necesito consultar todos los detalles de la máquina virtual y actualizar en MongoDB.

Which require the output look like this. 
{
    "_id" : ObjectId("5800509f23888a12bccf2347"),
    "ResourceGrp" : "XYZZ-MachineGrp",
    "ProcessTime" : ISODate("2016-10-14T03:27:16.586Z"),
    "SubscriptionName" : "GSS",
    "OS" : "Windows",
    "HostName" : "VM1",
    "IPAddress" : "192.168.22.11",
    "Tags" : {
        "costCenter" : "803344",
        "BusinessUNIT" : "WinEng",
        "MachineRole" : "App",
        "OwnerEmail" : "[email protected]",
        "appSupporter" : "Steve",
        "environment" : "Prod",
        "implementationOwner" : "[email protected]",
        "appSoftware" : "WebServer",
        "Code" : "Gx",
        "WholeOwner" : "[email protected]"
    },
    "SubscriptionID" : "",
    "Status" : "running fine",
    "ResourceGroupName" : "XYZZ-MachineGrp",
    "LocalTime" : "14-10-2016-11:27"
}

Tengo 3 conjuntos de matriz en Powershell

        $MachinesList  # Array 
        $ResourceList # Array
        $MachineTags  # Array
    
    pseudo code 

        $mongoDriverPath = 'C:\Program Files (x86)\MongoDB\CSharpDriver 1.7';
        Add-Type -Path "$($mongoDriverPath)\MongoDB.Bson.dll";
        Add-Type -Path "$($mongoDriverPath)\MongoDB.Driver.dll";

 $db = [MongoDB.Driver.MongoDatabase]::Create('mongodb://127.0.0.1:2701/RGrpMachines');
    [System.Collections.ArrayList]$TagList = $vm.tags 
      $A1 = $Taglist.key
      $A2 = $Taglist.value 
    foreach ($Machine in $MachinesList) 
    {
        foreach($Resource in $ResourceList) 
        {
                    $doc2 = $null
                   [MongoDB.Bson.BsonDocument] $doc2 = @{}; #Create a Document here 
                    for($i = 0; $i -lt $TagList.count; $i++)
                           {
                                $A1Key = $A1[$i].ToString()
                                $A2Value = $A2[$i].toString()
                                $doc2.add("$A1Key", "$A2Value")
                           }
                    
                     [MongoDB.Bson.BsonDocument] $doc = @{
                        "_id"= [MongoDB.Bson.ObjectId]::GenerateNewId();
                        "ProcessTime"= [MongoDB.Bson.BsonDateTime] $ProcessTime;
                        "LocalTime" = "$LocalTime";
                        "Tags" = [MongoDB.Bson.BsonDocument] $doc2; 
                        "ResourceGrp" = "$RGName"; 
                        "HostName"= "$VMName";
                        "Status"= "$VMStatus";
                        "IPAddress"= "$IPAddress";
                        "ResourceGroupName"= "$RGName";
                        "SubscriptionName"= "$CurSubName";
                        "SubscriptionID"= "$subid";
                        "OS"= "$OSType";
                    }; #doc loop close
 
                    $collection.Insert($doc);
            }
    }


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow