2016-06-24 30 views
1

在PowerShell中,同時將VM對象爲JSON, ($ JSON =的ConvertTo JSON的$​​ VM -compress)的ConvertTo JSON的在VMWare對象不起作用

我得到「的項目使用相同的密鑰具有已被添加「的例外。

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函數返回VM guest虛擬機的列表。請在下面找到我的代碼。

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

嘗試刪除'getVMHosts'中的「return」,並用'$ VMs'替換它自己的一行。 https://technet.microsoft.com/en-us/library/hh847760.aspx – Eris

+0

此外,'Disconnect-VIServer'命令可能會將值分散回管道中,因爲您沒有將其設置爲變量或轉換它以'[無效]' – Eris

回答

0

嘗試ConvertTo-JSON -Depth 1Sounds like對象中的屬性具有相同的名稱。

0

我沒有VCenter來驗證腳本,但我重構了一下,使它更加強大。

注:

CmdletBinding給你-Verbose和其他功能
未設置爲一個變量的任何對象輸出到管道默認
Return沒有做什麼,大多數開發人員希望

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 
    } 
} 
+0

感謝您的回覆和評論。我試圖運行上面的代碼,但在ConvertTo-Json操作中仍然出現相同的錯誤。 –