将数组动态辅助到哈希中的键时出现错误

时间:2019-05-21 10:26:47

标签: powershell

我有一个第三方功能,可以为服务器创建一个配置文件。当我创建一个数组并将其分配给第三方函数所需的哈希时,它可以正常工作,但是当我动态创建一个数组并对其进行分配时,却出现了错误。

曾尝试使用具有所有值的简单变量,并已静态创建并排列这些值。但是当我动态创建时,它会失败。

Function New-Disk
{
    Param (
        [parameter(Mandatory = $false)]
        [Array] $XXX_drivedata
    )
    if ($XXX_drivedata[3] -ieq "yes")
    {
        $boot_data = $TRUE;
    }
    else
    {
        $boot_data = $FALSE;
    }
    if ($XXX_drivedata[9] -ieq "yes")
    {
        $erase_data = $TRUE;
    }
    else
    {
        $erase_data = $FALSE;
    }
    $params1 = @{
        name = $XXX_drivedata[0];
        RAID = $XXX_drivedata[1];
        numberofDrives = $XXX_drivedata[2];
        driveType = $XXX_drivedata[5];
        driveSelectionBy = $XXX_drivedata[6];
        minDriveSize = $XXX_drivedata[7];
        maxDriveSize = $XXX_drivedata[8];
        eraseDataOnDelete = $erase_data;
        bootable = $boot_data;
        accelerator = $XXX_drivedata[4];
        storageLocation = $XXX_drivedata[10]
    }


$params = $params1.Clone()
    foreach($item in $params1.GetEnumerator())
    {
        #if ([string]::IsNullOrWhiteSpace($item.Value) -or ($item.Value -ieq "null"))
        if (!$item.value)
        {
            $params.Remove($item.Key)
        }
    }
    try {
        $logical_disk_create = New-<Function for disk> @params
        if ($logical_disk_create)
        {
            $XXX_disk_create_status = "pass"
            return $SCID_disk_create_status,$logical_disk_create.SasLogicalJBOD
        }
    }
    catch {
        Write-Error $_
        $XXX_disk_create_status = "fail"
        return $XXX_disk_create_status,$logical_disk_create
        continue
    }
}

#------------------------------------------------
#Attach local disk and JBOD to controller
#------------------------------------------------

Function New-Controller
{
    Param (
        [parameter(Mandatory = $true)]
        [Array] $SCID_controller_detail,

        [parameter(Mandatory = $true)]
        [Array] $SCID_logicaldisk_detail
    )
    if ($SCID_controller_detail[1] -ieq "yes")
    {
        $initialize_data = $TRUE;
    }
    else
    {
        $initialize_data = $FALSE;
    }
    $params1 = @{controllerID = $XXX_controller_detail[0];initialize = $initialize_data;writeCache = $XXX_controller_detail[2];logicalDisk = $XXX_logicaldisk_detail}

    $params = $params1.Clone()
    foreach($item in $params1.GetEnumerator())
    {
        if ($item.key -ne "logicalDisk")
        {
            #if ([string]::IsNullOrWhiteSpace($item.Value))
            if (!$item.value)
            {
                $params.Remove($item.Key)
            }
        }
    }
    try {

        $logicaldisk_controller_create = New-<Function for disk controller> @params
        if ($logicaldisk_controller_create)
        {
            $SCID_disk_create_status = "pass"
            return $SCID_disk_create_status,$logicaldisk_controller_create
        }
    }
    catch {
        Write-Error $_
        $SCID_disk_create_status = "fail"
        return $SCID_disk_create_status,$logicaldisk_controller_create
        continue
    }
}

#--------------------------------------------------
#Create Server Profile
#--------------------------------------------------

Function New-ServerProfile
{

        .......

    #------------------------------------------------------
    #Read local disk and JBOD details
    #------------------------------------------------------

    $SP_logical_disk_list = @()
    $SP_logical_disk_list_controller = @()
    $XXX_controllerdata = @("$($serverprofile.localStorages.integratedStorageController.controllerID)", "$($serverprofile.localStorages.integratedStorageController.reinitialize)", "$($serverprofile.localStorages.integratedStorageController.writeCache)")

    if ($serverprofile.localStorages.integratedStorageController.logicalDrive)
    {
        foreach ($logicaldrive in $($serverprofile.localStorages.integratedStorageController.logicalDrive))
        {
            $XXX_drivedata = @("$($logicaldrive.name)", "$($logicaldrive.raidLevel)", "$($logicaldrive.physicalDrives)", "$($logicaldrive.boot)", "$($logicaldrive.accelarator)", "$($logicaldrive.driveTechnology)")
            $logicaldisk_create = New-Disk -XXX_drivedata $XXX_drivedata
            if ($logicaldisk_create[0] -ne "fail")
            {
                $SP_logical_disk_list += $logicaldisk_create[1]
                $XXX_drivedata.Clear()
            }
        }
        $logdisk_controller = New-Controller -XXX_controller_detail $SCID_controllerdata -XXX_logicaldisk_detail $SP_logical_disk_list
        if ($logdisk_controller[0] -ne "fail")
        {
            $SP_logical_disk_list_controller += $logdisk_controller[1]
        }
    }

...........................

$LogicalDisk = New-<Fuctionfordisk> -Name "MyDisk" | New-<Function for disk controller> -Initialize
    $LogicalDisks = New-<Fuctionfordisk> -Name "MyDisk" | New-<Function for disk controller> -Initialize
    $logcaldr = @($LogicalDisk, $LogicalDisks)

$params1 = @{
        ....................
                other parameters

        logicalDisk = $SP_logical_disk_list_controller
    }
    $params = $params1.Clone()
    foreach($item in $params1.GetEnumerator())
    {
        if ($item.key -ne "LogicalDisk|localStorage")
        {
            #if ([string]::IsNullOrWhiteSpace($item.Value) -or ($item.Value -ieq "null"))
            if (!$item.value)
            {
                $params.Remove($item.Key)
            }
        }
    }
    $task = New-<Server Profile> @params | Wait-<task>

当我使用localdr时,它工作正常,但是当我使用$SP_logical_disk_list_controller进行存储时,出现错误

  

请求中发送的JSON包含一个未知类型,其中在第746列附近的第1行需要使用其他未知类型。更正JSON的内容,然后重试该请求。

我什至尝试使用$logdisk_controller[1],但仍然出现相同的错误。

0 个答案:

没有答案