VBScript卸载Windows应用程序

时间:2013-08-19 13:07:54

标签: c# visual-studio-2010 vbscript

我尝试使用VBScript卸载已安装在我的系统中的Windows应用程序exe。但是无法卸载exe。请帮帮我。提前谢谢。

我尝试使用以下代码:

Dim oReg, oShell, oFSO 
Dim UninstallString, ProductCode
Dim strComputer, colItems, objWMIService, objItem
Dim strKeyPath, subkey, arrSubKeys
strComputer = "." 

'********************************
'Enter Product Code Of The Application Here That You Want To Uninstall within the    Bracket 
ProductCode = "{XXXXC6BA-0F96-4E3B-BB14-211E2805XXXX}" 

'********************************

' Get scripting objects needed throughout script.
Set oShell = CreateObject("WScript.Shell")

'**************************
UninstallString = "Database Upgrade Utility.exe /X" & ProductCode & " /qn" & "   /norestart"

Const HKEY_LOCAL_MACHINE = &H80000002

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_ 
strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

For Each subkey In arrSubKeys 

  IF subkey = ProductCode Then 
    oShell.Run UninstallString, 1, True
  End If

Next

Set oShell = Nothing
Set oReg = Nothing

修改后的代码

Dim oReg, oShell, oFSO 
Dim UninstallString, ProductCode
Dim strComputer, colItems, objWMIService, objItem
Dim strKeyPath, subkey, arrSubKeys
strComputer = "." 

'********************************
'Enter Product Code Of The Application Here That You Want To Uninstall within the Bracket 
ProductCode = "{4AE9C6BA-0F96-4E3B-BB14-211E2805227E}" 

'********************************

' Get scripting objects needed throughout script.
Set oShell = CreateObject("WScript.Shell")

'**************************
UninstallString = """C:\Program Files\ASCO\DatabaseUpgradeUtility\ASCO Database Upgrade Utility.exe"" /X" & ProductCode & " /qn /norestart"
'UninstallString = "ASCO Database Upgrade Utility.exe /X" & ProductCode & " /qn" & " /norestart"
InputBox(UninstallString)
Const HKEY_LOCAL_MACHINE = &H80000002

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_ 
strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

For Each subkey In arrSubKeys 

  'IF subkey = ProductCode Then 
  '.Run UninstallString, 1, True
  'End If
  IF subkey = ProductCode Then
    oShell.Run "%COMSPEC% /k " & UninstallString, 1, True
  End If

Next

Set oShell = Nothing
Set oReg = Nothing

尝试了上面的内容,路径也用双引号进行了trid,但两者都没有用。如果我需要在上面的脚本中更改任何内容,请提供给我。

1 个答案:

答案 0 :(得分:0)

您的可执行文件名中包含空格,因此您需要在其周围加上双引号,否则shell对象将尝试运行无法找到的可执行文件Database

更改此行:

UninstallString = "Database Upgrade Utility.exe /X" & ProductCode & " /qn" & "   /norestart"

进入这个:

UninstallString = """Database Upgrade Utility.exe"" /X" & ProductCode & " /qn /norestart"

另外,请确保Database Upgrade Utility.exe的路径位于PATH环境变量中。如果不是,则需要以完整路径运行可执行文件。

UninstallString = """C:\Program Files\ASCO\DatabaseUpgradeUtility\ASCO Database Upgrade Utility.exe"" /X" & ProductCode & " /qn /norestart"

如果不起作用,请检查以下事项:

  • 首先执行Run语句?像这样更改条件以查看代码是否实际进入Then分支:

    IF subkey = ProductCode Then
      WScript.Echo "Subkey check OK."
      oShell.Run UninstallString, 1, True
    End If
    
  • uninstall命令是否返回错误代码?

    IF subkey = ProductCode Then
      rc = oShell.Run(UninstallString, 1, True)
      If rc <> 0 Then WScript.Echo "Command returned with status code " & rc & "."
    End If
    
  • uninstall命令是否在控制台上产生输出?

    IF subkey = ProductCode Then
      oShell.Run "%COMSPEC% /k " & UninstallString, 1, True
    End If
    
相关问题