简体   繁体   中英

Powershell Script - How do I remove multiple packages at once?

I am trying expand my existant Script with better code. So i wanted to let the script proof if the software really is uninstalled or isn't. I tried to do that with foreach. Here's my code:

function deletesoftware2 () {
$ErrorActionPreference = 'SilentlyContinue'
$appname = @(
    #"*HP Support Assistant*",
    #"*HP Support Solutions Framework*",
    #"*Dell SupportAssist*",
    #"*Dell Command | Update*",
    "*NuGet*"
)
ForEach($app in $appname){
    Write-Host "Removing $app from System"
    Get-Package -Name $app | Uninstall-Package -ErrorAction SilentlyContinue | Out-Null
        Start-Sleep -Seconds 20
    Get-Package | Where-Object{$_.Name -like $app} | Format-Table name | Out-String -OutVariable software | Out-Null
    if ($software -match $app) {
        Write-Host "$app wasnt found or could not removed"

    }
    else {
        Write-Host "$app was removed"   
    }        
}
Start-Sleep -Seconds 10

}

Edit: If i run the script that is shown, it jumps directly into the else, even though the software isn't removed. I tried to replace the "$app" in the if directly to NuGet and it worked. But if i write host the $app variable, it says exactly Nuget, and that's what i want. The prolem is, that it still won't work.

Well that doesn't work how i want it to, i'd be glad if someone can help me! Thanks in advance!

The reason for this behavior is that Format-Table name | Out-String -OutVariable software Format-Table name | Out-String -OutVariable software will provide a bit unexpected results.

Let's use Microsoft Teams package as an example as it's available on my system.

$q = Get-Package | ? { $_.name -eq "Microsoft Teams" }
$q.Name
Microsoft Teams

$q.Name.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

So far, everything looks good. Now let's bring Format-Table into the game like so,

$q | Format-Table name | Out-String -OutVariable software | Out-Null
$software

Name
----
Microsoft Teams

$software.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ArrayList                                System.Object

Wait, what? The type is not same at all. That's because formatting as a table will create an array of strings. That will never match the simple string name of a package.

As for a fix, just use Get-Package again and assign its result in a variable. If there isn't such a package, the output will be equal to $null . Like so,

Get-Package -Name $app | Uninstall-Package...
$q= Get-Package | Where-Object{$_.Name -like $app}
if($q -eq $null) {
    Write-Host "$app was removed"
}
else {
    Write-Host "$q is still present"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM