首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ForEach在ForEach里面?

ForEach在ForEach里面?
EN

Stack Overflow用户
提问于 2020-03-05 05:40:14
回答 2查看 285关注 0票数 1

我试图为每个工作项类型和页面循环$uriProcess for $ProjectProcessConfiguration和$FieldControlConfiguration。

代码语言:javascript
复制
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$(PAT)")) }

$projectProcessConfiguration= @{ 
 "controlls"= "null"                        
  "id"= "null"
  "label"= "Company"
  "order"= "0"
  "overridden"= "null"
  "inherited"= "null"
  "visible"= "true"
}  | ConvertTo-Json -Depth 5

$FieldControlConfiguration = @{ 
  "id"= "Custom.ID"
  "label"= "ID"
  "controlType"= "FieldControl"
  "readOnly"= "true"
  "visible"= "true"
  "isContribution"= "false"
}  | ConvertTo-Json -Depth 5

$uriProcess= "https://dev.azure.com/$(Company)/_apis/work/processes/$(CMMI)/workItemTypes/CMMIinherited.Bug/layout/pages/CMMI.Bug.Bug/sections/Section3/groups?api-version=5.0-preview.1"
Invoke-RestMethod -uri $uriProcess -Method POST -Headers $AzureDevOpsAuthenicationHeader -Body $projectProcessConfiguration -ContentType "application/json"

因此,每个URL都将用于生成总共9个URL:

代码语言:javascript
复制
$Pages= "CMMI.Bug.Bug","CMMI.ChangeRequest.ChangeRequest","CMMI.Epic.Epic","CMMI.Feature.Feature","CMMI.Issue.Issue","CMMI.Requirement.Requirement","CMMI.Review.Review","CMMI.Risk.Risk","CMMI.Task.Task"

$WorkItems="CMMIinherited.Bug","CMMIinherited.ChangeRequest","CMMIinherited.Epic","CMMIinherited.Feature","CMMIinherited.Issue","CMMIinherited.Requirement","CMMIinherited.Review","CMMIinherited.Risk","CMMIinherited.Task"

只发送其中一个就像这样:

代码语言:javascript
复制
$uriProcess= "https://dev.azure.com/$(Company)/_apis/work/processes/$(CMMI)/workItemTypes/CMMIinherited.Bug/layout/pages/CMMI.Bug.Bug/sections/Section3/groups?api-version=5.0-preview.1"

下一个会是这样的:

代码语言:javascript
复制
$uriProcess= "https://dev.azure.com/$(Company)/_apis/work/processes/$(CMMI)/workItemTypes/CMMIinherited.ChangeRequest/layout/pages/CMMI.ChangeRequest.ChangeRequest/sections/Section3/groups?api-version=5.0-preview.1"

希望这是合理的。

我想要创建9个URL,每个URL都有自己的特殊值,并通过调用-Restmethod发送每个URL。

我想做一个前程循环,但是我的"$pages“和"$workItems”似乎没有排好队,所以我得到了不好的结果。听起来这不是个好办法。对于如何在一个简单的布局中做到这一点,有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-05 10:31:01

如果计划在其他地方使用数据,请不要使用写主机。

不需要写主机将输出发送到屏幕,这是PowerShell的默认设置。如果你绝对“想”使用写,无论出于什么原因,那么选择写输出代替。

您可以使用变量压缩将变量赋值并同时输出到屏幕。然而,只有在开发/调试会话中才真正这样做,或者如果您真的计划随时将此输出放到屏幕上的话。

显示你所期待的结果,而不是强迫人们去假设或猜测。什么错误正在发生?当我这样做的时候,用你的样本,结果是没有错误的,尽管有一些奇怪的格式。

代码语言:javascript
复制
# Simple string only needs single quotes. Double is for expansion needs and specific formatting 
$Org       = 'MyOrgName'
$CMMI      = 'MyCMMIThingy'
$Pages     = 'C.Bug','C.Change','C.Epic'
$WorkItems = 'Bug','Change','Epic'

ForEach ($WorkItemTypes in $WorkItems) # This will happen 3 times
{
    # Assign the results to the variable for later use, while outputting to the screen
    '*'*60
    "*** Processing worktypes named $WorkItemTypes ***"
    '*'*60
    ($uriProcess2 = "https://dev.azure.com/$Org/_apis/work/processes/$CMMI/workItemTypes/$WorkItemTypes/layout/pages/")

    ForEach ($PageTypes in $Pages) # This will happen 3 times for each of $WorkItems
    {
        # Assign the results to the variable for later use, while outputting to the screen
        '*'*60
        "*** Processing page types name $PageTypes  for worktypes named $WorkItemTypes for the Invoke command ***"
        '*'*60
        ($uriProcess3 = "$PageTypes/sections/Section3/groups?api-version=5.0-preview.1")
        <#
        Assign the results to the variable for later use, while outputting to the screen
        Using splatting for readability
        #>
        ($uriProcess = $uriProcess2 + $uriProcess3)
        <#
        $invokeRestMethodSplat = @{
            ContentType = 'application/json'
            Method = 'POST'
            Body = $projectProcessConfiguration
            Headers = $AzureDevOpsAuthenicationHeader
            Uri = $uriProcess
        }
        Invoke-RestMethod @invokeRestMethodSplat
        #>
    }
}

<#
# Results


************************************************************
*** Processing worktypes named Bug ***
************************************************************
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Bug/layout/pages/
************************************************************
*** Processing page types name C.Bug  for worktypes named Bug for the Invoke command ***
************************************************************
C.Bug/sections/Section3/groups?api-version=5.0-preview.1
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Bug/layout/pages/C.Bug/sections/Section3/groups?api-version=5.0-preview.1   
************************************************************
*** Processing page types name C.Change  for worktypes named Bug for the Invoke command ***
************************************************************
C.Change/sections/Section3/groups?api-version=5.0-preview.1
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Bug/layout/pages/C.Change/sections/Section3/groups?api-version=5.0-preview.1************************************************************
*** Processing page types name C.Epic  for worktypes named Bug for the Invoke command ***
************************************************************
C.Epic/sections/Section3/groups?api-version=5.0-preview.1
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Bug/layout/pages/C.Epic/sections/Section3/groups?api-version=5.0-preview.1  
************************************************************
*** Processing worktypes named Change ***
************************************************************
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Change/layout/pages/
************************************************************
*** Processing page types name C.Bug  for worktypes named Change for the Invoke command ***
************************************************************
C.Bug/sections/Section3/groups?api-version=5.0-preview.1
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Change/layout/pages/C.Bug/sections/Section3/groups?api-version=5.0-preview.1************************************************************
*** Processing page types name C.Change  for worktypes named Change for the Invoke command ***
************************************************************
C.Change/sections/Section3/groups?api-version=5.0-preview.1
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Change/layout/pages/C.Change/sections/Section3/groups?api-version=5.0-preview.1
************************************************************
*** Processing page types name C.Epic  for worktypes named Change for the Invoke command ***
************************************************************
C.Epic/sections/Section3/groups?api-version=5.0-preview.1
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Change/layout/pages/C.Epic/sections/Section3/groups?api-version=5.0-preview.1
************************************************************
*** Processing worktypes named Epic ***
************************************************************
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Epic/layout/pages/
************************************************************
*** Processing page types name C.Bug  for worktypes named Epic for the Invoke command ***
************************************************************
C.Bug/sections/Section3/groups?api-version=5.0-preview.1
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Epic/layout/pages/C.Bug/sections/Section3/groups?api-version=5.0-preview.1
************************************************************
*** Processing page types name C.Change  for worktypes named Epic for the Invoke command ***
************************************************************
C.Change/sections/Section3/groups?api-version=5.0-preview.1
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Epic/layout/pages/C.Change/sections/Section3/groups?api-version=5.0-preview.1
************************************************************
*** Processing page types name C.Epic  for worktypes named Epic for the Invoke command ***
************************************************************
C.Epic/sections/Section3/groups?api-version=5.0-preview.1
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Epic/layout/pages/C.Epic/sections/Section3/groups?api-version=5.0-preview.1
#>

您将获得每个条目的3个项,因为这是您通过嵌套循环要求它执行的。

每当您要调试迭代/循环时,始终使用尽可能少的代码,以确保您得到的是您想要的基本结果。

代码语言:javascript
复制
$Pages     = 'C.Bug','C.Change','C.Epic'
$WorkItems = 'Bug','Change','Epic'

ForEach ($WorkItemTypes in $WorkItems)
{
    " Work type is `"$WorkItemTypes`""       
    ForEach ($PageTypes in $Pages)
    {"Page type is $PageTypes and work type is `"$WorkItemTypes`""}
}

<#
# Results

 Work type is "Bug"
Page type is C.Bug and work type is "Bug"
Page type is C.Change and work type is "Bug"
Page type is C.Epic and work type is "Bug"
 Work type is "Change"
Page type is C.Bug and work type is "Change"
Page type is C.Change and work type is "Change"
Page type is C.Epic and work type is "Change"
 Work type is "Epic"
Page type is C.Bug and work type is "Epic"
Page type is C.Change and work type is "Epic"
Page type is C.Epic and work type is "Epic"
#>

更新

根据我对你的评论,采取这一行动是因为,正如你的评论中所写的,我不知道这是什么。

最终,我希望得到这样的输出:工作类型是"Bug“,页面类型是C.Bug,工作类型是"Bug”,工作类型是“C.Change”,工作类型是“C.Change”,工作类型是"Epic“,页面类型是C.Epic,工作类型是"Epic”

..。或者真正意味着用例的最终结果。但是,一个循环绝不会以这种方式处理,使用您提供的内容和方式。

即使把它分成几段,我现在也在挠头。

代码语言:javascript
复制
Work type is "Bug" Page type is C.Bug 
and 
work type is "Bug" 
Work type is "Change" Page type is C.Change 
and 
work type is "Change" 
Work type is "Epic" Page type is C.Epic 
and 
work type is "Epic"

您正在迭代一个列表,它只处理每一项一次。如果我用多维数组作为循环做了一部分,你就可以做到这一点。

代码语言:javascript
复制
Clear-Host
$Org       = 'MyOrgName'
$CMMI      = 'MyCMMIThingy'
$Counter   = -1
# Using multi-dimentional arrays
$WorkItems = @('Bug','Change','Epic'),@('C.Bug','C.Change','C.Epic')
$WorkItems[0] | 
ForEach{
    $Counter++
    ($uriProcess2 = "https://dev.azure.com/$Org/_apis/work/processes/$CMMI/workItemTypes/$($WorkItems[0][$Counter])/layout/pages/$($WorkItems[1][$Counter])")

}

<#
# Results

https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Bug/layout/pages/C.Bug
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Change/layout/pages/C.Change
https://dev.azure.com/MyOrgName/_apis/work/processes/MyCMMIThingy/workItemTypes/Epic/layout/pages/C.Epic
#>
票数 0
EN

Stack Overflow用户

发布于 2020-03-05 05:56:24

代码语言:javascript
复制
 $Pages= "C.Bug","C.Change","C.Epic"
    $WorkItems= "Bug","Change","Epic"

    ForEach ($WorkItemTypes in $WorkItems)
    {
        $uriProcess2= "https://dev.azure.com/`$Org/_apis/work/processes/`$CMMI/workItemTypes/$WorkItemTypes/layout/pages/"
        Write-Host $uriProcess2    
        ForEach ($PageTypes in $Pages)        {
            $uriProcess3= "$PageTypes/sections/Section3/groups?api-version=5.0-preview.1"
            Write-Host $uriProcess3
            $uriProcess= $uriProcess2 + $uriProcess3
            Write-Host $uriProcess 
            Invoke-RestMethod -uri $uriProcess -Method POST -Headers $AzureDevOpsAuthenicationHeader -Body $projectProcessConfiguration -ContentType "application/json"
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60538826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档