[英]Powershell - Export csv for program name, version, installdate
im trying to run a command against a list of computers to find out information on the programs on the computers.我试图对计算机列表运行命令以查找有关计算机上程序的信息。 then i would like to export that information on a Csv in the following format:
然后我想以以下格式在 Csv 上导出该信息:
Computer name, McAfee Agent, version, Installdate计算机名称、McAfee Agent、版本、安装日期
i wanted to get the list of these computers and the programs version numbers and installdate.我想获取这些计算机的列表以及程序版本号和安装日期。
this command works, but not for exporting:此命令有效,但不适用于导出:
Get-WmiObject win32_product -ComputerName SYSTEMName01 | Where name -Match "McAfee Agent" | Select Name, Version, Installdate
I tried using the following code here, but this isn't working because it populates the information in Powershell then gives me a:我在这里尝试使用以下代码,但这不起作用,因为它填充了 Powershell 中的信息,然后给了我一个:
$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"
If you're gonna go through the trouble of just querying single information, might as well query the system info a long with it.如果您要通过仅查询单个信息的麻烦来go,不妨用它来查询系统信息。
<#
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
}
}
}
}
The issue with what you're trying to do is, you're not including the computername to be exported along with the other information.您尝试做的问题是,您没有包括要与其他信息一起导出的计算机名。 You also need to
-Append
to the file.您还需要
-Append
到文件中。 The errors you receive mean that you just couldn't connect to the remote machine.您收到的错误意味着您无法连接到远程计算机。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.