Download A Few Files from Azure Blob Storage

Azure Blob Storage is a great and cheap place to upload a lot of files.

One of the websites I am currently work on, stores all the PDFs that users can download, in an Azure Blob Storage container.

We needed to pull down a large amount of those PDFs to confirm the content in them.

So I wrote the following PowerShell:

$LocalTargetDirectory = "D:\Temp\FilesDownloaded\"
$StorageAccountName = "YourStorageAccountName"
$StorageAccountKey = "YourStorageAccountKey"
$ContainerName = "YourContainerName"
$Blobs = @("file1","file2")

New-Item -Force -ItemType directory -Path $LocalTargetDirectory
$Context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

foreach($Blob in $Blobs)
{
Get-AzureStorageBlobContent -Blob $Blob -Container $ContainerName -Destination $LocalTargetDirectory -Context $Context
}

To get this to work for you, replace:
LocalTargetDirectory with where you want to download the files to.
StorageAccountName with the name of the Azure Storage Account Name.
StorageAccountKey with the name of the Azure Storage Account Key.
ContainerName with the container that you have the files saved in.
Blobs is an array of the files you want to download from the container.

Check a list of urls in PowerShell to see what are valid

Recently, one of my colleagues was asked to check what of our products had images on our website and didn’t.

Being a typical developer, I didn’t like the idea of someone wasting energy on monotonous tasks like that when a machine could do it so much more accurately.

The outline of the code I used was:

$urls = @("http://www.google.com","http://www.google.com.au","http://this.doesnt.exist")

foreach($url in $urls)
{
$HTTP_Request = [System.Net.WebRequest]::Create($url)

Try
{
 $HTTP_Response = $HTTP_Request.GetResponse()
 $HTTP_Status = [int]$HTTP_Response.StatusCode
}
catch [System.Net.WebException]
{
 Write-Host "$url is invalid."
}

$HTTP_Response.Close()
}

The output for the above is: http://this.doesnt.exist is invalid.

This was a great start.
To complete this for the task at hand, I enhanced the code to populate the variable $urls with a list of all our products attached to the base image path.
This reduced the amount of time from hours to minutes to find all the missing images.

NuGet- Failed to initialize the PowerShell host issues

I recently had issues updating some NuGet packages in my solution at work, it was giving me the following error:

“Failed to initialize the PowerShell host. If your PowerShell execution policy setting is set to AllSigned, open the Package Manager Console to initialize the host first.”

nuget fail

There is a really simple fix for this.

1.Click start and type in powershell.
2.Right click “Windows PowerShell (x86)” then click run as administrator
3.Type in: “Set-ExecutionPolicy Unrestricted” (without the quotes) and hit enter.

Finally restart Visual Studio and try adding that NuGet package again.