0

I want to be able to look for an installed program in powershell and output to a file the results. So far, I have something that will list the install programs, and select the string that has the name of the program, but I am not sure how to specify a text file for it to use for the list of systems, and a way to make it output cleanly.

Get-WmiObject -Class Win32_Product -cn $computernamehere | Select-Object -Property Name | Sort-Object Name | Select-String Vmware | Out-File C:\Users\ajstepanik\Desktop\installed_programs.txt

I would like it to output in a fashion like:

COMPUTER1 - VMware Horizon View Client
COMPUTER2 - VMware Horizon View Client
COMPUTER3 - VMware Horizon View Client

Currently it outputs:

@{Name=VMware Horizon View Client}
@{Name=VMware Horizon View Client}
@{Name=VMware Horizon View Client}
4

2 回答 2

1

在你的情况下,我放弃了我之前关于-ExpandProperty. 我仍然正确,它只会返回一个字符串数组而不是对象属性。但是,我认为如果您将其保留为对象并添加您正在寻找的属性“计算机”,您将有更多选择。那就是我们可以将它作为一个不错的 CSV!我将假设这里有一些你没有显示的循环结构。

$list = Get-Content C:\Users\ajstepanik\Desktop\computers.txt
$list | ForEach-Object{
    $computer = $_
    Get-WmiObject -Class Win32_Product -ComputerName $computer | 
            Select-Object -Property Name | 
            Sort-Object Name | 
            Where-Object{$_.Name -match "Citrix"} | 
            Add-Member -MemberType NoteProperty -Name "Computer" -Value $computer -passthru
} | Export-Csv -NoTypeINformation -Path C:\temp\path.csv

由于我保留了该对象,因此将其更改select-stringWheren 。Match用于Add-MemberComputer. 现在我们可以使用Export-CSV

忘记foreach($x in $y)了不能像其他 foreach 那样使用输出。

于 2014-12-10T17:15:59.263 回答
0

C:\ComputerList.txt每行有一个计算机名称的计算机列表,例如:

COMPUTER1
COMPUTER2
COMPUTER3
COMPUTER4

所以,我会这样做:

$ComputerListFile = 'C:\ComputerList.txt';
$OutputFile = 'C:\VMWareSoftwareReport.csv';

#Variable for the report with header
$Report = @();
$Report += '"ComputerName","SoftwareName","SoftwareVersion"';

#Get the list of computers
$ComputerList = Get-Content $ComputerListFile;

$ComputerList | ForEach-Object {
    $ComputerName = $_;
    #Ping the target to see if it's there
    If ((Get-WmiObject Win32_PingStatus -Filter "Address='$ComputerName' AND Timeout=1000").StatusCode -eq 0) {
        #Get the list of software
        $vmWareSoftware = Get-WmiObject -Query "SELECT Name, Version FROM Win32_Product WHERE Name LIKE '%vmWare%'" -ComputerName $ComputerName | Sort-Object -Property Name;
        $vmWareSoftware | ForEach-Object {
            #Format the results, escape any double quotes, and add them to the report
            $SoftwareName = $_.Name.Replace('"','""');
            $SoftwareVersion = $_.Version.Replace('"','""');
            $Report += '"{0}","{1}","{2}"' -f $ComputerName, $SoftwareName, $SoftwareVersion;
        }
    }
}

#Write the report file, overwriting if needed
Set-Content -Path $OutputFile -Value $Report -Force;

您可能想像Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*@expirat001 提到的那样使用,但Get-ItemProperty 不能以远程计算机为目标。它也可能不是一个完整的列表。就此而言,Win32_Product可能也不完整。没有全面的方法可以在 Windows 中查找所有已安装的软件。

另请注意,Out-File默认情况下将覆盖,而不是附加。您必须指定-Append开关,否则您只需在文件中获取一台计算机。

于 2014-12-10T17:38:58.090 回答