2016-06-24 10 views
1

In Powershell während VM-Objekte json Umwandlung ($ json = ConvertTo-Json $ vm -compress)ConvertTo-Json auf VMWare Objekte funktioniert nicht

i mit dem gleichen Schlüssel „Ein Element bin immer hat wurde bereits hinzugefügt "Ausnahme.

PS SQLSERVER:\> C:\Users\admin\Desktop\inventory.ps1 
ConvertTo-Json : An item with the same key has already been added. 
At C:\Users\huradmin\Desktop\inventory.ps1:68 char:31 
+  if($vm -ne $null){$json = ConvertTo-Json $vm -Compress;  insertToElasticSearc ... 
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : NotSpecified: (:) [ConvertTo-Json], ArgumentException 
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertToJsonCommand 

insertToElasticSearch : Cannot bind argument to parameter 'json' because it is null. 
At C:\Users\admin\Desktop\inventory.ps1:68 char:89 
+ ... icSearch -json $json -info:$true -Verbose:$true} 
+     ~~~~~ 
+ CategoryInfo   : InvalidData: (:) [insertToElasticSearch], ParameterBindingValidationException 
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,insertToElasticSearch 

getVMHosts Funktion gibt eine Liste der VM Gäste. Bitte finden Sie meinen Code unten.

function getVMHosts{ 
Param(
[Parameter(Mandatory=$True,Position=1)] 
[string]$vcenter, 
[Parameter(Mandatory=$False)] 
[switch]$info=$false 
) 
try 
{ 
    Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Importing VMWare modules" -verbose:$info 
    Get-Module -ListAvailable -Name "VMware.*" | Import-Module 
    Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Connecting to Vcenter:$vcenter" -verbose:$info 
    [void]::$(Connect-VIServer -Server $vcenter -ErrorAction SilentlyContinue) 
    Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Getting Data center servers" -verbose:$info 
    $DCs = Get-Datacenter 
    $VMs = $null 
    foreach($dc in $DCs) 
    { 
     Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Getting VM servers for Data Center:$dc" -verbose:$info 
     $VMs=$VMs+ $(Get-Datacenter -Name $dc.Name | Get-VM -Verbose:$info| Select PowerState,Name, NumCpu,MemoryMB,GuestId,VMHost, @{N="IP Address";E={@($_.guest.IPAddress[0])}}) 
    } 
    Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Disconnecting from VCenter:$vcenter" -verbose:$info 
    Disconnect-VIServer -Server $vcenter -ErrorAction SilentlyContinue -Confirm:$false 
    Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Returning VM Lists" -verbose:$info 
    return $VMs 
} 
catch 
{ 
    $errorMessage = "$($_.Exception.Message)`n$(($_|select -ExpandProperty invocationinfo).PositionMessage)" 
    Write-Warning -Message "Catched an exception in Function:$($MyInvocation.MyCommand)`n$errorMessage" -Verbose:$true 
} 
} 
$vmHosts = getVMHosts -vcenter "vcenter" 
$counter = 0 
foreach($vm in $vmHosts) 
{  
if($vm -ne $null){$json = ConvertTo-Json $vm -Compress;insertToElasticSearch json $json -info:$true -Verbose:$true} 
} 
+0

Versuchen Sie, "return" in "getVMHosts" zu entfernen und durch "$ VMs" in einer eigenen Zeile zu ersetzen. https://technet.microsoft.com/en-us/library/hh847760.aspx – Eris

+0

Außerdem spuckt der 'Disconnect-VIServer'-Befehl möglicherweise einen Wert in die Pipeline zurück, da Sie ihn nicht auf eine Variable oder Besetzung festgelegt haben es zu '[void]' – Eris

Antwort

0

Versuchen Sie ConvertTo-JSON -Depth 1. Sounds like Es gibt Eigenschaften im Objekt, die denselben Namen haben.

0

Ich habe kein VCenter, um das Skript zu verifizieren, aber ich habe Ihr Projekt etwas umstrukturiert, um es potenter zu machen.

Hinweise:

CmdletBinding gibt Ihnen -Verbose und andere Merkmale
Jedes Objekt auf eine Variable ausgegeben wird standardmäßig auf die Pipeline nicht gesetzt
Return tun nicht, was die meisten Entwickler

function getVMHosts{ 
    [CmdletBinding()] 
    Param(
     [Parameter(Mandatory=$True,Position=1)] 
     [string]$vcenter, 
    ) 
    try 
    { 
     Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Importing VMWare modules" 
     Get-Module -ListAvailable -Name "VMware.*" | Import-Module 
     Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Connecting to Vcenter:$vcenter" 
     [void]$(Connect-VIServer -Server $vcenter -ErrorAction SilentlyContinue) 
     Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Getting Data center servers" 
     Get-Datacenter | 
      ForEach-Object { 
       Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Getting VM servers for Data Center:$_" 
       Get-Datacenter -Name $_.Name | 
        Get-VM -Verbose:$Verbose| 
        Select PowerState, Name, NumCpu, MemoryMB, GuestId, VMHost, @{N="IP Address";E={@($_.guest.IPAddress[0])}} 
     } 

     Write-Verbose "$(get-date -Format "dd/MM/yyyy HH:mm") - Function:$($MyInvocation.MyCommand) - Disconnecting from VCenter:$vcenter" 
     [void]Disconnect-VIServer -Server $vcenter -ErrorAction SilentlyContinue -Confirm:$false 
    } 
    catch 
    { 
     $errorMessage = "$($_.Exception.Message)`n$(($_|select -ExpandProperty invocationinfo).PositionMessage)" 
     Write-Warning -Message "Exception caught in Function:$($MyInvocation.MyCommand)`n$errorMessage" 
    } 
} 

getVMHosts -vcenter "vcenter" | 
    ForEach-Object { 
     $json = ConvertTo-Json $_ -Compress; 
     insertToElasticSearch json $json -info:$true -Verbose:$true 
    } 
} 
erwarten
+0

danke für Ihre Antwort und Kommentare. Ich habe versucht, den obigen Code auszuführen, aber immer noch den gleichen Fehler bei der ConvertTo-Json-Operation. –