2017-01-18 43 views
1

我有几百台运行不同版本MS Office的机器。我需要找到哪些机器正在运行哪些版本。我有一个PowerShell脚本,我可以获取并导出安装了MS Office的计算机的名称到csv文件,但我无法获得安装在计算机上的office版本以导出到csv。我正在使用的代码如下所示:我在PowerShell中遇到了一些问题

$Computers = Get-Content "\\networkpath\ComputerList.txt" 
$csvFilePath = "\\networkpath\SoftwareList.csv" 

if (!(Test-Path -path $csvFilePath)) { ""|select name,version | Export-Csv -Path $csvFilePath -NoTypeInformation} 

$outputArray = New-Object -TypeName System.Collections.ArrayList 


ForEach ($Computer in $Computers) 
{ 
     Get-WmiObject -computerName $computer -Class CIM_Product -Filter 'Name like "%Microsoft Office Professional Plus%"' | select name 


$Version = select name 
$row = ""|select name,version 
$row.Name = $Computer.ToString() 
$row.Version = $Version.ToString() 
$outputArray.Add($row) 
     } 



$outputArray | Export-Csv -Path $csvFilePath -NoTypeInformation #-Append 

回答

1

您没有存储要在您的Get-WmiObject ...行中重复使用的版本信息。

获得所需结果的一种方法是将get-wmiobject调用的结果存储到变量中,然后使用点符号来获得所需的特定属性。

$wmiObject = get-wmiobject win32_product .... 
$wmiObject.Name 
$wmiObject.Version 

通常情况下,这是不好的做法使用选择,在网上,如果你打算重新使用该对象后来就停机脚本来格式化你的对象。作为一般指导,我会将原始对象数据存储在一个变量中,然后在该行后面对该变量进行格式化。

# declare your array  
$outputarray = @() 

# loop through your collection, build the custom psobject, and add it to your output array 
foreach ($computer in $computers) { 
    $wmiObject = get-wmiobject -computername $computer | where name -like 'Microsoft Office Proffesional Plus*' 
    $obj = new-object -typename psobject 
    $obj | add-member -membertype noteproperty -name 'Name' -value $wmiObject.name 
    $obj | add-member -membertype noteproperty -name 'Version' -value $wmiObject.version 
    $outputarray += $obj 
} 
0

像野性解释你,你不存储你的wmi命令的结果。 我已简化您这样的代码

$Computers = Get-Content "\\networkpath\ComputerList.txt" 
$csvFilePath = "\\networkpath\SoftwareList.csv" 

$Computers | 
    %{Get-WmiObject -computerName $_ -Class CIM_Product -Filter 'Name like "%Microsoft Office Professional Plus%"' | select PSComputerName, Name, Version} | 
     Export-Csv $csvFilePath -NoTypeInformation 
+0

谢谢您的信息。 – Jason

0

感谢您的信息。我只是让它比所需要的更难,最终能够通过更改脚本来获取我需要的数据,这些脚本将所有单元信息取出并直接导出到文本文件。

$$Computers = Get-Content "\\networkpath\ComputerList.csv" 
$FilePath = "\\networkpath\SoftwareList.txt" 

if (!(test-path $FilePath)){New-Item -Path $FilePath} 
ForEach ($computer in $Computers) 
{ 
$Result1 = Get-WmiObject -ComputerName $computer -Class CIM_Product -Filter 'Name like "%Microsoft Office Professional Plus%"' | select Name,Version 
$Result2 = Get-wmiobject Win32_computersystem -computer $computer | select name 
$Result += $Result2, $Result1 | Out-File -FilePath $Filepath -Append 
} 
相关问题