Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SQL Server 2012 with PowerShell V3 Cookbook

You're reading from   SQL Server 2012 with PowerShell V3 Cookbook

Arrow left icon
Product type Paperback
Published in Oct 2012
Publisher Packt
ISBN-13 9781849686464
Length 634 pages
Edition 1st Edition
Concepts
Arrow right icon
Author (1):
Arrow left icon
Donabel Santos Donabel Santos
Author Profile Icon Donabel Santos
Donabel Santos
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

SQL Server 2012 with PowerShell V3 Cookbook
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with SQL Server and PowerShell FREE CHAPTER 2. SQL Server and PowerShell Basic Tasks 3. Basic Administration 4. Security 5. Advanced Administration 6. Backup and Restore 7. SQL Server Development 8. Business Intelligence 9. Helpful PowerShell Snippets SQL Server and PowerShell CheatSheet PowerShell Primer Resources Creating a SQL Server VM Index

Converting script into functions


A function is a reusable, callable code block(s). A function can accept parameters, and can produce different results based on values that are passed to it.

A typical anatomy of a PowerShell function looks like:

function Do-Something
{
    <#
       comment based help
    #>
    param
    (
       #parameters
    )    
    #blocks of code
}

To illustrate, let's create a very simple function that takes a report server URL and lists all items in that report server. This function will take in a parameter for the report server URL, and another switch called $ReportsOnly, which can toggle displaying between all items, or only report items.

function Get-SSRSItems
{
    <#
       comment based help
    #>
    param
    (
       [Parameter(Position=0,Mandatory=$true)]
       [alias("reportServer")]
       [string]$ReportServerUri,
       [switch]$ReportsOnly
    )    

    Write-Verbose "Processing $($ReportServerUri) ..."
    $proxy = New-WebServiceProxy `
        -Uri $ReportServerUri `
        -UseDefaultCredential 
    if ($ReportsOnly)
    {
        $proxy.ListChildren("/", $true) |
        Where TypeName -eq "Report"
    }
    else
    {
        $proxy.ListChildren("/", $true) 
    }         
}

To call this function, we can pass in the value for –ReportServerUri and also set the –ReportsOnly switch:

$server = "http://server1/ReportServer/ReportService2010.asmx"

Get-SSRSItems -ReportsOnly -ReportServerUri $server |
Select Path, TypeName |
Format-Table -AutoSize 

To allow your function to behave more like a cmdlet and work with the pipeline, we will need to add the [CmdletBinding()] attribute. We can also change the parameters to enable values to come from the pipeline by using ValueFromPipeline=$true. Inside the function definition, we will need to add three blocks:

  • BEGIN

    Preprocessing; anything in this block will be executed once when the function is called.

  • PROCESS

    Actual processing that is done for each item that is passed in the pipeline.

  • END

    Post-processing; this block will be executed once before the function terminates executing.

We will also need to specify in the parameter block that we want to accept input from the pipeline.

A revised function follows:

function Get-SSRSItems
{
    <#
       comment based help
    #>
    [CmdletBinding()]
    param
    (
       [Parameter(Position=0,Mandatory=$true,
                  ValueFromPipeline=$true,
                  ValueFromPipelineByPropertyName=$true)]
       [alias("reportServer")]
       [string]$ReportServerUri,
       [switch]$ReportsOnly
    )    
    BEGIN
    {
    }
    PROCESS
    {
        Write-Verbose "Processing $($ReportServerUri) ..."
        $proxy = New-WebServiceProxy `
                 -Uri $ReportServerUri -UseDefaultCredential 
        if ($ReportsOnly)
        {
          $proxy.ListChildren("/", $true) |
          Where TypeName -eq "Report"
        }
        else
        {
         $proxy.ListChildren("/", $true) 
        }         
    }
    END
    {
        Write-Verbose "Finished processing"
    }
}

To invoke, we can pipe an array of servers to the Get-SSRSItems function, and this automatically maps the servers to our –ReportServerUri parameter since we specified ValueFromPipeline=$true. Note that Get-SSRSItems will get invoked for each value in our array:

$servers = @("http://server1/ReportServer/ReportService2010.asmx", "http://server2/ReportServer/ReportService2010.asmx")

$servers | 
Get-SSRSItems -Verbose -ReportsOnly |
Select Path, TypeName |
Format-Table -AutoSize
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image