Invoke-WebRequest – Download files with PowerShell – Improved script!

A few months further I’ve learned and read some new things about Powershell and also about this Cmdlet. This resultated in an improved script compared to my old post Invoke-WebRequest. I just want to share it so maybe you can benefit from it too. The script now uses the Join-Path and Split-Path Cmdlets and a Leaf parameter. This results in more efficiënt code. I’ve also written a small feature which measures the time it took to download the file and also I’ve builtin some error handling because IF the download fails I don’t want to see the message ‘.NET Framework 4.6 download completed’ rather something like this ‘Something went wrong’. The PowerShell language is amazingly powerful!

$Uri = "https://download.microsoft.com/download/C/3/A/C3A5200B-D33C-47E9-9D70-2F7C65DAAD94/NDP46-KB3045557-x86-x64-AllOS-ENU.exe"
$DownloadFileName = Split-Path $Uri -Leaf
$OutFile = Join-Path "$env:USERPROFILE" -ChildPath "Desktop"
$OutFile += "\$DownloadFileName"
$Timestamp = Get-Date
try 
{
Invoke-WebRequest -Uri $Uri -OutFile $OutFile
Write-Host "Download completed. It took $((Get-Date).Subtract($Timestamp).Minutes) minutes and $((Get-Date).Subtract($Timestamp).Seconds) seconds ."
}
catch
{
Write-Host "Something went wrong."
}
Advertisements

Create Application Pools and Websites via PowerShell

In my daily job I need to regularly create Application Pools in IIS Manager for our innovative products (portal software which integrates with our Dynamics Add-on for example). If you just need to create one that’s an easy and quick job. However sometimes I need to create at least 8 application pools! Ofcourse this is possible by using PowerShell :) and once again I love it. It makes my work more efficient, repeatable and the quality is more in control and consistent. I not going to paste a lot of code here but will explain only the concept so you can easily build something useful for yourself.

Global steps:

  1. Open Powershell ISE
  2. Import the ‘WebAdministration’ module
  3. Use the New-WebAppPool Cmdlet to create an Application Pool
  4. Use the New-Website Cmdlet to create a website

 

 

Invoke-WebRequest – Download files with PowerShell

The Invoke-WebRequest is a very useful and cool Cmdlet in my opinion. This Cmdlet enables you to download files from the internet. Why the need to use Invoke-WebRequest just to download a file? I can name some reasons but the main reason is because I perform repeatable tasks in my daily job as a Technical Consultant. So I’m striving to work as efficiënt as possible. As most of us experience: time is always an issue! For example: what if you need to perform 5 identical downloads every day on different machines every time to basically the same folder? I won’t do that manually everytime but I’m going to find or create a solution for it in order to simplify and automate the task and gain more time in the long run.

So, the first thing that came to my mind: Powershell.

I did some research on Technet. On Microsoft Technet I found this Cmdlet and more information about it ofcourse. After reading quickly through the page I didn’t saw any examples… I got unpatient :) So I immediately started the PowerShell ISE and just typed in the Cmdlet.

The Invoke-WebRequest cmdlet asked me an Uri. That sounds logical. So I entered the URI to the .NET Framework 4.6 Offline installer, because I needed it to download. I quickly found it that it didn’t produced the result I wanted. My goal was to download the file and to save it to my Desktop. So once again I checked the documentation on Technet. The Outfile parameter saves the output to a file. So after specifying a destination filename it all worked.

At the end I created a small script with some extra things like dynamic constructing the path to the current desktop and it resulted in a small script I now use a lot. I will share it so you may benefit or learn from it too:

PowerShell example: Download .NET Framework 4.6:

$DownloadURL="https://download.microsoft.com/download/C/3/A/C3A5200B-D33C-47E9-9D70-2F7C65DAAD94/NDP46-KB3045557-x86-x64-AllOS-ENU.exe"
$Path_Desktop="$env:USERPROFILE\Desktop"
$Path_DotNetSetup46="$Path_Desktop\NDP46-KB3045557-x86-x64-AllOS-ENU.exe"
# Download: Microsoft .NET Framework 4.6 (Offline Installer) for Windows Vista SP2, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2008 SP2 Windows Server 2008 R2 SP1, Windows Server 2012 and Windows Server 2012 R2. URL: https://www.microsoft.com/en-US/download/confirmation.aspx?id=48137
Invoke-WebRequest -Uri $DownloadURL -OutFile $Path_DotNetSetup46 -Verbose
Write-Output ".NET Framework 4.6 download completed."