0
votes

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?

1
Looks like you didn't return any applications that matched your $csv. So $app is $null which means it doesn't have an uninstall method. You could ForEach-Object loop on $app which would handle having no matches and multiple matches. - BenH
Please print out the $app variable before calling .Uninstall() on it. If its not what you expect, move up the the variable declaration chain and see where you did something wrong by just printing variables and results of operations. I suspect this line to be BS: Where-Object {$_.DisplayName -match $csv."Installed Applications"} - Mindaugas Bernatavičius
The data you posted isn't comma-separated in the first place, so your code shouldn't even get to the point where it could throw that error. Please show us a minimal reproducible example and the error that code produces. - Ansgar Wiechers
Thanks for all the comment. I will try what is suggested and let you know. @Angar: the CSV format is comma-separated, I had to modify the format for this message. - simonb

1 Answers

0
votes
$computers = $csv.Name

selects one column only, so when you run your foreach, you're iterating within this column;

$_.DisplayName -match $csv."Installed Applications"

in the same way checks wheather DisplayName matches the column of Installed Applications, so you're not getting any sensible outcome there.

You just need to run your foreach on the whole csv array

foreach ($row in $csv) {

and then use $row.Name and $row."Installed Applications" for the values corresponding to the same server row.