Showing posts with label Azure. Show all posts
Showing posts with label Azure. Show all posts

Friday, April 5, 2013

Windows Azure storage through Powershell

This is just a first stab at manipulating Windows Azure Storage via Powershell.

Get Microsoft.WindowsAzure.Storage.dll using NuGet 
  1. Navigate to this by first going to the Windows Azure Downloads site.
  2. Select .NET. 
  3. Select Client libraries under Resources.
  4. Instructions on how to download Windows Azure Storage are found here. 
Here is the code I'm using to download files from blob storage

Param (
    [Parameter(Mandatory=$true)] [String] $StorageAccountName,
    [Parameter(Mandatory=$true)] [String] $AccessKey,
    [Parameter(Mandatory=$true)] [String] $BlobFilename,
    [Parameter(Mandatory=$true)] [String] $LocalFilename,
    [String] $StorageAccountEndpoint = "http://$storageAccountName.blob.core.windows.net/"
)

Add-Type -Path '.\Microsoft.WindowsAzure.Storage.dll'

$BlobFilename = $BlobFilename.Replace('%20', ' ').Trim('/')
$storageCredentials = New-Object Microsoft.WindowsAzure.Storage.Auth.StorageCredentials($StorageAccountName, $AccessKey)
$blobClient = New-Object Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient($StorageAccountEndpoint, $storageCredentials)
$blob = $blobClient.GetBlobReferenceFromServer($BlobFilename)
$stream = New-Object System.IO.FileStream($LocalFilename, [System.IO.FileMode]::Create)
$blob.DownloadToStream($stream)
$stream.Close()





Thursday, March 21, 2013

Extracting data from SQL Azure using Powershell

I have a SQL Website that is logging some debug output to a SQL Azure DB Linked Resource. In order to do some quick troubleshooting, I tried using the Manage interface in Azure, however it is lacking a way to export the data. I could manually copy and paste out database rows and columns, but that is a pain. I don't have SQL 2013 Management Studio and just want a lightweight way for me to access my databases.

I ended up doing this with Powershell:

$datasource=mydatasource
$database=mydatabasename
$userid=myuserid
$password=mypassword
$CommandText=some_sql_query


$connectionString = "Data Source=$datasource;Initial Catalog=$database;User ID=$userid;Password=$password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
$SqlCmd=New-Object System.Data.SqlClient.SqlCommand($CommandText)
$SqlCmd.Connection=New-Object System.Data.SqlClient.SqlConnection($ConnectionString)

$SqlAdapter=New-Object System.Data.SqlClient.SqlDataAdapter($SqlCmd)
$DataSet=New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
...
I can then get at the data through $DataSet.

I can also get the connection string though the Windows Azure console and go to SQL Databases, select my database, select "View SQL Database connection strings for ADO .Net, ODBC, PHP, and JDBC", copy the ADO.NET connection string, and replace "Server" with "Data Source", "Database" with "Initial Catalog", and "{your_password_here}" with my password.






Wednesday, November 21, 2012

Accessing Windows Azure blob storage

I have some files transferred to a container in my storage account. Now on a new VM, I need to access these files, but do not have CloudXplorer since .NET has not yet been installed. So I turned to the StorageClient module and Powershell.

Import-Module Microsoft.WindowsAzure.StorageClient.dll
$storageAccountName = 'my storage account'
$accessKey = 'my access key'
$storageCredentials = New-Object Microsoft.WindowsAzure.StorageCredentialsAccountAndKey($storageAccountName, $accessKey)
$storageAccountEndpoint = "http://$storageAccountName.blob.core.windows.net/"
$blobClient = New-Object Microsoft.WindowsAzure.StorageClient.CloudBlobClient($storageAccountEndpoint, $storageCredentials)
$filename = "my file"
$blob = $blobClient.GetBlobReference($filename)
$localFilename = "local filename"
$blob.DownloadToFile($localFilename)

References:
CloudBlob.DownloadToFile
Microsoft.WindowsAzure.StorageClient


Edit 2013-04-05
This is based on the 1.0 Storage Client. Updated script can be found in an updated post on Windows Azure Storage through Powershell