Ghost Solution Suite

 View Only

 Error 255 during script execution - uninstalling Teams machine wide

Jump to  Best Answer
Jerome Engel's profile image
Jerome Engel posted Jan 15, 2021 02:16 AM

Greetings, 

we deployed Teams with GSSC as MSI on all of our stations so that every user that logs in automatically has it installed. there are usually dozens of userprofiles per machine on dozens of machines. So Ghost offers a great QoL improvement.
Now teams needs a new version. In order for that to work we need to remove teams on every machine for every user and also uninstall the teams machine wide installer.
I got a working script for that. At least when I paste the code locally on the PC into the PowerShell it works perfectly and I can push the new MSI via Ghost.

but when I want to execute the script via Ghost I get "Error 255 during script execution".

I'm using the local admin user to execute it (as the script needs access to every user profile) 

what am I doing wrong?  would love the help.

script:

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 Machine-Wide Installer
Write-Host "Removing Teams Machine-wide Installer" -ForegroundColor Yellow
$MachineWide = Get-WmiObject -Class Win32_Product | Where-Object{$_.Name -eq "Teams Machine-Wide Installer"}
$MachineWide.Uninstall()

# Get all Users
$Users = Get-ChildItem -Path "$($ENV:SystemDrive)\Users"

# Process all the Users
$Users | ForEach-Object {
        Write-Host "Process user: $($_.Name)" -ForegroundColor Yellow

        #Locate installation folder
        $localAppData = "$($ENV:SystemDrive)\Users\$($_.Name)\AppData\Local\Microsoft\Teams"
        $programData = "$($env:ProgramData)\$($_.Name)\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 for user $($_.Name)"
        }
}
Blake Thomas's profile image
Broadcom Employee Blake Thomas  Best Answer
If you're running this script in the actual script window in the GSS task (and not as a .ps1 file) then you may need to put #powershell in the first line​. They talk about it a bit in this thread. I would imagine this is the problem as error 255 is a generic batch script error.

If you're still having issues, you can paste this script into a ps1 file and execute it via command with something like this:
PowerShell -ExecutionPolicy Bypass -File C:\temp\myscript.ps1
This is of course assuming you have permissions to do so, although this could be considered risky behavior and may be picked up by antivirus monitoring.