wmi 类“win32reg_addremoveprograms”的 64 位等效类?

发布于 2024-08-24 18:13:27 字数 2375 浏览 5 评论 0原文

我的下面的代码在 32 位 Windows 计算机上完美运行,但由于代码中使用了 32 位 WMI 类 win32reg_addremoveprograms,因此它无法在 64 位计算机上运行。是否有该类的 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

My code below works perfectly on a 32-bit Windows machine, however it refuses to work on a 64-bit machine due to the 32-bit WMI class win32reg_addremoveprograms used in the code. Is there a 64-bit equivalent of this class?

$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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-08-31 18:13:27

您的 64 位计算机上是否安装了 SCCM/SMS 客户端?因为 Win32Reg_AddRemovePrograms WMI 类是由 SCCM/SMS 客户端添加的,因此如果您没有安装它,则该类不可用。

另请注意,Win32Reg_AddRemovePrograms 仅提供有关 32 位已安装应用程序的信息。要获取有关 64 位应用程序的信息,请使用 Win32Reg_AddRemovePrograms64 类(在 SMS 2003 SP3 及更高版本中提供)。

Do you have the SCCM/SMS client installed on your 64-bit machine? Because the Win32Reg_AddRemovePrograms WMI class is added by the SCCM/SMS client, and therefore isn't available if you don't have it installed.

Note also that Win32Reg_AddRemovePrograms provide info on 32-bit installed applications only. To get info on 64-bit applications, use the Win32Reg_AddRemovePrograms64 class (available in SMS 2003 SP3 and later).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文