如何找到64位或32位excel安装通过Powershell



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

但是当用户只安装excel而不安装outlook时,此注册表项不可靠(在64位操作系统中可用,但在32位操作系统中不可用)。

下面是我们写的函数来找到它。现在,由于注册表项不可用,它不工作。还有其他方法可以找到excel的位值吗?

Function Get-OfficeVersionInstalled
{
    $NoExcelInstalled = '0'
    $excelApplicationRegKey = "HKLM:SOFTWAREClassesExcel.ApplicationCurVer"
    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:SOFTWAREMicrosoftOffice$officeVersionOutlook"
        $OutlookWow6432NodeKey = "HKLM:SOFTWAREWow6432NodeMicrosoftOffice$officeVersionOutlook"
        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
}

如果没有模拟器,就不能在32位版本的Windows上运行64位软件(有没有办法在32位计算机上执行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.
}

最新更新