'Uninstall MS Teams with Powershell script trough N-Able RMM doesn't work while locally it does work. Why is that? What can i do to fix it?

I have a Powershell script to remove MS Teams and it works locally but when i use it with N-Able RMM it fails.

In my script i think somehow he doesnt recognize the environment variables because as output i get this: "Teams installation not found" While teams is installed and he can find it locally with no issues.


#Uninstall teams
function unInstallTeams($path) {

    $clientInstaller = "$($path)\Update.exe"
    
     try {
          $process = Start-Process -FilePath "$clientInstaller" -ArgumentList "--uninstall /s" -PassThru -Wait -ErrorAction STOP
  
          if ($process.ExitCode -ne 0)
      {
        Write-Error "UnInstallation failed with exit code  $($process.ExitCode)."
          }
      }
      catch {
          Write-Error $_.Exception.Message
      }  
  } 
  
  # Remove Teams for Current Users
  $localAppData = "$($env:LOCALAPPDATA)\Microsoft\Teams"
  $programData = "$($env:ProgramData)\$($env:USERNAME)\Microsoft\Teams"  
  
  If (Test-Path "$($localAppData)\Current\Teams.exe") 
  {
    unInstallTeams($localAppData)      
  } elseif (Test-Path "$($programData)\Current\Teams.exe") {
    unInstallTeams($programData)
  } else {
    Write-Warning  "Teams installation not found"
  }

So i made a second script where i downloaded script 1 to then locally execute it.

$uri = "https://vincienergies-my.sharepoint.com/:u:/g/personal/thomas_vanhaute_vinci-energies_net/EaB4gznIHXtIsH8z3QTmXt0BhJ27oclQoIGoyr0jNaclsA?download=1"
$out = "C:\Windows\Temp\uninstallTeams.ps1"
Invoke-WebRequest -uri $uri -OutFile $out
C:\Windows\Temp\uninstallTeams.ps1

I got the warning that a downloaded script can't be executed with current policies. So i tried with adding the following commands one by one:

Powershell.exe -ExecutionPolicy Bypass C:\Windows\Temp\uninstallTeams.ps1

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser -Force

Set-ExecutionPolicy -scope currentuser unrestricted -force

Set-ExecutionPolicy Undefined  -Scope CurrentUser -Force

But all of them gave my the following error:

Summary: Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by
a policy defined at a more specific scope. Due to the override, your shell will retain its current effective
execution policy of Bypass. Type \"Get-ExecutionPolicy -List\" to view your execution policy settings. For more
information please see \"Get-Help Set-ExecutionPolicy\".
At line:1 char:79
 + ... ionPolicy RemoteSigned }; .\\82967.ps1; Set-ExecutionPolicy $p; Exit $ ...
 + ~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
 + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

When i enter Get-ExecutionPolicy -List the following shows

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    Unrestricted
 LocalMachine    RemoteSigned

Does anybody knows what i am doing wrong or how i can fix this/make this work?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source