简体   繁体   English

Powershell:卸载提示用户输入的软件

[英]Powershell: Uninstall Software that has prompts for user input

Does anybody know if it is possible to uninstall software through PowerShell if it prompts for user input?如果提示用户输入,是否有人知道是否可以通过 PowerShell 卸载软件? 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.我特别想卸载下载 PDFCreator 时安装的 PDFForge 工具栏。

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.我编写的脚本都没有失败,它们只是在运行时挂起,我相信是因为卸载过程要求用户输入才能继续。

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. PowerShell 不会与提示进行交互……您不能只是告诉它单击可执行文件中的“下一步”,因为它看不到它。

You can send keys to it though.不过,您可以将密钥发送给它。 You're really just using COM objects.您实际上只是在使用 COM 对象。

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.因此,首先,通过设置一个包含数组的变量来获取您的进程 ID,该数组的数据由您的进程名称定义。 Let's say your process is called, "Uninstall" and the process is ALREADY RUNNING:假设您的进程被称为“卸载”并且该进程已经在运行:

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

Start the COM:启动COM:

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

Bring the uninstallation program with this process ID to the front so we can send it keystrokes:将带有此进程 ID 的卸载程序放在最前面,以便我们可以向它发送按键:

$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:我选择了 5,但如果您的机器没有压力,2 可能就足够了:

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:假设第一个屏幕是“下一步”,您可以通过告诉 PowerShell 发送 ENTER 键并等待 5 秒钟来发送您的第一个命令:

$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.) (如果你不想要它$wshell.SendKeys('~')会发送 ENTER 键并立即移动到下一个命令。)

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 (eg 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).手动完成卸载,使用所有按键,对于您发送的任何按键,请注意在加载卸载的下一部分之前需要多长时间,并等待比脚本更长的时间(例如,如果它处理您的 ENTER 键瞬间,我会让它在发送下一个命令之前等待 3 或 5 秒。如果加载需要 5 秒,我会告诉它在发送下一个命令之前等待 10 秒)。

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 .大多数非命令只是分配给它们的键(意味着如果你想输入“K”你的命令就是$wshell.SendKeys('K') )你可以在这里获得特定键的纲要: https:// msdn.microsoft.com/en-us/library/office/aa202943(v=office.10).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM