首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell批处理10次

Powershell批处理10次
EN

Stack Overflow用户
提问于 2019-11-07 12:50:41
回答 1查看 1.2K关注 0票数 1

我有下面的代码从合作伙伴中心获取数据

代码语言:javascript
复制
$custIDs = Get-MicrosoftPartner
foreach ($custID in $custIDs)

{
 Export-MicrosoftData -CustomerId $custID
 }

在这个foreach循环中,我有上千个CustomerID,所以我希望在第一个迭代中运行这个命令,在第一个迭代中获取前10个客户,接下来的10个等等。

我有自己想要的代码,但比一次循环所有客户时要慢得多(而且这个工作也很慢)。

代码语言:javascript
复制
$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)
        })

有没有更有效的方法来做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-07 15:46:14

[$i..(($i+= $group) - 1)]错了。这使得除了第一批之外,每个批处理的启动$i都太低,这意味着您所做的工作太多了。

你指的是[($i * $group)..(($i+1) * $group - 1)],也就是这样的东西

代码语言:javascript
复制
$batches = for ($i = 0; $i -lt $custIDs.Count / $group; $i++) {
    ,$custIDs[($i * $group)..(($i+1) * $group - 1)]
}

请注意开头的,,这确保您得到了一个嵌套数组。

话虽如此,我还是要重组Export-MicrosoftOrganizationData。主要是因为检索对象和导出对象是两件不同的事情,它们不应该在同一个函数中完成。

这使得一切变得更简单--不再需要beginend块,不需要中间数组,函数(让我们称之为Get-MicrosoftOrganizationData)总体上变得更加通用:

代码语言:javascript
复制
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调用还不够复杂,不足以隐藏到函数中:

代码语言:javascript
复制
$custIDs = Get-MicrosoftPartnerCustomers
$orgData = $custIDs | Get-MicrosoftOrganizationData
$orgData | Export-Csv -NoTypeInformation -Delimiter "," -Path .\TestOutputOrg.csv

从本质上说,你甚至可以把所有这些都放在一条线上。

总的来说,从输入中生成X批次可能对测试事物很有用,但是如果您无论如何要处理所有it,这不是一种有用的策略。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58749152

复制
相关文章

相似问题

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