2017-01-18 47 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 
} 
相關問題