Install_Update.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID f5c37daf-5ad6-443a-9a75-25937e860b59
 
.AUTHOR bikush
 
.COMPANYNAME
 
.COPYRIGHT
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 "This is to help IT admins to install WIndows Updates using Script"
 
 Gets a list of Updates to be installed and Triggers the installation of each KB one by one...
 
AUTHOR
Bindusar Kushwaha
Microsoft Senior Premier Field Engineer
bikush@microsoft.com
 
.PRIVATEDATA
 
.EXAMPLE
 
Install_Updates.ps1 -LogLocation C:\Windows\Temp -KBs KB12345 KB67890
#>


<#
 
.DESCRIPTION
 "This is to help IT admins to install WIndows Updates using Script"
 
#>
 

<#
DISCLAIMER STARTS
This Sample Code is provided for the purpose of illustration only and is not intended to be used in a #production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" #WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO #THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We #grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and #distribute the object code form of the Sample Code, provided that You agree:(i) to not use Our name, #logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to #include a valid copyright notice on Your software product in which the Sample Code is embedded; and #(iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or #lawsuits, including attorneys� fees, that arise or result from the use or distribution of the Sample Code."
"This sample script is not supported under any Microsoft standard support program or service. The #sample script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied #warranties including, without limitation, any implied warranties of merchantability or of fitness for a #particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in #the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, #without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or #documentation, even if Microsoft has been advised of the possibility of such damages"
DISCLAIMER ENDS
#>


###########################
#Defining Variables
Param(
    [Parameter(HelpMessage="Please provide the Logging Location where the log file for this activity will be generated.")]
    $LogLocation= "C:\Windows\Temp",

    [Parameter(HelpMessage="Please provide the list of KBs to be Installed.")]
    [String[]]$KBs= "KB12345"
)



Function Logging()
{

    <#
    .SYNOPSIS
    This function is used to configure the logging.
    .DESCRIPTION
    This function is used to configure the logging.
    .EXAMPLE
    Logging -T "Logging Started..."
    .NOTES
    NAME: Logging
    #>


    Param($T)
    $D=get-date -Format yyyyMMdd

    #CompletePath
    $Log = "$LogLocation\INstallingUpdates_$D.log"

    (Get-Date -Format yyyyMMdd-HHmmssffff)+" $T " >> $Log
}

Logging -T "Importing Module PSWindowsUpdate"
Import-Module PSWindowsUpdate

Logging -T "Installing Module PSWindowsUpdate"
Install-module pswindowsupdate -force

Logging -T "Got following Updates to install"
Logging -T $KBs

$updates = Get-WUInstall -MicrosoftUpdate -AcceptAll -UpdateType Software -Verbose
Logging -T "Found $($updates.count) update(s)" -ForegroundColor Green

$scannedKBs=$updates.KB
Logging -T "KBs obtained after scanning from MU are $scannedKBs"

Logging -T "Trying to install KBs given by Script Initiator"

foreach($KB in $KBs)
{
    Logging -T "Installing Update: $KB ..."
    sleep -Seconds 5
    
    try
    {
        Install-WUUpdates -Update $KB 
        Logging -T "Installation complete: $KB"
    }

    catch [system.exception]
    {
        Logging -T "Error: Failed to install update: $KB with exception: $($_.exception.message)"
    }
}

Logging -T "Done!!!"