解析输出以删除适当的文件

时间:2019-04-26 12:07:40

标签: powershell

以下脚本的任务是删除Windows的DriverRepository中的较旧版本的驱动程序。已对其进行了修改,以过滤特定供应商(柯尼卡美能达)的驱动程序。

我想删除所有的Konica Minolta驱动程序,无论它们是否旧,这是我遇到的困难。该脚本返回三个Konica打印机驱动程序,但随后该脚本进行检查以查看它们是否旧/重复等,这不是必需的。我只需要删除过滤器最初返回的任何内容,例如所有Konica驱动程序。

$dismOut = Dism /Online /Get-Drivers
$Lines = $dismOut | select -Skip 10  #Discard the first ten lines
$Operation = "theName"
$Drivers = @()

foreach ( $Line in $Lines ) {
    $tmp = $Line
    $txt = $($tmp.Split( ':' ))[1]

    switch ($Operation) {

        'theName' { $Name = $txt
                    $Operation = 'theFileName'
                    break
                  }

        'theFileName' { $FileName = $txt.Trim()
                        $Operation = 'theInbox'
                        break
                      }


        'theInbox' { $Inbox = $txt.Trim()
                    $Operation = 'theClassName'
                    break
                  }

        'theClassName' { $ClassName = $txt.Trim()
                         $Operation = 'theVendor'
                         break
                       }

        'theVendor' { $Vendor = $txt.Trim()
                      $Operation = 'theDate'
                      break
                    }

        'theDate' { # change the date format for easy sorting

                     $tmp = $txt.split( '.' )
                     $txt = "$($tmp[2]).$($tmp[1]).$($tmp[0].Trim())"
                     $Date = $txt
                     $Operation = 'theVersion'
                     break
                   }

        'theVersion' { $Version = $txt.Trim()
                       $Operation = 'theNull'
                       $params = [ordered]@{ 'FileName' = $FileName
                                             'Vendor' = $Vendor
                                             'Date' = $Date
                                             'Name' = $Name
                                             'ClassName' = $ClassName
                                             'Version' = $Version
                                             'Inbox' = $Inbox
                                           }


                        $obj = New-Object -TypeName PSObject -Property $params
                        $Drivers += $obj
                        break
                      }

         'theNull' { $Operation = 'theName'
                    break
                   }

    }

}


Write-Host "ALL INSTALLATION THIRD-PARTY DRIVERS"

$Drivers | sort Filename | where Vendor -eq "KONICA MINOLTA" | ft

Write-Host "Different Versions:"

$last = ''

$NotUnique = @()

foreach ( $Dr in $($Drivers | sort Filename | where Vendor -eq "KONICA MINOLTA") ) {

    if ($Dr.FileName -eq $last  ) {  $NotUnique += $Dr  }

    $last = $Dr.FileName

}

$NotUnique | sort FileName | where Vendor -eq "KONICA MINOLTA" | ft



Write-Host "Outdated Drivers:"

$list = $NotUnique | select -ExpandProperty FileName -Unique

$ToDel = @()

foreach ( $Dr in $list ) {

    Write-Host "Duplicate found..." -ForegroundColor Yellow

    $sel = $Drivers | where { $_.FileName -eq $Dr } | sort date -Descending | select -Skip 1

    $sel | ft

    $ToDel += $sel

}

Write-Host "DRIVERS TO REMOVE:" -ForegroundColor Red

$ToDel | ft

# removing old drivers

foreach ( $item in $ToDel ) {

    $Name = $($item.Name).Trim()

    Write-Host "deleting $Name" -ForegroundColor Yellow

    Write-Host "pnputil.exe -d $Name" -ForegroundColor Yellow

   # Invoke-Expression -Command "pnputil.exe -d $Name"

}

我已注释掉实际上执行删除操作的底部一行,因此实际上没有删除任何内容。

1 个答案:

答案 0 :(得分:0)

要删除驱动程序,您需要使用类似...的方式再次调用DISM。

pnputil /delete-driver <driver>

此行返回柯尼卡驱动程序...

$Drivers | sort Filename | where Vendor -eq "KONICA MINOLTA" | ft

您可以调整该行以获得所需的内容...

foreach ($driver in $($Drivers | where Vendor -eq "KONICA MINOLTA")) {
    pnputil /delete-driver $Driver[0].FileName
}

您可能不得不玩对pnputil的调用; this可能会有所帮助。我无法测试。

此外,还有一个本机Powershell DISM模块。有关详细信息,请参见here。这样,您可以调用Get-WindowsDriverRemove-WindowsDriver cmdlet。如果pnpUtil或dsim没有执行您想要的操作,则可能需要看看它们。