我正在創建一個腳本來從多個位置卸載 Firefox。我有一個我創建的腳本,它在一定程度上有效
Set-ExecutionPolicy unrestricted -force -ErrorAction SilentlyContinue
$Folders = Get-ChildItem -Path "C:\Users"
if (Test-Path "${env:ProgramFiles(x86)}\Mozilla Firefox\uninstall\helper.exe"){
Start-Process -FilePath "${env:ProgramFiles(x86)}\Mozilla Firefox\uninstall\helper.exe" -ArgumentList '/S' -ErrorAction SilentlyContinue
}elseif (Test-Path "${env:ProgramFiles}\Mozilla Firefox\uninstall\helper.exe"){
Start-Process -FilePath "${env:ProgramFiles}\Mozilla Firefox\uninstall\helper.exe" -ArgumentList '/S' -ErrorAction SilentlyContinue
}
## Uninstalling for each user
ForEach ($Folder in $Folders){
$Userpath = "C:\Users\" $folder.name
if (Test-Path "$Userpath\AppData\Local\Mozilla Firefox\uninstall\helper.exe"){
Start-Process -FilePath "$Userpath\AppData\Local\Mozilla Firefox\uninstall\helper.exe" -ArgumentList '/S' -ErrorAction SilentlyContinue
}
Start-Sleep 5
##Remove shortcuts from appdata
Remove-Item -Path "$userpath\AppData\Local\Mozilla" -Force -Recurse -ErrorAction SilentlyContinue
Remove-Item -Path "$userpath\desktop\firefox.lnk" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$userpath\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" -Force -ErrorAction SilentlyContinue
}
if (Test-Path HKLM:\Software\Mozilla){
Remove-Item -Path HKLM:\SOFTWARE\Mozilla -Force -Recurse -ErrorAction SilentlyContinue
}elseif (Test-Path HKLM:\SOFTWARE\mozilla.org){
Remove-Item -Path HKLM:\SOFTWARE\mozilla.org -Force -Recurse -ErrorAction SilentlyContinue
}elseif (Test-Path HKLM:\SOFTWARE\MozillaPlugins){
Remove-Item -Path HKLM:\SOFTWARE\MozillaPlugins -Force -Recurse -ErrorAction SilentlyContinue
}elseif (Test-Path HKLM:\SOFTWARE\WOW6432Node\Mozilla){
Remove-Item -Path HKLM:\SOFTWARE\WOW6432Node\Mozilla -Force -Recurse -ErrorAction SilentlyContinue
}elseif (HKLM:\SOFTWARE\WOW6432Node\mozilla.org){
Remove-Item -Path HKLM:\SOFTWARE\WOW6432Node\mozilla.org -Force -Recurse -ErrorAction SilentlyContinue
}elseif (Test-Path HKLM:\SOFTWARE\WOW6432Node\MozillaPlugins){
Remove-Item -Path HKLM:\SOFTWARE\WOW6432Node\MozillaPlugins -Force -Recurse -ErrorAction SilentlyContinue
}elseif (Test-Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Firefox.lnk"){
Remove-Item -Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" -Force -ErrorAction
}
我無法處理的腳本的唯一部分是洗掉注冊表項。我需要通過 Intune 運行腳本,所以我被告知“腳本的狀態基于最后一個命令的最后一個回傳碼。所以如果 HKLM\Software\WOW6432Node\MozillaPlugins 不存在,它會嘗試洗掉它, 會回傳錯誤碼 1, not found. 最好對每個 remove-item 使用 test-path , 這樣每行的回傳碼都是 0 除非洗掉失敗, 這種情況下腳本的失敗狀態是合法的。”
簡單地說,想要測驗每個注冊表項的路徑是否存在,然后如果不移動到下一個則將其洗掉,依此類推。
如果我運行 (Test-Path HKLM:\Software\Mozilla) 它會回傳 true,但作為一個整體運行腳本它不會洗掉注冊表項。
uj5u.com熱心網友回復:
我假設問題是您的鏈式if\ elseif\else條件,可能發生的情況是,如果第一個條件是$true您僅洗掉第一個注冊表項,然后退出鏈式條件(這是設計使然):
# only results in 'hello if' and then exits the chained conditions
if($true) {
'hello if'
}
elseif($true) {
'hello elseif'
}
在這種情況下,您可以做的是將所有路徑存盤在一個陣列中,然后回圈遍歷它們,測驗路徑是否存在,如果存在,則將其洗掉:
$pathToRemove = @(
'HKLM:\Software\Mozilla'
'HKLM:\SOFTWARE\mozilla.org'
'HKLM:\SOFTWARE\MozillaPlugins'
'HKLM:\SOFTWARE\WOW6432Node\Mozilla'
'HKLM:\SOFTWARE\WOW6432Node\mozilla.org'
'HKLM:\SOFTWARE\WOW6432Node\MozillaPlugins'
'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Firefox.lnk'
)
foreach($path in $pathToRemove) {
if(Test-Path $path) {
try {
Write-Verbose "Attempting to remove: $path" -Verbose
Remove-Item $path -Recurse -Force
Write-Verbose "Successfully removed: $path" -Verbose
}
catch {
Write-Warning $_.Exception.Message
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/448706.html
標籤:电源外壳 火狐 调音 microsoft365-defender
