使用arraylist时如何在powershell中显示对象
使用arraylist时如何在powershell中显示对象,powershell,powershell-2.0,powershell-3.0,azure-powershell,powershell-4.0,Powershell,Powershell 2.0,Powershell 3.0,Azure Powershell,Powershell 4.0,我想将所有分支名称显示为字符串,而不是对象的形式,即{,…,…}如果这只是为了显示格式,即隐式表格格式的显示输出中显示的内容,您可以显式调用并使用a将自定义格式应用于分支名称列: $result = New-Object System.Collections.ArrayList ForEach ($repoElement in $Repo.value) { $repoId = $repoElement.id $BranchCreatorUrl = "https://dev.azur
我想将所有分支名称显示为字符串,而不是对象的形式,即{,…,…}如果这只是为了显示格式,即隐式表格格式的显示输出中显示的内容,您可以显式调用并使用a将自定义格式应用于
分支名称
列:
$result = New-Object System.Collections.ArrayList
ForEach ($repoElement in $Repo.value)
{
$repoId = $repoElement.id
$BranchCreatorUrl = "https://dev.azure.com/xyz/_apis/git/repositories/$repoId/refs?api-version=6.1-preview.1"
$CreateorInfo = (Invoke-RestMethod -Uri $BranchCreatorUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$url1= "https://dev.azure.com/xyz/_apis/policy/configurations?api-version=4.1"
$response = (Invoke-RestMethod -Uri $url1 -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$obj=[pscustomobject]@{
RepositoryName = $repoElement.name
RepositoryId = $repoId
BranchName = $CreateorInfo.value.name
PolicyName = $response.value.type.displayname
}
$result += $obj
}
Write-Output $result
The above code gives an output
上面生成了一个表格显示,其BranchName
列没有封闭的{…}
,PowerShell使用它来指示列值是一个数组(可能令人困惑,因为它看起来像一个脚本块):
$result.BranchName
查看每个对象的值<代码>$result[0]。BranchName查看第一个对象的值<代码>$result[0]。BranchName-join','以将第一个对象的值视为逗号分隔的字符串,而不是集合。这都是假设您没有像标记的那样实际使用PowerShell v2。我想将分支对象中的所有名称显示为所有存储库(行)@AdminOfThings$result.foreach({$\ BranchName-join',})的逗号分隔字符串
要做到这一点,您可能需要将BranchName=$CreateorInfo.value.name替换为BranchName=$($CreateorInfo.value.name-join“,”),它可以工作。谢谢你的帮助,很有效。谢谢你的帮助。很高兴听到这个消息,@SudarshanSharma;我的荣幸。
# Sample result.
$result = [pscustomobject]@{
RepositoryName = 'name'
RepositoryId = 'id'
BranchName = 'branch1', 'branch2', 'branch3'
PolicyName = 'policy'
}
# Use explicit Format-Table formatting with custom formatting of the
# 'BranchName' column.
$result | Format-Table RepositoryName,
RepositoryId,
@{ n='BranchName'; e={ $_.BranchName -join ', ' } },
PolicyName
RepositoryName RepositoryId BranchName PolicyName
-------------- ------------ ---------- ----------
name id branch1, branch2, branch3 policy