PowerShell Function: Get-Sysinternals

    ##--------------------------------------------------------------------------
    ##  FUNCTION.......:  Get-Sysinternals
    ##  PURPOSE........:  Downloads or updates the Sysinternal Utilities.
    ##  REQUIREMENTS...:  PowerShell 2.0
    ##  NOTES..........:  
    ##--------------------------------------------------------------------------
    Function Get-Sysinternals {
        <#
        .SYNOPSIS
         Downloads or updates the Sysinternals Utilities. For examples type:
         Get-Help Get-Sysinternals -examples
        .DESCRIPTION
         The Get-Sysinternals Function uses file timestamps to determine if 
         there are updates or (optionally) new utilities available from 
         live.sysinternals.com, if there are they will be downloaded and saved 
         in the specified directory.
         This function will also log all output to whatever directory is 
         specified by the -Path parameter (by default this is 
         "c:\admin\tools\logfile.txt").
        .PARAMETER <Path>
         This optional parameter defines the location of your Sysinternals 
         Utilities, or where they will be saved if none are present. The default
         value is "c:\admin\tools". If you specify a path that does not exist, 
         it will be created. If a trailing slash is used in the path, the 
         operation will fail.
        .PARAMETER <getnew>
         This optional parameter will get any utilities that are available on 
         live.sysinternals.com, which do not exist in the specified directory.
        .EXAMPLE
         C:\PS>Get-Sysinternals
         
         This example will check the directory "c:\admin\tools" for existing 
         Sysinternals utilities and update any with newer verisons.
         
        .EXAMPLE
         C:\PS>Get-Sysinternals c:\sysinternals
         
         This example will do the same thing as Example 1, but using the 
         "c:\sysinternals" folder.
         
        .EXAMPLE
         C:\PS>Get-Sysinternals c:\sysinternals -verbose
         
         This example will do the same thing as Example 2, but will also write 
         verbose output to the console.
         
        .EXAMPLE
         C:\PS>Get-Sysinternals c:\sysinternals -getnew -verbose
         
         This example will do the same thing as Example 3, but will also 
         download any utilities that are available on live.sysinternals.com, 
         but which do not exist in the specified path.
        
        .NOTES
         NAME......:  Get-Sysinternals
         AUTHOR....:  Joe Glessner
         LAST EDIT.:  24MAY12
         CREATED...:  11APR11
        .LINK
         https://joeit.wordpress.com/
        #>
        [CmdletBinding()]
        Param(
            [Parameter(Mandatory=$False, 
                        Position=0,                           
                        ValueFromPipeline=$false,             
                        ValueFromPipelineByPropertyName=$false)]
            [String]$Path = "c:\admin\tools",
            [Switch]$GetNew
        )#END: Param
        $SysIn = "\\live.sysinternals.com\tools"
        If ($PSBoundParameters['Verbose']) {
            net use $SysIn /persistent:no;
            New-PSDrive -Name SYS -PSProvider filesystem -Root $SysIn
        }#END: If ($PSBoundParameters['Verbose'])
        Else {
            Net Use $SysIn /persistent:no | Out-Null;
            New-PSDrive -Name SYS -PSProvider filesystem -Root $SysIn | out-null
        }#END: Else
        If(!(Test-Path -Path $Path -PathType Container)) {
            $null = New-Item -Type Directory -Path $Path -Force
        }#END: If(!(Test-Path...
        $log = join-path $Path "logFile.txt"
        Write-Verbose "Logging to: $log"
        Add-Content -force $log -value "`n`n[$(get-date)]Update started..."
        Get-ChildItem SYS: -recurse | ForEach {
            $fileName = $_.name
            $localFile = join-path $Path $_.name
            $exist = test-path $localFile
            $msgNew = "  ** New utility found: $fileName , downloading..."
            $msgUpdate = "  ** Update found for: $fileName , downloading..."
            $msgNoChange = "    No update for: $fileName"
            If($exist) {
                If($_.lastWriteTime -gt (Get-Item $localFile).lastWriteTime) {
                    Copy-Item $_.fullname $Path -force
                    Write-Verbose $msgUpdate
                    Add-Content -force $log -value $msgUpdate
                 }#END: ($_.lastWriteTime...
                Else {
                    Add-Content $log -force -value $msgNoChange
                    Write-Verbose $msgNoChange
                 }#END: Else
            }#END: If($exist)
            If($GetNew) {
                If(!($exist)) {
                    If($_.extension -eq ".exe") {
                          Write-Verbose $msgNew
                          Add-Content -force $log -value $msgNew
                     }#END: If($_.extension -eq ".exe")
                    Copy-Item $_.fullname $Path -force
                }#END: If(!($exist))
            }#END: If($GetNew)
        }#END: Get-ChildItem SYS: -recurse | ForEach
        Write-Verbose "[$(get-date)]Update completed."
        Add-Content -force $log -value "`n`n[$(get-date)]Update completed."
    }#END: Function Get-Sysinternals

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: