2016-08-24 76 views
1

我們編寫了powershell函數來查找是否安裝了64位或32位msi。我們正在檢查Outlook註冊表項,因爲它是具有位信息的那個。如何查找是否通過Powershell安裝了64位或32位excel?

但是,當用戶只安裝沒有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 
} 
+0

您可以使用-ErrorAction SilentlyContinue你REG querys月底停止顯示,錯誤並保持如果不存在鍵去。示例 - $ Reg32Key = Get-ItemProperty -path「HKLM:\ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall \ Google Chrome」-name「Version」-ErrorAction SilentlyContinue –

回答

2

無法在沒有模擬器的情況下在32位版本的Windows上運行64位軟件(Is there any way to execute 64-bit programs on a 32-bit computer?)。這意味着如果您檢測到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. 
} 
相關問題