I am having script to uninstall Veeam software from control panel through PowerShell. While during uninstall of last the last Veeam components I am getting below error:
You Cannot call a method on a null-valued expression PowerShell : line 15 char:1
+ $console.Uninstall()
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+FullyQualifiedErrorId : InvokeMethodOnNull
I have no idea, Experts help me to fix it. I am waiting for your reply. I have attached my script below.
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
The reason why you're getting that error is because after this:
Your
$console
variable is$null
meaning that there was no object found with the Name property equal to Veeam Backup & Replication Console:This could be easily fixed by adding a condition before trying to call the
uninstall
method.