使用CSV-Export输出已安装的程序

时间:2017-04-05 11:53:40

标签: powershell csv export output

我的当前项目的输出和导出存在几个问题。我使用cmdlet Get-RemoteProgram通过网络和注册表项获取已安装的软件。

这是我现在的代码:

function Get-RemoteProgram ....

$computername = Import-Csv "C:\data\test\test.csv" |
                select -ExpandProperty PCName
$regex = @("Program1|Program2|Program3")
$items = @()

foreach ($computer in $computername) {
  if (Test-Connection $computer -ErrorAction SilentlyContinue -Count 1) {
    $query = Get-RemoteProgram -ComputerName $computer -Property DisplayVersion |
             where {$_.ProgramName -match $regex}
    $obj = New-Object -Type PSObject
    $obj | Add-Member -Name ("Computername") -Value $computer -MemberType NoteProperty -Force
    $maxcount = $query.ProgramName.Count
    if ($maxcount -gt 1) {
      for ($i=0; $i -lt $maxcount; $i++) {
        $progandversion = $query.ProgramName[$i] + $query.DisplayVersion[$i]
        $obj | Add-Member -Name ($progandversion) -Value "Available" -MemberType NoteProperty -Force
      }
    } elseif ($maxcount -eq 1) {
      $progandversion = $query.ProgramName + $query.DisplayVersion
      $obj | Add-Member -Name ($progandversion) -Value "Available" -MemberType NoteProperty -Force
    }

    $obj | Add-Member -Name ("ProgrammVersion$i") -Value $query.DisplayVersion[$i] -MemberType NoteProperty -Force
    $items += $obj
  }

  $items | Export-Csv c:\daten\inventur\output_final.csv -Append -Force
  Write-Host "$computer has been checked.."
}

我现在遇到的问题是我的脚本没有列出我正在寻找的所有不同的程序。它应该导出计算机名称,然后 - 在同一行 - 如果软件已安装则输出可用或如果找不到程序则保持清洁。

这就是我现在得到的输出:

#TYPE System.Management.Automation.PSCustomObject
Computername,"Program1","Program2"
Computer1,"Available","Available"
Computer1,"Available","Available"
Computer2,,
Computer1,"Available","Available"
Computer3,,
Computer2,,
Computer1,"Available","Available"

我不知道为什么计算机在输出中多次出现。

我想这样:

Computername,Program1,Program2,Program3,Program4
Computer1,Available,,Available,,
Computer2,Available,,,,
Computer3,,,Available,
Computer4,,,,

你能以某种方式帮助我吗?

2 个答案:

答案 0 :(得分:2)

你的问题是双重的。首先,您希望以CSV格式更新现有数据,而是在运行-Append时使用Export-CSV。这解释了存在多个具有给定ComputerName的行。其次,您没有为给定的ProgramName设置默认值,因此输出CSV中不存在任何地方找不到的程序的属性。要解决您的第一个问题,您需要运行Export-CSV而不必将整个数据集保存到CSV文件中。要解决您的第二个问题,您应该使用属性预填充新的PSObject。应该这样做准备:

$programs=get-content "programs.txt" # one name one line, or an array of names in @()
$regex='('+($programs -join ',')+')' # regex out of array

然后在主循环中,在调用New-Object后添加此行:

$programs | % { $obj | Add-Member -Name $_ -Value "Not found" -MemberType NoteProperty } # create default values

应该这样做。交换"未找到"如果你愿意,可以换一个空字符串。

答案 1 :(得分:0)

c#安装程序中的编码版本通过Windows注册表

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace SoftwareInventory
{
    class Program
    {
        static void Main(string[] args)
        {
            //!!!!! Must be launched with a domain administrator user!!!!!
            Console.ForegroundColor = ConsoleColor.Green;
            StringBuilder sbOutFile = new StringBuilder();
            Console.WriteLine("DisplayName;IdentifyingNumber");
            sbOutFile.AppendLine("Machine;DisplayName;Version");

            //Retrieve machine name from the file :File_In/collectionMachines.txt
            //string[] lines = new string[] { "NameMachine" };
            string[] lines = File.ReadAllLines(@"File_In/collectionMachines.txt");
            foreach (var machine in lines)
            {
                //Retrieve the list of installed programs for each extrapolated machine name
                var registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
                using (Microsoft.Win32.RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machine).OpenSubKey(registry_key))
                {
                    foreach (string subkey_name in key.GetSubKeyNames())
                    {
                        using (RegistryKey subkey = key.OpenSubKey(subkey_name))
                        {
                            //Console.WriteLine(subkey.GetValue("DisplayName"));
                            //Console.WriteLine(subkey.GetValue("IdentifyingNumber"));
                            if (subkey.GetValue("DisplayName") != null)
                            {
                                Console.WriteLine(string.Format("{0};{1};{2}", machine, subkey.GetValue("DisplayName"), subkey.GetValue("Version")));
                                sbOutFile.AppendLine(string.Format("{0};{1};{2}", machine, subkey.GetValue("DisplayName"), subkey.GetValue("Version")));
                            }
                        }
                    }
                }
            }
            //CSV file creation
            var fileOutName = string.Format(@"File_Out\{0}_{1}.csv", "Software_Inventory", DateTime.Now.ToString("yyyy_MM_dd_HH_mmssfff"));
            using (var file = new System.IO.StreamWriter(fileOutName))
            {

                file.WriteLine(sbOutFile.ToString());
            }
            //Press enter to continue 
            Console.WriteLine("Press enter to continue !");
            Console.ReadLine();
        }


    }
}