PowerShell WebClient Example

UPDATE [August 2014] : I made an update to this post here.

 

I’m still amazed that PowerShell has no commandlet equivalent of WGET or CURL. Even in SQL Server Integration Services (SSIS) this is missing.

In the age of RESTful webservices, the official guidance (at the time of writing) is to write your own with custom .Net code. Seriously?!?

It seems they are indeed serious…

So here for your amusement is a simple  function you can use in your PS1 scripts. It calls the .Net WebClient class.

## Declare a function that takes source and destination arguments
Function Get-Webclient ($url, $out) {

$proxy = [System.Net.WebRequest]::GetSystemWebProxy()

$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

$request = New-Object System.Net.WebCLient

$request.UseDefaultCredentials = $true ## Proxy credentials only

$request.Proxy.Credentials = $request.Credentials

$request.DownloadFile($url, $out)

}

## Call the function with our source and destination.
Get-Webclient "http://www.google.com" "C:\tmp\Foo3.txt"

But what if the destination is password protected and you need authentication credentials?

## Declare a function that takes source and destination arguments
Function Get-Webclient ($url, $out) {

$proxy = [System.Net.WebRequest]::GetSystemWebProxy()

$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

$request = New-Object System.Net.WebCLient

$request.UseDefaultCredentials = $true

$request.Proxy.Credentials = $request.Credentials

$passwd	= ConvertTo-SecureString "mypassword" -AsPlainText -Force; ## Website credentials

$request.Credentials = New-Object System.Management.Automation.PSCredential ("username", $passwd);

$request.DownloadFile($url, $out)

}

## Call the function with our source and destination.
Get-Webclient "http://www.securedwebsite.com" "C:\tmp\Foo3.txt"
Advertisements

SSRS Example Wrapping a long thin tablix into two columns on the page

I have a report table with only a few columns but lots of rows. When printed it runs to several pages with a lot of white space (and wasted paper). I came up with a solution I’ll probably end up using again so decided to write it up and share.

Challenge

Wrap the tablix into two columns on the page. Records should appear to flow across the page (left to right). As opposed to down the first column then down the second column.

Solution

Introduce a hidden column in my Tablix containing a row number.

=RowNumber(“Tablix1”)

Copy and paste the Tablix then line the two up, side by side.

Access the Group properties for the default Details1 Group (containing the invisible row number). This is the first tablix on the left half of the page.

Either Filter the group (or set the visibility) by dividing (mod) the invisible row number by 2 and check for the remainder.

=IIF(ReportItems!Tab1Row.Value MOD 2 = 0 , True, False)

Access the Group for the Details2 Group in the second tablix on the right side of the page and filter the group (or set visibility) the same way. Note the switched True/False args.

=IIF(ReportItems!Tab2Row.Value MOD 2 = 0 , False, True)

Result

Pain Dashboard

My result is even numbered record items (row numbers) in the left column and odd in the right column. Report data appears to run left to right.