Powershell批量返回结果
Powershell Returning Results in Batches
我有下面的 office365 命令,如果可能的话,我可以用它来做。
$GetUsersIDs = Get-MsolUser | Where-Object {($_.licenses).AccountSkuId -match "STANDARDWOFFPACK_STUDENT"} | Select -ExpandProperty UserPrincipalName
Grant-CsUserPolicyPackage -identity $GetUsersIDs -PackageName "Education_PrimaryStudent_RemoteLearning"
我想要它 return 前 10 个结果和 运行 一个命令然后 return 接下来的 10 个结果和 运行 一个命令等等。这背后的原因是由于 Microsoft 的限制只允许您 运行 批量执行命令 X.
您可以使用递增 10 的 for
循环来执行此操作。由于您的用户列表将是一个集合,因此您可以结合使用索引和范围运算符 ..
来输出批次。
$GetUsersIDs = Get-MsolUser | Where-Object {($_.licenses).AccountSkuId -match "STANDARDWOFFPACK_STUDENT"} |
Select -ExpandProperty UserPrincipalName
for ($i = 0; $i -lt $GetUsersIDs.Count; $i += 10) {
Grant-CsUserPolicyPackage -Identity $GetUsersIDs[$i..($i+9)] -PackageName "Education_PrimaryStudent_RemoteLearning"
}
请注意,Grant-CsUserPolicyPackage 命令允许以 20 个为一组进行更新。
我有下面的 office365 命令,如果可能的话,我可以用它来做。
$GetUsersIDs = Get-MsolUser | Where-Object {($_.licenses).AccountSkuId -match "STANDARDWOFFPACK_STUDENT"} | Select -ExpandProperty UserPrincipalName
Grant-CsUserPolicyPackage -identity $GetUsersIDs -PackageName "Education_PrimaryStudent_RemoteLearning"
我想要它 return 前 10 个结果和 运行 一个命令然后 return 接下来的 10 个结果和 运行 一个命令等等。这背后的原因是由于 Microsoft 的限制只允许您 运行 批量执行命令 X.
您可以使用递增 10 的 for
循环来执行此操作。由于您的用户列表将是一个集合,因此您可以结合使用索引和范围运算符 ..
来输出批次。
$GetUsersIDs = Get-MsolUser | Where-Object {($_.licenses).AccountSkuId -match "STANDARDWOFFPACK_STUDENT"} |
Select -ExpandProperty UserPrincipalName
for ($i = 0; $i -lt $GetUsersIDs.Count; $i += 10) {
Grant-CsUserPolicyPackage -Identity $GetUsersIDs[$i..($i+9)] -PackageName "Education_PrimaryStudent_RemoteLearning"
}
请注意,Grant-CsUserPolicyPackage 命令允许以 20 个为一组进行更新。