PowerShell Teams脚本异常:关PS时Teams随关且运行报错
Microsoft Teams VDI缓存清理脚本异常问题修复
问题现象
使用以下PowerShell脚本清理Teams缓存并启动程序,此前运行正常,当前出现两个异常:
- 关闭PowerShell窗口时,Teams进程也随之终止
- 关闭PowerShell前会弹出报错信息
Teams为Citrix VDI/VDA环境下按微软VDI部署建议安装的HDX优化版本。
原脚本内容:
# start Teams # Start-Process -File "C:\Program Files (x86)\Microsoft\Teams\current\Teams.exe" -ArgumentList '--processStart "Teams.exe"' # Write-Host "Teams Process Sucessfully Started" # Start-Sleep -Seconds 5 #Stop Teams process Get-Process -ProcessName Teams -ErrorAction SilentlyContinue | Stop-Process -Force Start-Sleep -Seconds 3 Write-Host "Teams Process Sucessfully Stopped" #Clear Team Cache try{ # Get-ChildItem -Path $env:APPDATA"Microsoft\Teams" | Remove-Item -Recurse -ErrorAction SilentlyContinue # Clear.all Get-ChildItem -Path $env:APPDATA"Microsoft\Teams\blob_storage" | Remove-Item -Recurse -ErrorAction SilentlyContinue Get-ChildItem -Path $env:APPDATA"Microsoft\Teams\databases" | Remove-Item -Recurse -ErrorAction SilentlyContinue Get-ChildItem -Path $env:APPDATA"Microsoft\Teams\cache" | Remove-Item -Recurse -ErrorAction SilentlyContinue Get-ChildItem -Path $env:APPDATA"Microsoft\Teams\gpucache" | Remove-Item -Recurse -ErrorAction SilentlyContinue Get-ChildItem -Path $env:APPDATA"Microsoft\Teams\Indexeddb" | Remove-Item -Recurse -ErrorAction SilentlyContinue Get-ChildItem -Path $env:APPDATA"Microsoft\Teams\Local Storage" | Remove-Item -Recurse -ErrorAction SilentlyContinue Get-ChildItem -Path $env:APPDATA"Microsoft\Teams\tmp" | Remove-Item -Recurse -ErrorAction SilentlyContinue Write-Host "Teams Cache Cleaned" }catch{ echo $_ } #Remove Credential from Credential manager $credential = cmdkey /list | ForEach-Object{if($_ -like "*Target:*" -and $_ -like "*msTeams*"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}} #Remove Reg.Key $Regkeypath= "HKCU:\Software\Microsoft\Office\Teams" $value = (Get-ItemProperty $Regkeypath).HomeUserUpn -eq $null If ($value -eq $False) { Remove-ItemProperty -Path "HKCU:\Software\Microsoft\Office\Teams" -Name "HomeUserUpn" Write-Host "The registry value Sucessfully removed" } Else { Write-Host "The registry value does not exist"} #Get Desktop-config.json $TeamsFolders = "$env:APPDATA\Microsoft\Teams" try{ $SourceDesktopConfigFile = "$TeamsFolders\desktop-config.json" $desktopConfig = (Get-Content -Path $SourceDesktopConfigFile | ConvertFrom-Json) } catch{ Write-Host "Failed to open Desktop-config.json" } #Overwrite the desktop-config.json Write-Host "Modify desktop-Config.Json" try{ $desktopConfig.isLoggedOut = $true $desktopConfig.upnWindowUserUpn =""; #The email used to sign in | also: whoami /upn $desktopConfig.userUpn =""; $desktopConfig.userOid =""; $desktopConfig.userTid = ""; $desktopConfig.homeTenantId =""; $desktopConfig.webAccountId=""; $desktopConfig | ConvertTo-Json -Compress | Set-Content -Path $SourceDesktopConfigFile -Force } catch{ Write-Host "Failed to overwrite desktop-config.json" } Write-Host "Modify desktop-Config.Json - Finished" #Delete the Cookies file. This is a fix for when the joining as anonymous, and prevents the last used guest name from being reused. Get-ChildItem "$TeamsFolders\Cookies" | Remove-Item #Lastly delete the storage.json, this corrects some error that MSTeams otherwise would have when logging in again. Get-ChildItem "$TeamsFolders\storage.json" | Remove-Item #Try to remove the Link School/Work account if there was one. It can be created if the first time you sign in, the user all # $LocalPackagesFolder ="$env:LOCALAPPDATA\Packages" # $AADBrokerFolder = Get-ChildItem -Path $LocalPackagesFolder -Recurse -Include "Microsoft.AAD.BrokerPlugin_*"; # $AADBrokerFolder = $AADBrokerFolder[0]; # Get-ChildItem "$AADBrokerFolder\AC\TokenBroker\Accounts" | Remove-Item -Recurse -Force # Start-Sleep -Seconds 5 # start Teams Start-Process -File "C:\Program Files (x86)\Microsoft\Teams\current\Teams.exe" -ArgumentList '--processStart "Teams.exe"' # "$($env:USERProfile)\AppData\Local\Microsoft\Teams\Update.exe" Start-Sleep -Seconds 3 Write-Host "Teams Process Sucessfully Started" # Stop for testpurposes pause
问题原因及修复方案
1. 关闭PowerShell时Teams随之关闭的修复
原因:直接启动Teams.exe会让进程继承PowerShell的控制台会话,父进程关闭时会连带终止子进程。VDI环境下Teams官方推荐通过Update.exe启动,而非直接调用Teams.exe。
修复:替换启动命令为VDI专属方式,确保Teams进程脱离PowerShell控制台控制:
Start-Process -FilePath "$env:LOCALAPPDATA\Microsoft\Teams\Update.exe" -ArgumentList "--processStart Teams.exe" -NoNewWindow
2. 关闭PowerShell前报错的修复
原因:脚本未处理多种错误场景,比如注册表项不存在时Get-ItemProperty会抛出错误、文件被占用时删除操作失败等。
修复:
- 给注册表操作添加路径检测,避免项不存在时报错
- 文件删除前先检查路径存在性,增强错误忽略逻辑
- 移除测试用的
pause命令(生产环境可省略)
修改后的完整脚本
# Stop Teams process Get-Process -ProcessName Teams -ErrorAction SilentlyContinue | Stop-Process -Force Start-Sleep -Seconds 3 Write-Host "Teams Process Successfully Stopped" # Clear Team Cache try{ $cachePaths = @( "$env:APPDATA\Microsoft\Teams\blob_storage", "$env:APPDATA\Microsoft\Teams\databases", "$env:APPDATA\Microsoft\Teams\cache", "$env:APPDATA\Microsoft\Teams\gpucache", "$env:APPDATA\Microsoft\Teams\Indexeddb", "$env:APPDATA\Microsoft\Teams\Local Storage", "$env:APPDATA\Microsoft\Teams\tmp" ) foreach ($path in $cachePaths) { if (Test-Path $path) { Get-ChildItem -Path $path -Recurse -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue } } Write-Host "Teams Cache Cleaned" } catch{ Write-Host "Cache cleanup error: $_" } # Remove Credential from Credential manager cmdkey /list | ForEach-Object { if ($_ -match "Target:\s*(.*msTeams.*)") { cmdkey /del:$($matches[1].Trim()) } } # Remove Reg.Key $Regkeypath = "HKCU:\Software\Microsoft\Office\Teams" if (Test-Path $Regkeypath) { $homeUserUpn = Get-ItemProperty -Path $Regkeypath -Name HomeUserUpn -ErrorAction SilentlyContinue if ($homeUserUpn) { Remove-ItemProperty -Path $Regkeypath -Name HomeUserUpn -ErrorAction SilentlyContinue Write-Host "The registry value successfully removed" } else { Write-Host "The registry value does not exist" } } else { Write-Host "Registry path does not exist" } # Modify desktop-config.json $TeamsFolders = "$env:APPDATA\Microsoft\Teams" $SourceDesktopConfigFile = "$TeamsFolders\desktop-config.json" try{ if (Test-Path $SourceDesktopConfigFile) { $desktopConfig = Get-Content -Path $SourceDesktopConfigFile -Raw | ConvertFrom-Json $desktopConfig.isLoggedOut = $true $desktopConfig.upnWindowUserUpn = "" $desktopConfig.userUpn = "" $desktopConfig.userOid = "" $desktopConfig.userTid = "" $desktopConfig.homeTenantId = "" $desktopConfig.webAccountId = "" $desktopConfig | ConvertTo-Json -Compress | Set-Content -Path $SourceDesktopConfigFile -Force Write-Host "Modify desktop-Config.Json - Finished" } else { Write-Host "desktop-config.json not found" } } catch{ Write-Host "Failed to modify desktop-config.json: $_" } # Delete Cookies and storage.json files $filesToDelete = @( "$TeamsFolders\Cookies", "$TeamsFolders\storage.json" ) foreach ($file in $filesToDelete) { if (Test-Path $file) { Remove-Item -Path $file -Force -ErrorAction SilentlyContinue } } # Start Teams using VDI-recommended method if (Test-Path "$env:LOCALAPPDATA\Microsoft\Teams\Update.exe") { Start-Process -FilePath "$env:LOCALAPPDATA\Microsoft\Teams\Update.exe" -ArgumentList "--processStart Teams.exe" -NoNewWindow Start-Sleep -Seconds 3 Write-Host "Teams Process Successfully Started" } else { Write-Host "Update.exe not found, falling back to direct Teams.exe launch" Start-Process -File "C:\Program Files (x86)\Microsoft\Teams\current\Teams.exe" -ArgumentList '--processStart "Teams.exe"' -NoNewWindow Start-Sleep -Seconds 3 Write-Host "Teams Process Successfully Started" }
内容的提问来源于stack exchange,提问作者StraussKrone
相关产品推荐
相关产品推荐

