我正在研究一种从注册表读取UninstallStrings软件的方法。然后我尝试执行这些字符串来卸载软件。
当我打印出保存字符串信息的变量时,它会正确地打印整个字符串(带有参数),但我无法运行这些字符串。这个问题有多个部分。
有些安装字符串的格式如下
c:\file path\blah\app.exe /uninstall
"c:\file path\blah\app.exe" /uninstall
c:\file path\blah\app.exe --uninstall
'c:\file path\blah\app.exe' /uninstall我想要做的是找出最佳的方法,以便能够以“通用”的方式运行卸载程序。有办法有效地做到这一点吗?
我试着用两种不同的方式执行字符串。
& $uninstaller和
Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninstaller -Wait 两者似乎都不起作用。没有错误,但它们似乎没有运行,因为当我检查应用程序时,它仍在安装。
我试着把课文分成几个部分。
$Uninstaller.split("/")[0]
$Uninstaller.split("/",2)[1]
$($Uninstaller) | Invoke-Expression
$Uninstaller.Substring(0,$Uninstaller.lastIndexOf('.exe '))
$Uninstaller.split('^(*?\.exe) *')提前感谢!
发布于 2020-05-26 21:02:12
弄明白了。也许有更好的方法,但这似乎对我有用。
CLS
$Software = "OneDrive"
$Filter = "*" + $Software + "*"
$Program = $ProgUninstall = $FileUninstaller = $FileArg = $NULL
try
{
if (Test-Path -Path "HKLM:\SOFTWARE\WOW6432Node")
{
$programs = Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction Stop
}
$programs += Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction Stop
$programs += Get-ItemProperty -Path "Registry::\HKEY_USERS\*\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction SilentlyContinue
}
catch
{
Write-Error $_
break
}
foreach($Program in $Programs)
{
$ProgDisplayName = $Program.DisplayName
$ProgUninstall = $Program.UninstallString
if($ProgDisplayName -like $Filter)
{
if($ProgUninstall -like "msiexec*")
{
$FileUninstaller = $ProgUninstall.split(" ")[0]
$FileArg = ($($ProgUninstall).split(" ",2)[1])
}
else
{
if(($ProgUninstall -like '"*"*') -or ($ProgUninstall -like "'*'*"))
{
#String has quotes, don't need to do anything
}
else
{
if($NULL -ne $ProgUninstall)
{
#String doesn't have quotes so we should add them
$ProgUninstall = '"' + ($ProgUninstall.Replace('.exe','.exe"'))
}
}
#Let's grab the uninstaller and arguments
$FileUninstaller = $ProgUninstall.split('"')[1]
$FileArg = $ProgUninstall.split('"')[-1]
}
#Debug
#$FileUninstaller
#$FileArg
#Run the Uninstaller
Start-Process $FileUninstaller -ArgumentList $FileArg -wait -ErrorAction SilentlyContinue
}
}发布于 2020-05-26 21:44:51
这对于msi安装非常容易:
get-package *whatever* | uninstall-package下面是一个非msi静默卸载示例,但您必须添加"/S“或任何静默卸载选项:
get-package notepad++* |
% { & $_.Meta.Attributes['UninstallString'] /S }https://stackoverflow.com/questions/62023960
复制相似问题