Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

If statement seems to produce strange results

$
0
0

Relatively new to PowerShell. Trying to write a script (using PowerShell 4.0) to determine the various versions of Microsoft Office that might exist on a computer, since our environment is currently a bit of a mess. Our desktops run Windows 7 64-bit. Possible versions of Office that might be installed are 2010 x86, 2013, or 365 (and could be x86 or x64 version of either of the latter two).

The script evaluates which version(s)of Office is installed and then writes a text file for any version found. I plan to use another script (or add on to this one) to take appropriate action (uninstall/install/repair), with the goal of getting everyone up to Office 365 and removing any older versions of Office from the environment.

My script seems to work correctly (I've commented out some Write-Host commands to tell me the value of my variables), but when I get to a series of If statements that use those save variables, I'm getting unexpected results.

For example, the Write-Host commands will return the following info on a computer that has Office 2010 x86 AND Office 365 x86 installed:
O365 x86 installed? True
O365 x64 installed? False
O2010 installed? True
O2013 x86 installed? False
O2013 x64 installed? False

But when I run the script, the If statements that are writing text files write the following text files:
O2010_x86.txt
O365_x86.txt
O365_x64.txt

Not sure if there's something wrong with how I've written the If statement or what's going on, or maybe someone can suggest a better way to do it.

# Set variables to registry paths we want to test the existence of.
$RegPathO365 = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\O365ProPlusRetail - en-us"  #OFFICE 365
$RegPathO2010_x86 = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Office14.PROPLUS"  #Office 2010 x86
$RegPathO2013_x86 = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Office15.PROPLUS" # Office 2013 x86
$RegPathO2013_x64 = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Office15.PROPLUS"  #Office 2013 x64

# Now test those variables to see if one or more of the registry paths actually exist.
#  This will tell us the Office version and the bitness, except for Office 365, which needs an additional test for bitness.
$O365_installed = (Test-Path $RegPathO365)
$O2010_x86_installed = (Test-Path $RegPathO2010_x86)
$O2013_x86_installed = (Test-Path $RegPathO2013_x86)
$O2013_x64_installed = (Test-Path $RegPathO2013_x64)

# start checking bitness of Office 365
If ($O365_installed -eq $true) {
 # Set a variable to get the contents of the ModifyPath property of the Uninstall reg key
 $RegPathString = (Get-Item  $RegPathO365 | Get-ItemProperty -name "ModifyPath")
 # Check if variable contains a match of the specified string to determine bitness of Office 365
 If ($RegPathString -match "platform=x86") {
#   # Office 365 is 32-bit
   $O365_x86_installed = "True"
 }
 Else {$O365_x86_installed = "False"
 }
 If ($RegPathString -match "platform=x64") {
#   # Office 365 is 64-bit
   $O365_x64_installed = "True"
 }
 Else {$O365_x64_installed = "False"
 }


# Now that we've assigned True/False values to the various OS possibilities, let's take action based on those values.
# Let's check for the existence of "C:\Windows\Deploy\OfficeVersions", which is where we'll write some info to.
$PathExists = Test-Path -Path "C:\Windows\Deploy\OfficeVersions"
 # If the path doesn't exist, create it.
 If (!$PathExists) {
  New-Item -Path "C:\Windows\Deploy\" -Name "OfficeVersions" -ItemType Directory
 }


<# 
Write-Host "O365 x86 installed? $O365_x86_installed"
Write-Host "O365 x64 installed? $O365_x64_installed"
Write-Host "O2010 installed? $O2010_x86_installed"
Write-Host "O2013 x86 installed? $O2013_x86_installed"
Write-Host "O2013 x64 installed? $O2013_x64_installed"
#>

# For each variable, if the variable is true, write a corresponding text file.
 # We'll use those text files to determine if multiple versions of Office are installed on the target computer.
If ($O365_x86_installed) {
  New-Item -Path "C:\Windows\Deploy\OfficeVersions" -Name "O365_x86.txt" -ItemType File -Force
}
If ($O365_x64_installed) {
  New-Item -Path "C:\Windows\Deploy\OfficeVersions" -Name "O365_x64.txt" -ItemType File -Force
}
If ($O2010_x86_installed) {
  New-Item -Path "C:\Windows\Deploy\OfficeVersions" -Name "O2010_x86.txt" -ItemType File -Force
}
If ($O2013_x86_installed) {
  New-Item -Path "C:\Windows\Deploy\OfficeVersions" -Name "O2013_x86.txt" -ItemType File -Force
}
If ($O2013_x64_installed) {
  New-Item -Path "C:\Windows\Deploy\OfficeVersions" -Name "O2013_x64.txt" -ItemType File -Force
}


Viewing all articles
Browse latest Browse all 6937

Trending Articles