如何通过Powershell查找是否安装了64位或32位excel?

时间:2016-08-24 13:55:48

标签: excel windows powershell outlook

我们编写了powershell函数来查找是否安装了64位或32位msi。我们正在检查outlook注册表项,因为它是具有位数信息的那个。

但是当用户只安装没有outlook的excel时,这个注册表项不可靠(在64位操作系统中它可用但在32位操作系统中它不可用)。

以下是我们编写的功能。现在,由于注册表项不可用,因此无效。还有其他方法我们可以找到excel的位数吗?

Function Get-OfficeVersionInstalled
{
    $NoExcelInstalled = '0'
    $excelApplicationRegKey = "HKLM:\SOFTWARE\Classes\Excel.Application\CurVer"
    if( Test-Path $excelApplicationRegKey)
    {
        $excelApplicationCurrentVersion = (Get-ItemProperty $excelApplicationRegKey).'(default)'

        #Get version number alone from registry value
        $($excelApplicationCurrentVersion -replace "Excel.Application.","")
    }
    else
    {
        $NoExcelInstalled
    }
}

Function Test-Excel2013AndAbove
{
    Param
    (
        [ValidateSet("x64", "x86")]
        $Edition="x64"  
    )
    $isExpectedEditionInstalled = $false
    $officeVersion = Get-OfficeVersionInstalled
    $office2013Version = 15

    if( $officeVersion -ge $office2013Version) {

    # In registry, version will be with decimal
        $officeVersion = $officeVersion+".0"

        # Outlook key is having bitness which will decide the edition. 
    # Even if outlook is not installed this key will be present.
    # This is the only place where we can reliably find the edition of Excel
        $OutlookKey = "HKLM:\SOFTWARE\Microsoft\Office\$officeVersion\Outlook"
        $OutlookWow6432NodeKey = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Office\$officeVersion\Outlook"

        if(Test-Path $OutlookKey)
        {       
            $officeRegKey = $OutlookKey
        }
        else
        {        
            $officeRegKey = $OutlookWow6432NodeKey
        }

        $BitNess = (Get-ItemProperty $officeRegKey).BitNess

        if($BitNess -eq $Edition)
        {
            $isExpectedEditionInstalled = $true
        }
        else
        {
            $isExpectedEditionInstalled = $false
        }

    }

    return $isExpectedEditionInstalled
}

1 个答案:

答案 0 :(得分:2)

如果没有模拟器(Is there any way to execute 64-bit programs on a 32-bit computer?),则无法在32位版本的Windows上运行64位软件。这意味着如果您检测到32位操作系统,则Excel的任何本地非模拟安装(如果有)将为32位。

所以这里有一些伪代码可以做到这一点:

if (OS.BitSize == 32)
{
    Check if Excel installed. If so, then it is 32 bit.
}
else
{
    //64 bit OS
    Check registry key to determine whether 32 or 64 bit Excel is installed.
}