You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将查询软件许可状态的PowerShell脚本导出为CSV并从AD取计算机名?

PowerShell软件许可查询脚本常见问题解答

1. 如何将脚本执行结果导出为CSV文件?

直接在脚本末尾追加 Export-Csv 命令即可,推荐加上 -NoTypeInformation 参数去掉默认的类型信息行,避免CSV文件冗余。如果结果包含中文,建议加上 -Encoding UTF8 防止乱码。修改后的完整脚本如下:

Get-CimInstance -ClassName SoftwareLicensingProduct -computerName test,test2,test3,test |
     where PartialProductKey | select Pscomputername,Name,@{Name='LicenseStatus';Expression={
        switch ($_.LicenseStatus)
        {
            0 {'Unlicensed'}
            1 {'licensed'}
            2 {'OOBGrace'}
            3 {'OOTGrace'}
            4 {'NonGenuineGrace'}
            5 {'Notification'}
            6 {'ExtendedGrace'}
            Default {'Undetected'}
        }
    }} | Export-Csv -Path "C:\LicenseStatus.csv" -NoTypeInformation -Encoding UTF8
  • 替换C:\LicenseStatus.csv为你实际需要保存的路径和文件名即可

2. 是否可以从Active Directory(AD)中获取计算机名作为-computerName参数的输入值?

可以,但前提是你的PowerShell环境已安装Active Directory模块(通常需安装RSAT工具包)。

用Get-ADComputer命令可获取AD中的计算机列表,筛选启用状态的计算机后,提取Name属性作为目标主机列表。修改后的脚本示例:

# 获取AD中所有启用的计算机名
$computers = Get-ADComputer -Filter {Enabled -eq $true} | Select-Object -ExpandProperty Name

# 执行许可查询并导出结果
Get-CimInstance -ClassName SoftwareLicensingProduct -computerName $computers |
     where PartialProductKey | select Pscomputername,Name,@{Name='LicenseStatus';Expression={
        switch ($_.LicenseStatus)
        {
            0 {'Unlicensed'}
            1 {'licensed'}
            2 {'OOBGrace'}
            3 {'OOTGrace'}
            4 {'NonGenuineGrace'}
            5 {'Notification'}
            6 {'ExtendedGrace'}
            Default {'Undetected'}
        }
    }} | Export-Csv -Path "C:\ADComputerLicenseStatus.csv" -NoTypeInformation -Encoding UTF8
  • 若需筛选特定OU下的计算机,可给Get-ADComputer添加-SearchBase参数,例如-SearchBase "OU=Servers,DC=domain,DC=com"
  • 注意:执行脚本的账户需具备读取AD计算机对象的权限,且目标计算机需在线并允许CIM访问

内容的提问来源于stack exchange,提问作者Kourosh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 03:25:00