2010-03-10 50 views
3

我的代碼在32位Windows機器上完美工作,但由於代碼中使用了32位WMI類win32reg_addremoveprograms,它拒絕在64位機器上工作。這個類有64位的等價物嗎?wmi類「win32reg_addremoveprograms」的64位等效類?

$ServerFile = "D:\SharePoint\Powershell\AddRemovePrograms\Machines.txt" 
$ServerList = Get-Content $ServerFile 

$Excel = New-Object -Com Excel.Application 
$Excel.displayalerts=$False 
$Excel.visible = $True 

$workbook = $Excel.Workbooks.Add() 
$workbook.workSheets.item(2).delete() 
$workbook.WorkSheets.item(2).delete() 

$Sheet = $workbook.WorkSheets.Item(1) 
$Sheet.Name= "Program List"; 

$intRow = 1 

foreach ($NextServer in $ServerList) 
{ 
    $Sheet.Cells.Item($intRow,1) = 「Computer Name」 
    $Sheet.Cells.Item($intRow,2) = $NextServer 

    $Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 8 
    $Sheet.Cells.Item($intRow,1).Font.ColorIndex = 11 
    $Sheet.Cells.Item($intRow,1).Font.Bold = $True 
    $Sheet.Cells.Item($intRow,2).Interior.ColorIndex = 8 
    $Sheet.Cells.Item($intRow,2).Font.ColorIndex = 11 
    $Sheet.Cells.Item($intRow,2).Font.Bold = $True 

    $intRow = $intRow + 2 
    $Sheet.Cells.Item($intRow,1) = "Programs" 
    $Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 12 
    $Sheet.Cells.Item($intRow,1).Font.ColorIndex = 8 
    $Sheet.Cells.Item($intRow,1).Font.Bold = $True 
    $intRow = $intRow + 1 

    $colItems =get-wmiobject -class "win32reg_addremoveprograms" -computername $NextServer | select-object -property DisplayName | sort-object DisplayName 

    foreach ($objItem in $colItems) 
    { 
     if ($objItem.DisplayName.IndexOf('Update') -eq -1 -and $objItem.DisplayName.IndexOf('2007 Microsoft Office') -eq -1) 
     { 
      $Sheet.Cells.Item($intRow,1) = $objItem.DisplayName 
      $intRow = $intRow + 1 
     } 
    } 
    $intRow = $intRow + 2 
} 

$workArea = $Sheet.UsedRange 
$workArea.EntireColumn.AutoFit() 
$workbook.SaveAs("D:\SharePoint\Powershell\AddRemovePrograms\Programs.xls") 
$workbook.close() 
$a = [System.Runtime.InteropServices.Marshal]::ReleaseComObject($workArea) 
$a = [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Sheet) 
$a = [System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) 
$Excel.Quit() 
$a = [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Excel) 
[System.GC]::Collect() 
[System.GC]::WaitForPendingFinalizers() 
Remove-Variable Excel, intRow ,workbook, Sheet,colItems, ServerFile, ServerList, a 

回答

5

是否在您的64位機器上安裝了SCCM/SMS客戶端?由於SCCM/SMS客戶端添加了Win32Reg_AddRemovePrograms WMI類,因此如果沒有安裝它,則不可用。

還要注意,Win32Reg_AddRemovePrograms只提供有關32位安裝的應用程序的信息。要獲取64位應用程序的信息,請使用Win32Reg_AddRemovePrograms64類(可在SMS 2003 SP3及更高版本中使用)。