0

I keep getting following error messages whenever I try to run my Powershell script:

Array assignment to [csr] failed: Cannot convert value "csr" to type "System.Int32". Error: "Input string was not in a correct format.".
At C:\fcheck\run-new.ps1:38 char:38
+         $Applications[$appname][$os][ <<<< $lob]=@()
    + CategoryInfo          : InvalidOperation: (System.Object[]:Object[]) [], RuntimeException
    + FullyQualifiedErrorId : ArrayAssignmentFailed

Array assignment to [csr] failed: Cannot convert value "csr" to type "System.Int32". Error: "Input string was not in a correct format.".
At C:\fcheck\run-new.ps1:42 char:38
+         $Applications[$appname][$os][ <<<< $lob]+=$location
    + CategoryInfo          : InvalidOperation: (c$\temp:String) [], RuntimeException
    + FullyQualifiedErrorId : ArrayAssignmentFailed

Array assignment to [csr] failed: Cannot convert value "csr" to type "System.Int32". Error: "Input string was not in a correct format.".
At C:\fcheck\run-new.ps1:38 char:38
+         $Applications[$appname][$os][ <<<< $lob]=@()
    + CategoryInfo          : InvalidOperation: (System.Object[]:Object[]) [], RuntimeException
    + FullyQualifiedErrorId : ArrayAssignmentFailed

Array assignment to [csr] failed: Cannot convert value "csr" to type "System.Int32". Error: "Input string was not in a correct format.".
At C:\fcheck\run-new.ps1:42 char:38
+         $Applications[$appname][$os][ <<<< $lob]+=$location
    + CategoryInfo          : InvalidOperation: (c$\temp:String) [], RuntimeException
    + FullyQualifiedErrorId : ArrayAssignmentFailed

It seems to be coming from the following lines

Import-CSV applications.csv | ForEach-Object {
    $appname = $_.appname.ToLower()
    $os = $_.os.ToLower()
    $lob = $_.lob.ToLower()
    $location = $_.location.ToLower()

    if ($Applications.Keys -notcontains $appname) {
        $WindowsOS=@{}
        $WindowsOS["windows xp"]=@()
        $WindowsOS["windows 7"]=@()
        $Applications[$appname]=$WindowsOS
    } 

    if ($Applications[$appname][$os].Keys -notcontains $lob) {
        $Applications[$appname][$os][$lob]=@()
    }

    if ($Applications[$appname][$os][$lob].Keys -notcontains $location) {
        $Applications[$appname][$os][$lob]+=$location
    }
}

Any idea how I can resolve it?

methuselah
  • 12,766
  • 47
  • 165
  • 315

1 Answers1

1

You declared your $WindowsOS elements to be arrays rather than hash tables. When you attempt to assign the $lob variable as a new key it is expecting an number since you declared them as an array. If you change your declarations to use hash tables instead you should be all set. See below.

if ($Applications.Keys -notcontains $appname) {
    $WindowsOS=@{}
    $WindowsOS["windows xp"]=@{}   # <-- Change to hash table notation as shown
    $WindowsOS["windows 7"]=@{}    # <-- Change to hash table notation as shown
    $Applications[$appname]=$WindowsOS
}