首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VMWare对象上的ConvertTo-Json不起作用

VMWare对象上的ConvertTo-Json不起作用
EN

Stack Overflow用户
提问于 2016-06-24 20:04:33
回答 4查看 3.5K关注 0票数 1

在powershell中将VM对象转换为json时,($json = ConvertTo-Json $vm -Compress)

我得到了“具有相同关键字的项目已被添加”异常。

代码语言:javascript
复制
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来宾的列表。请在下面找到我的代码。

代码语言:javascript
复制
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}   
}
EN

回答 4

Stack Overflow用户

发布于 2016-06-25 03:43:56

试试ConvertTo-JSON -Depth 1Sounds like对象中有同名的属性。

票数 3
EN

Stack Overflow用户

发布于 2016-06-26 01:35:18

我没有使用VCenter来验证脚本,但我对您的脚本进行了一些重构,使其更强大。

备注:

CmdletBinding为您提供了-Verbose和其他特性

缺省情况下,任何未设置为变量的对象都会输出到管道

Return没有做大多数开发人员所期望的事情

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-02-24 07:14:31

答案已删除,因为可能没有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38013036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档