我有脚本卸载Veeam软件从控制面板通过PowerShell。在卸载最后一个Veeam组件的过程中,我得到了以下错误:
You Cannot call a method on a null-valued expression PowerShell : line 15 char:1
+ $console.Uninstall()
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+FullyQualifiedErrorId : InvokeMethodOnNull我不知道,专家帮我修好了。我在等你的答复。我已经把我的剧本附在下面。
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发布于 2021-06-02 20:49:02
之所以会出现这个错误,是因为在此之后:
$console = $objects | where {$_.Name -eq $veeamConsole}您的$console变量是$null,这意味着在名称属性中没有找到对象,该属性等于Veeam备份和复制控制台
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方法之前添加一个条件可以很容易地解决这个问题。
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()
}
uninstallvmhttps://stackoverflow.com/questions/67812056
复制相似问题