Share via


Question on a script, getting my arrays and hashtables crossed

Question

Tuesday, July 10, 2012 5:00 PM

I'm writing a script using the SMLets module to modify the classification of a bunch of records in our SCSM 2010 environment.  The logic of the script seems to work pretty well, but when I'm trying to populate my "fallout" list, I keep getting this error:

You can add another hash table only to a hash table.
At C:\Powershell\SCSMProblemClassifications.ps1:38 char:38

  •         $Falloutlist = $Falloutlist + <<<<  $CurrentItem
        + CategoryInfo          : InvalidOperation: (System.Object:Object) [], RuntimeException
        + FullyQualifiedErrorId : AddHashTableToNonHashTable

I'm pretty sure the object $CurrentItem is not a hashtable, and when I run get-member against it, here's the results:

TypeName: System.Object

Name           MemberType   Definition
           
Equals         Method       bool Equals(System.Object obj)
GetHashCode    Method       int GetHashCode()
GetType        Method       type GetType()
ToString       Method       string ToString()
Classification NoteProperty System.String Classification=
ID             NoteProperty System.String ID=PR645

I've done this sort of thing before with other scripts and it worked fine, so I'm not sure what I'm missing this time around.  For reference, here's my complete script in progress:

#=============================================================================#
#                    
# SCSMProblemClassifications.ps1
# Powershell Script to gather import and update
# active SCSM problems with new classifications
# Author: Nick Fields
# Last Modified: 10.7.2012
# Version: 0.9
#               
#=============================================================================#


##Initialize Vars and import Old and New Classifications to Hash Table##
Import-Module SMLets -ErrorAction SilentlyContinue
$ClassList = @{}
$Falloutlist = @{}
Import-Csv c:\powershell\MasterlistofProb.csv | foreach {$ClassList[$_.OldClass] = $_.NewClass}
$Property = "Classification"

##Get Changes from SCSM Database
$Problems = Get-SCSMObject -Class (Get-SCSMClass -Name System.WorkItem.Problem$)

foreach ($line in $Problems) {
    $classmatch = $ClassList.Keys | where {$line.Classification -match $_}
    [string]$ID = $line.ID
    [string]$Class = $line.Classification
    if ($classmatch) {
        Write-Host $ClassList.$classmatch
        Set-SCSMObject $line -Property $Property -Value $ClassList.$classmatch -WhatIf
        Write-Host "Successfully set" $line.ID

        }
    elseif ($line.Area -eq $null) {
        $CurrentItem = New-Object system.Object
        $CurrentItem | Add-Member -type Noteproperty -Name ID -Value $ID
        $CurrentItem | Add-Member -type Noteproperty -Name Classification -Value " "
        Write-Host "No match for" $line.ID "and mapping is empty"   
        $Falloutlist = $Falloutlist + $CurrentItem   
    }
    else {
        $CurrentItem = New-Object system.Object
        $CurrentItem | Add-Member -type Noteproperty -Name ID -Value $ID
        $CurrentItem | Add-Member -type Noteproperty -Name Classification -Value $Class
                
        $Falloutlist = $Falloutlist + $CurrentItem
        Write-Host "Failed to set" $CurrentItem.ID
        }    
}

$Falloutlist | Export-Csv C:\temp\falloutlist.csv -NoTypeInformation

Let me know any thoughts, and thanks in advance!

All replies (3)

Tuesday, July 10, 2012 5:06 PM ✅Answered

Try replacing these lines:

$Falloutlist = $Falloutlist + $CurrentItem   #with$Falloutlist.Add($CurrentItem.Name, $CurrentItem.Value)

Or something like that. Hash Tables use an "Add" method not an overloaded + or += operator for non-hashtable types. 

G. Samuel Hays


Tuesday, July 10, 2012 5:07 PM ✅Answered | 2 votes

I am a n00b when it comes to this, but from what I see is, $Falloutlist is a hash table, $CurrentItem is a Object, you are trying to add a Object to a hash table, which I don't think you can do. You can add Objects to arrays though.

And it looks like you are trying to use $Falloutlist as if it where an array.

Try changing $Falloutlist = @{} to $Falloutlist = @()


Tuesday, July 10, 2012 5:11 PM

Both good answers, thanks.  In particular, thanks for catching my typo.  I usually declare this sort of thing as an empty array using $list = @(), but this time around I accidentally declared it as a hash table when I didn't mean too using $list = @{}.  When I finish the last few touches, I'll post the script to the Technet PS Repository, Cheers!