【问题标题】:Powershell - Export csv for program name, version, installdatePowershell - 导出 csv 以获取程序名称、版本、安装日期
【发布时间】:2021-04-02 12:01:50
【问题描述】:

我正在尝试对计算机列表运行命令以查找有关计算机上程序的信息。然后我想以以下格式在 CSV 上导出该信息:

计算机名称、McAfee Agent、版本、安装日期

我想获取这些计算机的列表以及程序版本号和安装日期。

此命令有效,但不适用于导出:

Get-WmiObject win32_product -ComputerName SYSTEMName01 | Where name -Match "McAfee Agent" | Select Name, Version, Installdate

我在这里尝试使用以下代码,但这不起作用,因为它会在 Powershell 中填充信息,然后给我一个:

  • “RPC 服务器不可用”红色文本。
  • 并且导出的 CSV 文档只给了我一个填充行的程序名称及其版本号和安装日期。但它不会填充它查看的计算机名称,也不会在 .txt 文档中列出其他计算机。
$computers = Get-content -Path "C:\nice\List-of-systems.txt"
Get-WmiObject win32_product -ComputerName $computers | Where name -Match "McAfee Agent" | Select Name, Version, Installdate |
Export-Csv -Path "C:\nice\computers-mcafee-status.csv"

【问题讨论】:

  • 什么是不起作用?你的C:\nice\List-of-systems.txt 是什么样的?
  • 它看起来像在另一个下面列出一个我可以ping通的网络上的系统名称。没有 IP 地址还是您想要图片参考?
  • 你没有导出计算机名,你需要-Append到文件中。
  • 目前您的脚本将在第一个错误处停止 - 如果您希望它检查所有计算机,您需要编写它以解决连接中的任何错误。
  • 这是一个您最好使用foreach ($system in $computers) {Get-WMIObject ... -ErrorAction Ignore} 处理计算机列表的地方 - 这样做可以让您获得所有可访问的计算机,同时跳过那些无法访问的计算机响应 WMI 调用。

标签: powershell csv


【解决方案1】:

如果你要苦于只查询单个信息,不妨用它查询系统信息。

<#
SYNTAX
    Get-SystemInfo [[-ComputerName] <string[]>]  [<CommonParameters>]

EXAMPLE
    Get-SystemInfo -ComputerName SystemOne, SystemTwo
    'SystemOne', 'SystemTwo' | Get-SystemInfo
    Get-content -Path "C:\nice\List-of-systems.txt" | Get-SystemInfo
    Get-content -Path "C:\nice\List-of-systems.txt" | Get-SystemInfo | Export-CSV C:\List-Of-Systems.csv -NoTypeInformation
#>
Function Get-SystemInfo{
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$False,
               ValueFromPipeLine=$True,
               ValueFromPipelineByPropertyName=$true,
               HelpMessage='Enter Computer Name')]
               [Alias('CN','Name')]
               [String[]]$ComputerName = $env:COMPUTERNAME)
Process{
    try{
        #$ExportPath = "C:\nice\computers-mcafee-status.csv"
        #if(-Not(Test-Path -Path '$ExportPath')){
            #New-Item '$ExportPath' -ItemType File }           
            foreach($Computer in $ComputerName){
                $CIMSession = New-CimSession -ComputerName $Computer -ErrorAction Stop
                #$OS        = Get-CimInstance -CimSession $CIMSession -Namespace Root/CIMv2 -ClassName WIN32_OperatingSystem -ErrorAction SilentlyContinue
                $CS         = Get-CimInstance -CimSession $CIMSession -Namespace Root/CIMv2 -ClassName WIN32_ComputerSystem -Property Model,Manufacturer,Username -ErrorAction SilentlyContinue
                $BS         = Get-CimInstance -CimSession $CIMSession -Namespace Root/CIMv2 -ClassName WIN32_BIOS -ErrorAction SilentlyContinue
                $MAC        = Get-CimInstance -CimSession $CIMSession -Namespace Root/CIMv2 -ClassName WIN32_NetworkAdapterConfiguration | Where-Object -Property Description -Like "*Ethernet*" 
                $MA         = Get-CimInstance -CimSession $CIMSession -Namespace Root/CIMv2 -ClassName WIN32_Product | Where-Object Name -Match "McAfee Agent" -ErrorAction SilentlyContinue    

                    [PSCustomobject] @{
                        "Computer Name"         = $Computer
                        "Status"                = "Online"
                        "Manufacturer"          = if($CS.Manufacturer){$CS.Manufacturer}Else{"Unable to get Manufacturer"}
                        "Model"                 = if($CS.Model){$CS.Model}Else{"Unable to get Model"}
                        "User Logged in"        = if($cs.UserName){$cs.UserName}Else{"No user logged in"}
                        "Serial Number"         = if($BS.SerialNumber){$BS.SerialNumber}Else{"Unable to get Serial #"}
                        "MAC Address"           = if($MAC.MACAddress){$MAC.MACAddress}Else{"Unable to get MAC Address"}
                        "McAfee InstallDate"    = if($MA.InstallDate){$MA.InstallDate}Else{"Unable to get InstallDate"}
                        "McAfee Version"        = if($Ma.Version){$MA.Version}Else{"Unable to get Version"}

                        } #| Export-Csv -Path '$ExportPath' -Append -NoTypeInformation
                    }
                } Catch [Microsoft.Management.Infrastructure.CimException] {
                     [PSCustomobject] @{
                        "Computer Name"      = $Computer
                        "Status"             = "Offline"
                        "Manufacturer"       = $null
                        "Model"              = $null
                        "User Logged in"     = $null
                        "Serial Number"      = $null
                        "MAC Address"        = $null
                        "McAfee InstallDate" = $null
                        "McAfee Version"     = $null 
                    } #| Export-Csv -Path '$ExportPath' -Append -NoTypeInformation
                } Finally {
                    if($CIMSession){
                      Get-CimSession | Remove-CimSession
                    } 
                }
            }
        }

您尝试做的问题是,您没有将要导出的计算机名与其他信息一起包含在内。您还需要-Append 到该文件。您收到的错误意味着您无法连接到远程计算机。

【讨论】:

  • 这太棒了!我从中学到了很多,非常感谢!
  • 希望它能满足您的需求。周末愉快!
  • @JeffreyRodriguez 如果您觉得亚伯拉罕的回答解决了您的问题,您应该点击左侧的大勾号图标✓ 接受它。见How to accept SO answers。这将帮助其他有类似问题的人更轻松地找到它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
相关资源
最近更新 更多