none
Add a hashtable to another hashtable RRS feed

  • Question

  • ########################################################################################################################
    Function HDD_Information($Output)
    {
        $Server = $Output.Server
        $Output1 = @{};
        $HDDInfor = gwmi win32_diskdrive -ComputerName $Server -EA stop
        Foreach ($HDD in $HDDInfor)
        {
           $Output."Size" = $HDD.Size
           $Output."Model" = $HDD.Model
           $Output1.Add($Output)
        }
        return $Output1
                
    

    I am trying to add a hashtable to another hastable, but i get an error  :- 

    Cannot find an overload for "Add" and the argument count: "1"

    +                 $Output1.Add <<<< ($Output)
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest

    First Hashtable = $Output 

    The $Output variable contains the follwing values

    @{Model=VMware Virtual disk SCSI Disk Device; Size=12231242555}

    @{Model=VMware Virtual disk SCSI Disk Device; Size=84853845435546}

    Second Hashtable = $Output1

    So When the function returns 2 values, the script enters only one value in the csv file.. I want to save both the values in a hashtable and enter into the csv. Have got the csv part all figured out, just having problem adding both the return values in CSV.

    Any help is appreciated!

    Wednesday, March 25, 2015 9:24 AM

Answers

  • I hear you. my code is wrong..

    Now since all of my servers have 2 physical disks...

    gwmi win32_diskdrive -ComputerName $Server -EA stop |select Size, Model

    This command returns 2 values .. one value for physical 0 and another value for physical 1..my code is designed to work with only 1 value.

    How do i make it work for 2 values and feed it into CSV.

    gwmi win32_diskdrive -ComputerName $Server |select Size, Model | Export-Csv myfile.csv -NoType

    ¯\_(ツ)_/¯


    • Edited by jrv Monday, March 30, 2015 8:43 AM
    • Marked as answer by Koz0s Monday, March 30, 2015 10:41 AM
    Monday, March 30, 2015 8:43 AM

All replies

  • $Output1.Add('Output',$Output)

    ¯\_(ツ)_/¯

    Wednesday, March 25, 2015 1:23 PM
  • $Output1.Add('Output',$Output)

    ¯\_(ツ)_/¯

    Error :-

    Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictio
    nary: 'Output'  Key being added: 'Output'"

    +                 $Output1.Add <<<< ('Output', $Output)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    Also When I try 

    $Output1 +=$Output

    I get an error stating that 

    You can add another hash table only to a hash table.
    +                 $Output1 += <<<< $Output
        + CategoryInfo          : InvalidOperation: (@{HDD Name=VMwa...Time - Day=183}:PSObj 
       ect) [], RuntimeException
        + FullyQualifiedErrorId : AddHashTableToNonHashTable
     

    Thursday, March 26, 2015 8:02 AM
  • The answer is correct fo ryour question however the rest of your code is wrong.  You cannot add multiple items to the same hash.

    This is all you need to do:

    gwmi win32_diskdrive -ComputerName $Server -EA stop |select Size, Model


    ¯\_(ツ)_/¯

    Thursday, March 26, 2015 11:38 AM
  • I hear you. my code is wrong..

    Now since all of my servers have 2 physical disks...

    gwmi win32_diskdrive -ComputerName $Server -EA stop |select Size, Model

    This command returns 2 values .. one value for physical 0 and another value for physical 1..my code is designed to work with only 1 value.

    How do i make it work for 2 values and feed it into CSV.

    • Edited by Koz0s Monday, March 30, 2015 7:32 AM
    Monday, March 30, 2015 7:15 AM
  • I hear you. my code is wrong..

    Now since all of my servers have 2 physical disks...

    gwmi win32_diskdrive -ComputerName $Server -EA stop |select Size, Model

    This command returns 2 values .. one value for physical 0 and another value for physical 1..my code is designed to work with only 1 value.

    How do i make it work for 2 values and feed it into CSV.

    gwmi win32_diskdrive -ComputerName $Server |select Size, Model | Export-Csv myfile.csv -NoType

    ¯\_(ツ)_/¯


    • Edited by jrv Monday, March 30, 2015 8:43 AM
    • Marked as answer by Koz0s Monday, March 30, 2015 10:41 AM
    Monday, March 30, 2015 8:43 AM