0

每当我尝试运行我的 Powershell 脚本时,我都会收到以下错误消息:

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

它似乎来自以下几行

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
    }
}

知道如何解决吗?

4

1 回答 1

1

您将 $WindowsOS 元素声明为数组而不是哈希表。当您尝试将 $lob 变量分配为新键时,它需要一个数字,因为您将它们声明为数组。如果您将声明更改为使用哈希表,那么您应该一切就绪。见下文。

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
} 
于 2013-10-02T10:49:09.967 回答