简体   繁体   English

PowerShell脚本中的嵌套循环

[英]Nested Loop in PowerShell Script

I have a program that creates the XML output file as below: 我有一个创建XML输出文件的程序,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<DvMCLIResult version="8.5.1-00">
<CommandResult command="GetStorageArray">
<StorageArray objectID="ARRAY.R800.57457" name="SU55CYBSVSPG02" description="VSP G1000 (57457) at SU55CYBSVSPG02" serialNumber="57457" arrayFamily="R800" arrayType="R800" microcodeVersion="80-05-45/00" agentVersion="09_03_02" productName="RAID800" controllerVersion="80-05-45-00/00" numberOfControllers="2" capacityInGB="1457209" cacheInMB="480512" sharedMemoryInMB="-1" numberOfSpareDrives="-1" freeCapacityInGB="518433" allocatedCapacityInGB="938775" hihsmCapacityInGB="0" onDemandCapacityInGB="0" totalFreeSpaceInGB="160563" largestFreeSpaceInGB="160563" capacityInKB="1527994394624" freeCapacityInKB="543617255424" allocatedCapacityInKB="984377139200" hihsmCapacityInKB="0" onDemandCapacityInKB="0" totalFreeSpaceInKB="168362734592" largestFreeSpaceInKB="168362657792" multipathSupport="1" securityStatus="2" sequenceNumber="57457" displayArrayFamily="VSP G1500" displayArrayType="VSP G1500" numberOfLUs="520" numberOfAllocatedLUs="340" numberOfUnallocatedLUs="37" slprStatus="-1" openTotalCapacity="1527994394624" openAllocatedCapacity="984377139200" openFreeCapacity="543617255424" openHiHsmCapacity="0" openOnDemandCapacity="0" imTotalCapacity="0" imAllocatedCapacity="0" imFreeCapacity="0" imHiHsmCapacity="0" imOnDemandCapacity="0" mfTotalCapacity="0" mfHiHsmCapacity="0" mfOnDemandCapacity="0" mfAllocatedCapacity="0" mfUnallocatedCapacity="0" numberOfOpenAllocatedLUs="340" numberOfOpenUnallocatedLUs="37" numberOfImAllocatedLUs="0" numberOfImUnallocatedLUs="0" numberOfMfLDEVs="0" numberOfAllocatedMfLDEVs="0" numberOfUnallocatedMfLDEVs="0" productCode="1" lastRefreshed="1509390388" autoFormatLU="0" statusOfDBInconsistency="0" configUpdateStatus="0" openAllocatedActualCapacity="0" openUnallocatedCapacity="117351657984" openUnallocatedActualCapacity="117351657984" openReservedCapacity="426265597440" openReservedActualCapacity="263305352704" numberOfReservedLUs="143" numberOfOpenReservedLUs="143" numberOfImReservedLUs="0" distributedMode="-1" clprNumber="0;1" >
<Pool objectID="JOURNALPOOL.R800.57457.5.14" name="ANET" poolFunction="5" poolID="14" encrypted="-1" controllerID="-1" poolType="32" status="0" threshold="70" threshold2="80" threshold2Mode="1" capacityInKB="118497146880" freeCapacityInKB="39398381568" usageRate="66" numberOfPoolVols="39" numberOfVVols="118" capacityOfVVolsInKB="176616050688" clprNumber="-1" raidLevel="-" combination="" volAlertNotice="-1" overProvisioningPercent="149" overProvisioningWarning="-1" overProvisioningLimit="-1" rpm="-1" diskType="-1" tierControl="enable" autoMigration="enable" migrationInterval="4" monitorStartTime="00:00" monitorEndTime="23:59" monitoringMode="1" externalMixCompatible="enable" monitorSideState="1" monitoringState="1" lastMonitorStartDate="2017/10/30 16:00:00" lastMonitorEndDate="-1" relocationState="0" relocationProgress="-1" relocationSpeed="3" numberOfTiers="2" thresholdRepDepAlert="-1" thresholdRepDataReleased="-1" userUsedCapacityInKB="-1" repDataCapacityInKB="-1" repManageCapacityInKB="-1" numberOfDpVols="-1" capacityOfDpVolsInKB="-1" protectDpVolOnFullPool="1" protectDpVolOnBlockedPoolVol="1" numberOfSnapshotPairs="-1" reservedCapacityInKB="0" realTimeRelocation="enable" basicUsableCapacityTotal="112028571648" basicUsableCapacityUsed="72630190080" basicUsableFMCCapacityTotal="41105799168" basicUsableFMCCapacityUsed="23335908303" fmcCapacityTotal="47574374400" fmcCapacityUsed="37378518576" deduplication="disable" >
<PoolTier objectID="JOURNALPOOLTIER.R800.57457.5.14.0" tierID="0" capacityInKB="47574374400" freeCapacityInKB="11080452096" usageRate="76" raidLevel="RAID5" combination="3D+1P" rpm="0" diskType="5" substance="0" bufSpaceForNewPageAssignment="0" bufSpaceForTierRelocation="2" />
<PoolTier objectID="JOURNALPOOLTIER.R800.57457.5.14.1" tierID="1" capacityInKB="70922772480" freeCapacityInKB="28317929472" usageRate="60" raidLevel="RAID6" combination="14D+2P" rpm="10000" diskType="4" substance="0" bufSpaceForNewPageAssignment="8" bufSpaceForTierRelocation="2" />
</Pool>
<Pool objectID="JOURNALPOOL.R800.57457.5.21" name="Virtual" poolFunction="5" poolID="21" encrypted="-1" controllerID="-1" poolType="32" status="0" threshold="90" threshold2="95" threshold2Mode="1" capacityInKB="284113428480" freeCapacityInKB="168230479872" usageRate="40" numberOfPoolVols="96" numberOfVVols="222" capacityOfVVolsInKB="807829327872" clprNumber="0" raidLevel="-" combination="" volAlertNotice="-1" overProvisioningPercent="284" overProvisioningWarning="450" overProvisioningLimit="500" rpm="-1" diskType="-1" tierControl="enable" autoMigration="enable" migrationInterval="4" monitorStartTime="00:00" monitorEndTime="23:59" monitoringMode="1" externalMixCompatible="enable" monitorSideState="1" monitoringState="1" lastMonitorStartDate="2017/11/06 16:00:00" lastMonitorEndDate="-1" relocationState="0" relocationProgress="-1" relocationSpeed="3" numberOfTiers="2" thresholdRepDepAlert="-1" thresholdRepDataReleased="-1" userUsedCapacityInKB="-1" repDataCapacityInKB="-1" repManageCapacityInKB="-1" numberOfDpVols="-1" capacityOfDpVolsInKB="-1" protectDpVolOnFullPool="1" protectDpVolOnBlockedPoolVol="1" numberOfSnapshotPairs="-1" reservedCapacityInKB="0" realTimeRelocation="enable" basicUsableCapacityTotal="284113428480" basicUsableCapacityUsed="115882948608" basicUsableFMCCapacityTotal="24051793920" basicUsableFMCCapacityUsed="14543913080" fmcCapacityTotal="24051793920" fmcCapacityUsed="23561653920" deduplication="disable" >
<PoolTier objectID="JOURNALPOOLTIER.R800.57457.5.21.0" tierID="0" capacityInKB="24051793920" freeCapacityInKB="481001472" usageRate="98" raidLevel="RAID5" combination="7D+1P" rpm="0" diskType="5" substance="0" bufSpaceForNewPageAssignment="0" bufSpaceForTierRelocation="2" />
<PoolTier objectID="JOURNALPOOLTIER.R800.57457.5.21.1" tierID="1" capacityInKB="260061634560" freeCapacityInKB="167749478400" usageRate="35" raidLevel="RAID6" combination="14D+2P" rpm="10000" diskType="4" substance="0" bufSpaceForNewPageAssignment="2" bufSpaceForTierRelocation="2" />
</Pool>
</StorageArray>
</CommandResult>
</DvMCLIResult>

How Do I extract the output as follows: 如何提取输出,如下所示:

#<Storage Array>
#(for Pool 1)
StorageArray.Name Pool.ObjectID PoolTier.objectID #(For PoolTier 1)
StorageArray.Name Pool.ObjectID PoolTier.objectID #(For PoolTier 2)

#(for Pool 2)
StorageArray.Name Pool.ObjectID PoolTier.objectID #(For PoolTier 1)
StorageArray.Name Pool.ObjectID PoolTier.objectID #(For PoolTier 2)

My script is as follows: 我的脚本如下:

[xml]$file = Get-Content -Path F:\Manu\output.xml

foreach ($StorageArray in $file.DvMCLIResult.CommandResult.StorageArray) {
    Write-Host $StorageArray.name
    foreach ($Pool in $file.DvMCLIResult.CommandResult.StorageArray.Pool) {
        foreach( $PoolTier in $file.DvMCLIResult.CommandResult.StorageArray.Pool.PoolTier) {
            Write-Host $file.DvMCLIResult.CommandResult.StorageArray.name $PoolTier.objectID $PoolTier.tierID
        }
    }
}

However, it outputs everything (pooltier) twice while I need details (pooltier) per 'Pool'. 但是,它两次输出所有内容(池层),而每个“池”需要我提供详细信息(池层)。

SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.14.0 0 
SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.14.1 1 
SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.21.0 0 
SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.21.1 1 

SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.14.0 0 
SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.14.1 1 
SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.21.0 0 
SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.21.1 1

Required output: 要求的输出:

SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.14.0 0 
SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.14.1 1 

SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.21.0 0 
SU55CYBSVSPG01 JOURNALPOOLTIER.R800.57434.5.21.1 1

You get duplicate results because you work from $file in every nested loop. 您会得到重复的结果,因为在每个嵌套循环中都使用$file进行工作。 Start from the loop variable of the respective parent loop: 从相应父循环的循环变量开始:

foreach ($StorageArray in $file.DvMCLIResult.CommandResult.StorageArray) {
    foreach ($Pool in $StorageArray.Pool) {
        foreach ($PoolTier in $Pool.PoolTier) {
            Write-Host $StorageArray.name $PoolTier.objectID $PoolTier.tierID
        }
    }
}

You're using nested loops on the entire file. 您正在整个文件上使用嵌套循环。 Inside the outermost loop you already have $StorageArray , that's what you should be looping on. 在最外层的循环中,您已经有$StorageArray ,这就是您应该循环的内容。 And inside the next loop, you should be looping on $Pool . 在下一个循环中,您应该在$Pool上循环。

Something like this: 像这样:

[xml]$file= Get-Content -Path F:\Manu\output.xml

foreach( $StorageArray in $file.DvMCLIResult.CommandResult.StorageArray)
{
  Write-Host $StorageArray.name
  foreach( $Pool in $StorageArray.Pool)
  {
    foreach( $PoolTier in $Pool.PoolTier)
    {
       write-Host $StorageArray.name $PoolTier.objectID $PoolTier.tierID 
    }        
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM