【问题标题】:Powershell get spare licensesPowershell 获得备用许可证
【发布时间】:2022-01-21 12:58:17
【问题描述】:

我正在尝试从Get-MsolAccountSku 命令获取可用的备用许可证数量。

这是代码(下面的输出)

$Licenses = Get-MsolAccountSku
$spare = Foreach ($License in $licenses)
{
  ($License.ActiveUnits - $License.ConsumedUnits)   
}

Get-MsolAccountSku | Select-Object -Property AccountSkuId,ActiveUnits,ConsumedUnits,@{L=’SpareLicenses’;E={$spare}}

我想在输出右侧添加一列,列出ForEach 循环中的减法可用的许可证数量。

ActiveUnits ConsumedUnits
----------- -------------
         30            26
       1601             1
         30            29
         25             0
          5             3
          1             0
      12550         12465
    1000000         12461
      12550         12466
      12555         12468
         31            19
      12550         12464

【问题讨论】:

    标签: powershell ms-office azure-powershell ms-office-script


    【解决方案1】:

    不需要您的$spare 对象,只需更新 Select 以进行计算...

    Get-MsolAccountSku | 
    Select-Object -Property AccountSkuId,ActiveUnits,ConsumedUnits,
    @{L=’SpareLicenses’;E={$_.ActiveUnits - $_.ConsumedUnits}}
    

    【讨论】:

      【解决方案2】:

      来自about Calculated Properties

      计算的属性由包含指定新属性名称的键值对、计算值的表达式和可选格式信息的哈希表定义。

      • expression - 用于计算新属性值的脚本块

      按照您的代码,您应该为脚本块更改 foreach 循环:

      $Licenses = Get-MsolAccountSku
      $spare = { $_.ActiveUnits - $_.ConsumedUnits }
      
      # $_ - References to each object being passed through the pipeline
      # See about Automatic Variables for more information
      
      Get-MsolAccountSku |
      Select-Object -Property AccountSkuId,ActiveUnits,ConsumedUnits,@{
          Name = 'SpareLicenses'
          Expression = $spare
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-19
        • 2017-05-17
        • 1970-01-01
        • 1970-01-01
        • 2016-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多