Powershell:Add-Member:无法添加名称为“”的成员,因为已存在具有该名称的成员

时间:2014-02-08 16:52:59

标签: powershell csv

我正在尝试使用Dell服务器上的PowerShell和racadm提取一些信息。脚本如下,我无法添加新的机箱/刀片信息...

我尝试了Select-Object,但Export-Csv未显示值,而是显示为System.Object[]

##################################################
# Variables that need to be set for script to work
#Path to RACADM Executable
$racadmpath = "C:\Program Files\Dell\SysMgt\rac5"
# Username and Password for DRAC Account with configure access. Suggested that this
# be set in the script you which to use with, or configured as script command line
# arguments for security.

$username = *****
$pass = *******
$ipchbld = Import-Csv 'ipCHBLD.csv'
#####################################################################################

$MyCollection = @()
foreach ($ip in $ipchbld) {
    $arrayobj = @()
    $object = New-Object PSObject
    $object | add-member -MemberType NoteProperty -Name "Chassis Slot" -value $ip.Chassis_Slot
    $object | add-member -MemberType NoteProperty -Name "DRAC IP" -value $ip.DRAC_IP
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getchassiname | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "Chassis Name" -value $line[0]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getsvctag | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "" -And $line -match "Chassis") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "Chassis Service Tag" -value $line[1]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -m cmc-1 | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "CMC" -value $line[0]
            $object | add-member -MemberType NoteProperty -Name "CMC Version" -value $line[1]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -m cmc-2 | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "CMC" -value $line[0]
            $object | add-member -MemberType NoteProperty -Name "CMC Version" -value $line[1]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getslotname | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "Blade Slot" -value $line[1]
            $object | add-member -MemberType NoteProperty -Name "Blade Name" -value $line[2]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -c | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "" -ANd $line -match "server") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "Blade Type" -value $line[2]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getsvctag | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "" -And $line -match "Server") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "Blade Service Tag" -value $line[1]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -l -f bios | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "BIOS" -value $line[2]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -l -f idrac | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "iDRAC" -value $line[2]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -l -f usc | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "Unified Server Configurator" -value $line[2]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -l -f diag | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "32-bit Diagnostics" -value $line[2]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -l -f drivers | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "OS Driver Package" -value $line[3]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -l -f nic | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "" -ANd $line -match "server") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "NIC Type" -value $line[1]
            $object | add-member -MemberType NoteProperty -Name "NIC FW Version" -value $line[9]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -l -f raid | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "" -ANd $line -match "Controller 0") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "PERC H700 Modular" -value $line[6]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion -c | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "" -ANd $line -match "server") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "CPLD Version" -value $line[1]
            }
            }
    $output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getkvminfo | select -Skip 8
    foreach ($line in $output) {
        if ($line -ne "") {
            $line = $line -replace '\s+', ','
            $line = $line.Split(",")
            $object | add-member -MemberType NoteProperty -Name "iKVM FW Version" -value $line[5]
            }
            }
    $arrayobj += $object
    }
$MyCollection += $arrayobj
$MyCollection | Export-Csv "C:\FWReport\CHBLD.csv" -UseCulture -NoTypeInformation

2 个答案:

答案 0 :(得分:0)

看起来你没有正确解析racadm.exe的输出,但不知道它看起来是什么样子是不可能的。

答案 1 :(得分:0)

您是否确信$output只包含您感兴趣的数据的非空行?如果没有,您的foreach ($line in $output) {}将尝试两次添加相同的属性名称。您可以输入break语句以确保不会发生。

$output = & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getchassiname | select -Skip 8
foreach ($line in $output) {
    if ($line -ne "") {
        $line = $line -split '\s+'
        $object | add-member -MemberType NoteProperty -Name "Chassis Name" -value $line[0]
        break
    }
}

要处理属性已存在的情况(追加数据),请尝试以下方法:

$prop = 'ChassisName'
if ($object.psobject.properties[$prop]) {
    $object.$prop += $line[0]
}
else {
    $object | add-member -MemberType NoteProperty -Name $prop -value $line[0]
}

BTW删除属性名称中的空格,以使您的生活更轻松。 : - )

更新:我没有运行racadm.exe所以我猜它的输出。试试这个:

$MyCollection = @()
foreach ($ip in $ipchbld) {
    $object = New-Object PSObject -Property @{
                  ChassisSlot=$ip.Chassis_Slot
                  ChassisIP=$ip.DRAC_IP
                  ChassisName=''
                  ChassisSvcTag=@('')
                  CMC=@('')
                  CMCVersion=@('')}

    & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getchassisname | 
        Select -Skip 8 | Where {$_} | 
        Foreach {$object.ChassisName = ($_ -replace '\s+',',' -split ',')[0]}

    & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getsvctag | 
        Select -Skip 8 | Where {$_ -match 'Chassis'} | 
        Foreach {$object.ChassisSvcTag += ($_ -replace '\s+',',' -split ',')[1]}

    & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion | 
        Select -Skip 60 | Where {$_} | 
        Foreach {$object.CMC += ($_ -replace '\s+',',' -split ',')[0]}

    & $racadmpath\racadm.exe -r $ip.DRAC_IP -u $username -p $pass getversion | 
        Select -Skip 60 | Where {$_} | 
        Foreach {$object.CMCVersion += ($_ -replace '\s+',',' -split ',')[1]}

    $MyCollection += $object
}

$MyCollection
$MyCollection | Export-Csv "C:\Users\FWReport\CHBLD.csv" -UseCulture -NoTypeInformation