我有下面的代码从合作伙伴中心获取数据
$custIDs = Get-MicrosoftPartner
foreach ($custID in $custIDs)
{
Export-MicrosoftData -CustomerId $custID
}在这个foreach循环中,我有上千个CustomerID,所以我希望在第一个迭代中运行这个命令,在第一个迭代中获取前10个客户,接下来的10个等等。
我有自己想要的代码,但比一次循环所有客户时要慢得多(而且这个工作也很慢)。
$custIDs = Get-MicrosoftPartner
$group = 10 # step of 10
$i = 0
do {
$custIDs[$i..(($i+= $group) - 1)]
'*****'
Export-MicrosoftData -CustomerId $custIDs
}
until ($i -ge $custIDs.count -1)
function Export-MicrosoftData {
[CmdletBinding()]
param (
# Customer ID from Microsoft Partner Center
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$CustomerId
)
$CustomerId.ForEach({
$Organization = [myClass]::new()
$Organization.PopulateData($_)
$ArrayOfOrganizations.Add($Organization)
})有没有更有效的方法来做到这一点?
发布于 2019-11-07 15:46:14
[$i..(($i+= $group) - 1)]错了。这使得除了第一批之外,每个批处理的启动$i都太低,这意味着您所做的工作太多了。
你指的是[($i * $group)..(($i+1) * $group - 1)],也就是这样的东西
$batches = for ($i = 0; $i -lt $custIDs.Count / $group; $i++) {
,$custIDs[($i * $group)..(($i+1) * $group - 1)]
}请注意开头的,,这确保您得到了一个嵌套数组。
话虽如此,我还是要重组Export-MicrosoftOrganizationData。主要是因为检索对象和导出对象是两件不同的事情,它们不应该在同一个函数中完成。
这使得一切变得更简单--不再需要begin和end块,不需要中间数组,函数(让我们称之为Get-MicrosoftOrganizationData)总体上变得更加通用:
function Get-MicrosoftOrganizationData {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)] # CustomerId from Microsoft Partner Center
[string[]]$CustomerId
)
@($CustomerId) | ForEach-Object {
$Organization = [Organization]::new()
$Organization.PopulateMicrosoftData($_)
$Organization
}
}无论如何,Export-Csv调用还不够复杂,不足以隐藏到函数中:
$custIDs = Get-MicrosoftPartnerCustomers
$orgData = $custIDs | Get-MicrosoftOrganizationData
$orgData | Export-Csv -NoTypeInformation -Delimiter "," -Path .\TestOutputOrg.csv从本质上说,你甚至可以把所有这些都放在一条线上。
总的来说,从输入中生成X批次可能对测试事物很有用,但是如果您无论如何要处理所有it,这不是一种有用的策略。
https://stackoverflow.com/questions/58749152
复制相似问题