powershell 从 PC 中删除软件
(Get-WmiObject -Class Win32_Product -ComputerName $PCNumber -ErrorAction SilentlyContinue | Where-Object { $_.Name -match "$softwareName" }).Uninstall() | Out-Null
我有以下代码,可以完美运行。唯一的问题是我不知道该软件是否已被删除。这并没有告诉我,但下面的代码可以。
这种方法对我有用。
$software = Get-WmiObject -Class Win32_Product -ComputerName $PCNumber -ErrorAction SilentlyContinue | Where-Object { $_.Name -match "$softwareName" }
$soft = $software.Uninstall();
$n = $software.ReturnValue;
if ( $n -eq 0 ){
SOFTWARE HAS BEEN REMOVED.
}
我的问题是如何判断该软件是否已被删除。 使用此代码。
(Get-WmiObject -Class Win32_Product -ComputerName $PCNumber -ErrorAction SilentlyContinue | Where-Object { $_.Name -match "$softwareName" }).Uninstall() | Out-Null
(Get-WmiObject -Class Win32_Product -ComputerName $PCNumber -ErrorAction SilentlyContinue | Where-Object { $_.Name -match "$softwareName" }).Uninstall() | Out-Null
I have following code which works perfectly. The only problem is that I wont to know if the software has been removed or not.This doesn't tells me but the code below does.
This way works for me.
$software = Get-WmiObject -Class Win32_Product -ComputerName $PCNumber -ErrorAction SilentlyContinue | Where-Object { $_.Name -match "$softwareName" }
$soft = $software.Uninstall();
$n = $software.ReturnValue;
if ( $n -eq 0 ){
SOFTWARE HAS BEEN REMOVED.
}
my question is that how do i tell if the software has been removed or not.
using this code.
(Get-WmiObject -Class Win32_Product -ComputerName $PCNumber -ErrorAction SilentlyContinue | Where-Object { $_.Name -match "$softwareName" }).Uninstall() | Out-Null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须检查 ReturnValue 属性。当您通过管道连接到 Out-Null 时,您会抑制操作的输出,并且无法判断发生了什么,除非您发出第二次调用来查找它是否返回有问题的软件。
我建议使用 Filter 参数(而不是使用
Where-Object
)来查询服务器上的软件。为了安全起见,您还应该将结果通过管道传输到Foreach-Object
cmdlet,您永远不知道由于匹配操作而返回了多少软件对象(并且您调用 Uninstall 方法,就好像结果是仅一个对象):You have to check the ReturnValue property. When you pipe to
Out-Null
you are suppressing the output of the operation and there's no way to tell what happened, unless you issue a second call to find if it returns the software in question.I recommend using the Filter parameter (instead of using
Where-Object
) to query the software on the server. To be safe you should also pipe the results to theForeach-Object
cmdlet, you never know how many software objects you get back due to the match operation (and you call the Uninstall method as if the result is one object only):