0

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?

  • Have you read the error and tried any of the suggestions of using `Get-ExecutionPolicy -List` and `Get-Help Set-ExecutionPolicy`. What did you see when doing `Get-ExecutionPolicy -List`? Also Google the error message and you will see plenty of results with solutions to try. – Daniel Apr 10 '22 at 03:45
  • I did that yes and you just see the different ways to set the execution policies and tried all those that could apply for what i need. And i went trough the first 5 pages of google and didn't find any solution yet. – Thomas Van Haute Apr 11 '22 at 11:54
  • What do you see when doing `Get-ExecutionPolicy -List`? Update your question with the results. – Daniel Apr 11 '22 at 13:44

0 Answers0