首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在从控制面板卸载第三方应用程序的过程中,我得到了这个错误。不能在空值表达式PowerShell上调用方法。

在从控制面板卸载第三方应用程序的过程中,我得到了这个错误。不能在空值表达式PowerShell上调用方法。
EN

Stack Overflow用户
提问于 2021-06-02 20:22:13
回答 1查看 144关注 0票数 1

我有脚本卸载Veeam软件从控制面板通过PowerShell。在卸载最后一个Veeam组件的过程中,我得到了以下错误:

代码语言:javascript
复制
You Cannot call a method on a null-valued expression PowerShell : line 15 char:1
+ $console.Uninstall()
+ CategoryInfo         : InvalidOperation: (:) [], RuntimeException
+FullyQualifiedErrorId : InvokeMethodOnNull

我不知道,专家帮我修好了。我在等你的答复。我已经把我的剧本附在下面。

代码语言:javascript
复制
function uninstallvm
{
$veeamConsole = 'Veeam Backup & Replication Console'
$objects = Get-WmiObject -Class win32_product
$veeamComponents = $objects | where {$_.Name -like 'Veeam*' -and $_.Name -ne $veeamConsole}
foreach ($object in $veeamComponents) {
  Write-Output "Uninstalling $($object.Name)"
  $object.Uninstall()
}
Start-Sleep -Seconds 5
$console = $objects | where {$_.Name -eq $veeamConsole}
Write-Output "Uninstalling $($console.Name)"
$console.Uninstall() 
}
uninstallvm
EN

回答 1

Stack Overflow用户

发布于 2021-06-02 20:49:02

之所以会出现这个错误,是因为在此之后:

代码语言:javascript
复制
$console = $objects | where {$_.Name -eq $veeamConsole}

您的$console变量是$null,这意味着在名称属性中没有找到对象,该属性等于Veeam备份和复制控制台

代码语言:javascript
复制
PS /> $null.Uninstall()
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $null.Uninstall()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

在尝试调用uninstall方法之前添加一个条件可以很容易地解决这个问题。

代码语言:javascript
复制
function uninstallvm
{
    $veeamConsole = 'Veeam Backup & Replication Console'
    $objects = Get-WmiObject -Class win32_product
    $veeamComponents = $objects | where {$_.Name -like 'Veeam*' -and $_.Name -ne $veeamConsole}
    
    foreach ($object in $veeamComponents)
    {
        Write-Output "Uninstalling $($object.Name)"
        $object.Uninstall()
    }
    
    Start-Sleep -Seconds 5
    
    $console = $objects | where {$_.Name -eq $veeamConsole}
    
    if(-not $console)
    {
        return "No object found with name: $veeamConsole"
    }

    Write-Output "Uninstalling $($console.Name)"
    $console.Uninstall() 
}

uninstallvm
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67812056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档