Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Скриптовые языки администрирования Windows (http://forum.oszone.net/forumdisplay.php?f=102)
-   -   Powershell-скрипт очистки C:\Windows\Installer (http://forum.oszone.net/showthread.php?t=322618)

farag 11-01-2017 13:32 2702636

Powershell-скрипт очистки C:\Windows\Installer
 
Имеется скрипт на сайте: http://www.bryanvine.com/2015/06/pow...eaning-up.html. Как его переделать полностью на PS?

Kazun 11-01-2017 14:28 2702647

Код:

$msi = New-Object -ComObject WindowsInstaller.Installer
$savelist = @()
$products = $msi.GetType().InvokeMember("Products","GetProperty",$null,$msi,$null)
foreach($product in $products)
{
        $patches = $msi.GetType().InvokeMember("Patches","GetProperty",$null,$msi,@($product))
        foreach($patch in $patches)
        {
                $location = $msi.GetType().InvokeMember("PatchInfo","GetProperty",$null,$msi,@($patch,"LocalPackage"))
                $savelist += [pscustomobject]@{
                        ProductCode = $product
                        PatchCode = $patch
                        PatchLocation = $location
                }
        }
}

$filelocation = $savelist | select -ExpandProperty PatchLocation

#First pass to remove exact file names
dir C:\windows\Installer -file | ForEach-Object{
    $fullname = $_.FullName
    if($filelocation | Where-Object{$_ -like "*$fullname*"}){
        "Keeping $fullname"
    }
    else{
        Remove-Item $fullname -Force -Verbose
    }
}

#second pass to match product and patch codes
dir C:\windows\Installer -Directory | ForEach-Object{
    $fullname = $_.name
    if($savelist | Where-Object{$_.ProductCode -like "*$fullname*" -or $_.PatchCode -like "*$fullname*" }){
        "Keeping $fullname"
    }
    else{
        Remove-Item $_.fullname -Force -Verbose -Recurse
    }
}


farag 11-01-2017 16:22 2702678

Спасибо большое, но такой вопрос назрел: на виртуалке после очередного запуска его скрипта, папка очистилась совсем. В чем может быть дело?


Время: 21:23.

Время: 21:23.
© OSzone.net 2001-