Powershell: Uninstall Software that has prompts for user input

╄→尐↘猪︶ㄣ 提交于 2019-12-29 02:12:09

问题


Does anybody know if it is possible to uninstall software through PowerShell if it prompts for user input? I have multiple scripts that can uninstall just about anything, except for the one software that I need it to uninstall. I'm specifically trying to uninstall PDFForge toolbar which is installed when downloading PDFCreator.

None of the scripts that I've written fail, they just hang when run, I believe because the uninstall process is asking for user input to proceed.


回答1:


PowerShell isn't going to interact with the prompts... you can't just tell it to click "Next" in an executable because it can't see it.

You can send keys to it though. You're really just using COM objects.

So, first, get your process ID by setting a variable that contains an array, the data for which is defined by the name of your process. Let's say your process is called, "Uninstall" and the process is ALREADY RUNNING:

$a = Get-Process | ?{$_.Name -eq "Uninstall"}

Start the COM:

$wshell = New-Object -ComObject wscript.shell;

Bring the uninstallation program with this process ID to the front so we can send it keystrokes:

$wshell.AppActivate($a.id)

Give it a few seconds to bring that window forward. I chose 5, but if your machine isn't stressed, 2 is probably enough:

Start-Sleep 5

Now start telling it what keys you want to send. The syntax here is this: whatever is in the () is what will be sent. The position in the single-quote is the keystroke to send, after the comma is how long you want it to wait before proceeding. Assuming the first screen is "Next" you can send your first command by telling PowerShell to send the ENTER key and wait 5 seconds:

$wshell.SendKeys('~',5)

The wait function is optional, but for your purposes, you're definitely going to want it. (If you don't want it $wshell.SendKeys('~') would send the ENTER key and immediately move to the next command.)

Walk through the uninstallation yourself manually, using all keystrokes, and for any keystroke you send, pay attention to how long it takes before the next part of uninstallation is loaded, and wait longer than that with your script (e.g. if it processes your ENTER key instantaneously, I'd have it wait 3 or 5 seconds before it sends the next command. If it takes 5 seconds to load, I'd tell it to wait 10 seconds before sending the next command).

Letters are letters, and numbers are numbers. Most non-commands are just assigned to their keys (meaning if you want to type "K" your command would just be $wshell.SendKeys('K')) You can get the rundown for the specific keys here: https://msdn.microsoft.com/en-us/library/office/aa202943(v=office.10).aspx.



来源:https://stackoverflow.com/questions/33650913/powershell-uninstall-software-that-has-prompts-for-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!