Today I completed the Microsoft Virtual Academy Advanced Tools & Scripting with PowerShell 3.0 Jump Start course. If you haven’t looked at it yet, I realy recommend you do so, lots of good stuff there. So for the purpose of applying some of the stuff I’ve learned there regarding the use of -Whatif and -verbose messages i wrote the below script which downloads the sysinternals tools.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<# .Synopsis Download Sysinternals Tools .DESCRIPTION Download the Sysinternals tools from live.sysinternals.com to the local machine .NOTES version 1.0 .EXAMPLE Get-Sysinternals -Path C:\Data|sysinternals .EXAMPLE Get-Sysinternals.ps1 -Path C:\Data\Sysinternals -Whatif -verbose #> [CmdletBinding(SupportsShouldProcess=$true)] Param( [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,ParameterSetName="Directory", HelpMessage= 'The local download folder')] [String]$Directory ) Begin { Write-Verbose "Starting Sysinternals Tool download" $url="http://live.sysinternals.com/tools/" if ((Test-path -path $Directory) -eq $False) { Write-Verbose "Creating Directory $Directory" if ($PScmdlet.ShouldProcess("Creating folder $Directory","","")) {New-Item -ItemType Directory -Path $Directory} } $siuri = Invoke-WebRequest -Uri $url -EA SilentlyContinue if ($siuri.BaseResponse.StatusCode -eq "OK") { $sfiles = $siuri.Links | Where-Object {$_.href -ne '/'} | Select-Object -ExpandProperty innerText } Else { Write-Verbose "Unable to reach $url" Exit 1 } } Process { ForEach ($st in $sfiles) { $downloadfile = $url+$st #Write-Verbose -Message "Downloading $downloadfile to $Directory\$st" if ($PScmdlet.ShouldProcess("Downloading now $downloadfile to $Directory\$st","","")) {Start-BitsTransfer -Description "SysinternalsDownload - $st" -Source $downloadfile -Destination "$Directory\$st"} } } End { Write-Verbose "Download complete" } |