powershell 从 PC 中删除软件

发布于 2024-12-21 07:13:03 字数 775 浏览 2 评论 0原文

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心旳酸楚 2024-12-28 07:13:03

您必须检查 ReturnValue 属性。当您通过管道连接到 Out-Null 时,您会抑制操作的输出,并且无法判断发生了什么,除非您发出第二次调用来查找它是否返回有问题的软件。

我建议使用 Filter 参数(而不是使用 Where-Object)来查询服务器上的软件。为了安全起见,您还应该将结果通过管道传输到 Foreach-Object cmdlet,您永远不知道由于匹配操作而返回了多少软件对象(并且您调用 Uninstall 方法,就好像结果是仅一个对象):

Get-WmiObject -Class Win32_Product -ComputerName $PCNumber -Filter "Name LIKE '%$softwareName%'" | Foreach-Object { 

     Write-Host "Uninstalling: $($_.Name)"

     $rv = $_.Uninstall().ReturnValue 

     if($rv -eq 0)
     {
        "$($_.Name) uninstalled successfully"
     }     # Changed this round bracket to a squigly one to prperly close the scriptblock for "if"
     else
     {
        "There was an error ($rv) uninstalling $($_.Name)"
     }
}

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 the Foreach-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):

Get-WmiObject -Class Win32_Product -ComputerName $PCNumber -Filter "Name LIKE '%$softwareName%'" | Foreach-Object { 

     Write-Host "Uninstalling: $($_.Name)"

     $rv = $_.Uninstall().ReturnValue 

     if($rv -eq 0)
     {
        "$($_.Name) uninstalled successfully"
     }     # Changed this round bracket to a squigly one to prperly close the scriptblock for "if"
     else
     {
        "There was an error ($rv) uninstalling $($_.Name)"
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文