xud6的笔记本

powershell

A 2 post collection


Powershell 卸载程序

 •  Filed under windows, powershell
function Uninstall-Application {
    $appName = $($args[0])
    Write "application to uninstall: $appName"
    $uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -Match $appName }
    $uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match $appName }

    if($uninstall32){
        if($uninstall32.QuietUninstallString){
            Write ("uninstall32 Quiet: " + $uninstall32.QuietUninstallString)
            Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninstall32.QuietUninstallString  -Wait
        }else{
            Write ("uninstall32 Normal: " + $uninstall32.UninstallString)
            Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninstall32.UninstallString  -Wait
        }
    }
    if($uninstall64){
        if($uninstall64.QuietUninstallString){
            Write ("uninstall64 Quiet: " + $uninstall64.QuietUninstallString)
            Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninstall64.QuietUninstallString  -Wait
        }else{
            Write ("uninstall64 Normal: " + $uninstall64.UninstallString)
            Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninstall64.UninstallString  -Wait
        }
    }
}

Uninstall-Application("application name*")

Powershell 操作 windows更新

 •  Filed under powershell, windows

定义更新搜索

$Criteria = "IsInstalled=0 and Type='Software'"

搜索可用的更新

$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$SearchResult = $Searcher.Search($Criteria).Updates

下载更新

$Session = New-Object -ComObject Microsoft.Update.Session
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $SearchResult
$Downloader.Download()

安装更新

$Installer = New-Object -ComObject Microsoft.Update.Installer
$Installer.Updates = $SearchResult
$Result = $Installer.Install()

检查是否需要重启

$Result.rebootRequired

PS脚本

function log($str){
    Write-Host $str
}

#Define update criteria.
$Criteria = "IsInstalled=0 and Type='Software'"

#Search for relevant updates.
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$SearchResult = $Searcher.Search($Criteria).Updates
$MissingPatches = @($SearchResult).length
log("$($MissingPatches) patches found")
$SearchResult | select Title,IsBeta,IsHidden,IsInstalled,MsrcSeverity | Format-Table -AutoSize

if($MissingPatches -gt 0){
    #Download updates.
    log("download updates")
    $Session = New-Object -ComObject Microsoft.Update.Session
    $Downloader = $Session.CreateUpdateDownloader()
    $Downloader.Updates = $SearchResult
    $Downloader.Download()

    #Install updates.
    log("install updates")
    $Installer = New-Object -ComObject Microsoft.Update.Installer
    $Installer.Updates = $SearchResult
    $Result = $Installer.Install()

    $Result
    #Reboot if required by updates.
    if($Result.rebootRequired){
        log("Reboot required")
    }else{
        log("Reboot not required")
    }
}else{
    log("no patch found")
}