I am trying to run a script that imports CSV info into PowerShell to uninstall software on a remote machine. The CSV file contains info like hostname, IP address and name of installed applications. The CSV file looks like this (sorry for bad input):
Name,Serial Number,IP Address,MAC Address,Installed Applications computer1,ABC123,1.1.1.1,12:34:45:67:89,Adobe Air
Basically, the idea is to uninstall the "Installed Application" (Adobe Air for this example) on the "Name" (computer hostname).
The PowerShell script:
$csv = Import-Csv C:\path\report.csv
$DisplayName = $csv."Installed Applications"
$path = Split-Path -Path $MyInvocation.MyCommand.Path
$computers = $csv.Name
foreach ($server in $computers) {
$app = Get-WmiObject -Class Win32_Product -ComputerName $server |
Where-Object {$_.DisplayName -match $csv."Installed Applications"}
$pathobj = New-Object -TypeName PSobject
$pathobj | Add-Member -MemberType NoteProperty -Name Computername -Value $server.ToUpper()
$pathobj | Add-Member -MemberType NoteProperty -Name Software -Value $csv."Installed Applications"
$var = ($app.Uninstall()).ReturnValue
if($var -eq 0) {
$pathobj | Add-Member -MemberType NoteProperty -Name Status -Value "Successfully Uninstalled"
$pathobj | Add-Member -MemberType NoteProperty -Name Date -Value $(Get-Date)
} else {
$pathobj | Add-Member -MemberType NoteProperty -Name Status -Value "Unable to uninstall"
$pathobj | Add-Member -MemberType NoteProperty -Name Date -Value $(Get-Date)
}
#Write-Output $pathobj | Export-Csv "$path\ObsoleteStatus.csv" -NoTypeInformation -Append
}
When I execute it, it always gives me:
You cannot call a method on a null-valued expression. At C:\Users\sbellec\Desktop\test5.ps1:18 char:28 + $var = ($app.Uninstall <<<< ()).ReturnValue + CategoryInfo : InvalidOperation: (Uninstall:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
I have tried different things to fix it with no results. Any ideas on what I am doing wrong?