0

I have a script that pulls version numbers and compares them to a string. The logic always fails. I initially compared with -ne and it always returned true. I then changed it to compare with -eq and it always returns true. What am I doing wrong??

I am trying to install an iManage update if the version is below the target version. I pull the installed version with Get-WMI-Object and expand the Version property. I then convert that to a string and trim whitespace. Later in the script I compare that variable with the version number enclosed in "". It always returns true, no matter if I compare with -ne or -eq. If I print the output of the variable it looks correct. If I connect to the machine and manually run the comparison command after pulling the version the logic works. But in the context of this script it does not work.

#iManage Installer: iManageInstaller.exe Path: C:\Optimal\iManage\iManage_10.6.0.5\iManage Installer 10.6.0.5\iManageInstaller.exe
#iManage Work Desktop x64 Agent Services: iManageAgentServices.exe Path: C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x64 Office)\iManageAgentServices.exe
#iManage Work Desktop x64 for Windows: iManageWorkDesktopforWindowsx64.exe Path: C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x64 Office)\iManageWorkDesktopforWindowsx64.exe
#iManage Work Desktop x86 Agent Services: iManageAgentServices.exe Path: C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x86 Office)\iManageAgentServices.exe
#iManage Work Desktop x86 for Windows: iManageWorkDesktopforWindowsx86.exe Path: C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x86 Office)\iManageWorkDesktopforWindowsx86.exe

#Defines function used to kill all Office, Adobe, and iManage applications
function Kill-Running-Applications{
$processList = "MSAccess","Excel","Teams","OneNote","OneDrive","Outlook","Powerpnt","Visio","MSPub","WinWord"
$processList | ForEach-Object {Get-Process -Name $_ -ErrorAction Ignore | Stop-Process -Force}
Get-Process -Name Adobe* -ErrorAction Ignore | Stop-Process -Force
Get-Process -Name Acro* -ErrorAction Ignore | Stop-Process -Force
Get-Process -Name 'CCLibrary' -ErrorAction Ignore | Stop-Process -Force
Get-Process -Name 'CCXProcess' -ErrorAction Ignore | Stop-Process -Force
Get-Process -Name 'CoreSync' -ErrorAction Ignore | Stop-Process -Force
Get-Process -Name 'AdobeIPCBroker' -ErrorAction Ignore | Stop-Process -Force
Get-Process -Name 'Adobe CEF Helper' -ErrorAction Ignore | Stop-Process -Force
Get-Process -Name iManage* -ErrorAction Ignore | Stop-Process -Force
Get-Process -Name Creative* -ErrorAction Ignore | Stop-Process -Force
}

#Defines function used to get installed software versions
function Get-Software-Versions{
#Gets current version of 3 iManage components from WMI and assigns to variables
$AgentVersion = Get-WmiObject -Class Win32_Product | where Name -eq 'iManage Agent Services' | select Version -ExpandProperty Version
$WorkDesktopVersion = Get-WmiObject -Class Win32_Product | where Name -eq 'iManage Work Desktop for Windows' | select Version -ExpandProperty Version
if($AgentVersion){
$AgentVersion = $AgentVersion.ToString()
$AgentVersion = $AgentVersion.Trim()
}else{
$AgentVersion = "0.0.0"
}
if($WorkDesktopVersion){
$WorkDesktopVersion = $WorkDesktopVersion.ToString()
$WorkDesktopVersion = $WorkDesktopVersion.Trim()
}else{
$WorkDesktopVersion = "0.0.0"
}

#Software versions should be:
#iManage Agent Services - 106.0.3
#iManage Installer - 106.0.5
#iManage Work Desktop - 106.0.66

#Writes the current software versions to output
Write-Host "iManage Agent Current Version:" $AgentVersion
Write-Host "iManage Work Desktop Current Version:" $WorkDesktopVersion
}

#Removes folders if they exist
Remove-Item -LiteralPath "C:\Optimal\iManage" -Force -Recurse
Remove-Item -LiteralPath "C:\Optimal\Zips" -Force -Recurse

#Creates C:\Optimal and subfolders if required
New-Item -ItemType Directory -Force -Path C:\Optimal
New-Item -ItemType Directory -Force -Path C:\Optimal\iManage
New-Item -ItemType Directory -Force -Path C:\Optimal\Zips

#Sets download URL for iManage 10.6.0.5 install files
$Url = 'http://files.optimalsphere.com/ODOODO/iManage_10.6.0.5.zip'

#Gets zip file name from URL and uses it to set download file name and location
$ZipFile = 'C:\Optimal\Zips\' + $(Split-Path -Path $Url -Leaf)

#Sets Zip file extraction location
$Destination= 'C:\Optimal\iManage'

#Downloads installer Zip file
Invoke-WebRequest -Uri $Url -OutFile $ZipFile 

#Extracts Zip file to C:\Optimal\iManage
$ExtractShell = New-Object -ComObject Shell.Application 
$Files = $ExtractShell.Namespace($ZipFile).Items() 
$ExtractShell.NameSpace($Destination).CopyHere($Files)

#Checks registry key to determine Office Bit Version
$OfficeRegKey = "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration"
$OfficeBitVersion = (Get-ItemProperty -Path $OfficeRegKey -Name Platform).Platform

#Calls function to check installed software versions
Get-Software-Versions

#Removed iManage Installer as it is not needed
If (Test-Path 'C:\ProgramData\Package Cache\*\iManageInstaller.exe' -PathType Leaf){
Start-process 'C:\ProgramData\Package Cache\*\iManageInstaller.exe' -ArgumentList '/uninstall /silent' -NoNewWindow -Wait -ErrorAction Ignore
}

#Checks office version, then software versions and installs if update is needed.
If($OfficeBitVersion -eq "x64"){

If($AgentVersion -eq "106.0.3"){
#Writes that update is not needed
Write-Host "iManage Agent Services Up to Date"
}else{
#Writes that update is needed
Write-Host "iManage Agent Services Update Needed"
#Calls function to kill running processes
Kill-Running-Applications
#Starts installer silently
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x64 Office)\iManageAgentServices.exe' -ArgumentList '/silent AUTO_UPDATE=0' -NoNewWindow -Wait
}

If($WorkDesktopVersion -eq "106.0.66"){
#Writes that update is not needed
Write-Host "iManage Work Desktop Up to Date"
}else{
#Writes that update is needed
Write-Host "iManage Work Desktop Update Needed"
#Calls function to kill running processes
Kill-Running-Applications
#Starts installer silently
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x64 Office)\iManageWorkDesktopforWindowsx64.exe' -ArgumentList '/silent' -NoNewWindow -Wait
}

}else{

If($AgentVersion -eq "106.0.3"){
#Writes that update is not needed
Write-Host "iManage Agent Services Up to Date"
}else{
#Writes that update is needed
Write-Host "iManage Agent Services Update Needed"
#Calls function to kill running processes
Kill-Running-Applications
#Starts installer silently
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x86 Office)\iManageAgentServices.exe' -ArgumentList '/silent AUTO_UPDATE=0' -NoNewWindow -Wait
}

If($WorkDesktopVersion -ne "106.0.66"){
#Writes that update is not needed
Write-Host "iManage Work Desktop Up to Date"
}else{
#Writes that update is needed
Write-Host "iManage Work Desktop Update Needed"
#Calls function to kill running processes
Kill-Running-Applications
#Starts installer silently
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x86 Office)\iManageWorkDesktopforWindowsx86.exe' -ArgumentList '/silent' -NoNewWindow -Wait
}}

#Calls function to check installed software versions
Get-Software-Versions

#Runs uninstall/reinstall for applications that failed to update
If($OfficeBitVersion -eq "x64"){

If($AgentVersion -eq "106.0.3"){
#Writes that update succeeded
Write-Host "iManage Agent Services update succeeded"
}else{
#Writes that update Failed, Performing cleanup and reinstall
Write-Host "iManage Agent Services Update Failed, Performing cleanup and reinstall"
#Calls function to kill running processes
Kill-Running-Applications
#Starts silent uninstall
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x64 Office)\iManageAgentServices.exe' -ArgumentList '/uninstall /silent' -NoNewWindow -Wait
#Starts installer silently
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x64 Office)\iManageAgentServices.exe' -ArgumentList '/silent AUTO_UPDATE=0' -NoNewWindow -Wait
}

If($WorkDesktopVersion -eq "106.0.66"){
#Writes that update succeeded
Write-Host "iManage Work Desktop update succeeded"
}else{
#Writes that update Failed, Performing cleanup and reinstall
Write-Host "iManage Work Desktop Update Failed, Performing cleanup and reinstall"
#Calls function to kill running processes
Kill-Running-Applications
#Starts silent uninstall
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x64 Office)\iManageWorkDesktopforWindowsx64.exe' -ArgumentList '/uninstall /silent' -NoNewWindow -Wait
#Starts installer silently
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x64 Office)\iManageWorkDesktopforWindowsx64.exe' -ArgumentList '/silent' -NoNewWindow -Wait
}

}else{

If($AgentVersion -eq "106.0.3"){
#Writes that update succeeded
Write-Host "iManage Agent Services update succeeded"
}else{
#Writes that update Failed, Performing cleanup and reinstall
Write-Host "iManage Agent Services Update Failed, Performing cleanup and reinstall"
#Calls function to kill running processes
Kill-Running-Applications
#Starts silent uninstall
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x86 Office)\iManageAgentServices.exe' -ArgumentList '/uninstall /silent' -NoNewWindow -Wait
#Starts installer silently
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x86 Office)\iManageAgentServices.exe' -ArgumentList '/silent AUTO_UPDATE=0' -NoNewWindow -Wait
}

If($WorkDesktopVersion -eq "106.0.66"){
#Writes that update succeeded
Write-Host "iManage Work Desktop update succeeded"
}else{
#Writes that update Failed, Performing cleanup and reinstall
Write-Host "iManage Work Desktop Update Failed, Performing cleanup and reinstall"
#Calls function to kill running processes
Kill-Running-Applications
#Starts silent uninstall
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x86 Office)\iManageWorkDesktopforWindowsx86.exe' -ArgumentList '/uninstall /silent' -NoNewWindow -Wait
#Starts installer silently
Start-Process 'C:\Optimal\iManage\iManage_10.6.0.5\iManage Work Desktop for Windows 10.6.0.66 (x86 Office)\iManageWorkDesktopforWindowsx86.exe' -ArgumentList '/silent' -NoNewWindow -Wait
}}

#One last version check, and write any failures to output
Get-Software-Versions

#checks versions and writes outputs
If($AgentVersion -eq "106.0.3"){
#Writes that update succeeded
Write-Host "iManage Agent Services update succeeded:" $AgentVersion
}else{
#Writes that update Failed
Write-Host "iManage Agent Services Update Failed:" $AgentVersion
}

If($WorkDesktopVersion -eq "106.0.66"){
#Writes that update succeeded
Write-Host "iManage Work Desktop update succeeded:" $WorkDesktopVersion
}else{
#Writes that update Failed
Write-Host "iManage Work Desktop Update Failed:" $WorkDesktopVersion
}

Output:

Directory: C:\


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
d-----        5/18/2022   5:44 PM                Optimal                                                               


    Directory: C:\Optimal


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
d-----        5/18/2022   5:44 PM                iManage                                                               
d-----        5/18/2022   5:44 PM                Zips                                                                  
iManage Agent Current Version: 106.0.3
iManage Work Desktop Current Version: 0.0.0
iManage Agent Services Update Needed
iManage Work Desktop Update Needed
iManage Agent Current Version: 106.0.3
iManage Work Desktop Current Version: 106.0.66
iManage Agent Services Update Failed, Performing cleanup and reinstall
iManage Work Desktop Update Failed, Performing cleanup and reinstall
iManage Agent Current Version: 106.0.3
iManage Work Desktop Current Version: 106.0.66
iManage Agent Services Update Failed: 
iManage Work Desktop Update Failed:

Output when using -ne:

Directory: C:\


Mode                LastWriteTime         Length Name                          
----                -------------         ------ ----                          
d-----        5/18/2022   5:23 PM                Optimal                       


    Directory: C:\Optimal


Mode                LastWriteTime         Length Name                          
----                -------------         ------ ----                          
d-----        5/18/2022   5:23 PM                iManage                       
d-----        5/18/2022   5:23 PM                Zips                          
iManage Agent Current Version: 106.0.3
iManage Installer Current Version: 
iManage Work Desktop Current Version: 106.0.66
iManage Agent Services Update Needed
iManage Work Desktop Update Needed
iManage Agent Current Version: 106.0.3
iManage Installer Current Version: 
iManage Work Desktop Current Version: 106.0.66
iManage Agent Services Update Failed, Performing cleanup and reinstall
iManage Work Desktop Update Failed, Performing cleanup and reinstall
iManage Agent Current Version: 106.0.3
iManage Installer Current Version: 
iManage Work Desktop Current Version: 106.0.66
iManage Agent Services Update Failed: 
iManage Work Desktop Update Failed:
  • 2
    As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies_ the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 May 18 '22 at 22:23
  • 2
    As an aside: `select Version -ExpandProperty Version` is redundant (it not only returns the version, but decorates it with a `.Version` ETS property) - use `select -ExpandProperty Version` – mklement0 May 18 '22 at 22:31
  • 2
    Perhaps you can whittle down your code to a [mcve]. – mklement0 May 18 '22 at 22:42
  • I've now tested this script on the machine directly in Powershell and it works as expected. I'm suspecting this is an issue with the way our RMM is processing the script when pushed. – Tony Scarborough May 18 '22 at 22:49

0 Answers0