Book Image

SQL Server 2012 with PowerShell V3 Cookbook

By : Donabel Santos
Book Image

SQL Server 2012 with PowerShell V3 Cookbook

By: Donabel Santos

Overview of this book

PowerShell is Microsoft's new command-line shell and scripting language that promises to simplify automation and integration across different Microsoft applications and components. Database professionals can leverage PowerShell by utilizing its numerous built-in cmdlets, or using any of the readily available .NET classes, to automate database tasks, simplify integration, or just discover new ways to accomplish the job at hand."SQL Server 2012 with PowerShell V3 Cookbook" provides easy-to-follow, practical examples for the busy database professional. Whether you're auditing your servers, or exporting data, or deploying reports, there is a recipe that you can use right away!You start off with basic topics to get you going with SQL Server and PowerShell scripts and progress into more advanced topics to help you manage and administer your SQL Server databases.The first few chapters demonstrate how to work with SQL Server settings and objects, including exploring objects, creating databases, configuring server settings, and performing inventories. The book then deep dives into more administration topics like backup and restore, credentials, policies, jobs.Additional development and BI-specific topics are also explored, including deploying and downloading assemblies, BLOB data, SSIS packages, and SSRS reports. A short PowerShell primer is also provided as a supplement in the Appendix, which the database professional can use as a refresher or occasional reference material. Packed with more than 100 practical, ready-to-use scripts, "SQL Server 2012 with PowerShell V3 Cookbook" will be your go-to reference in automating and managing SQL Server.
Table of Contents (21 chapters)
SQL Server 2012 with PowerShell V3 Cookbook
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Resources
Index

Embedding C# code


In this recipe, we will embed and execute C# code in our PowerShell script.

How to do it...

Let's explore how to embed C# code in PowerShell.

  1. Open PowerShell ISE. Go to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.

  2. Add the following script and run it:

    #define code
    #note this can also come from a file
    
    $code = @"
    using System;
    public class HelloWorld
    {
       public static string SayHello(string name)
       {
          return (String.Format("Hello there {0}", name));
       }
       public string GetLuckyNumber(string name)
       {
          Random random = new Random();
          int randomNumber = random.Next(0, 100);
          string message = String.Format("{0}, your lucky" +
                           " number for today is {1}", 
                           name, randomNumber);
          return message;
       }
    }
    "@
    
    #add this code to current session
    Add-Type -TypeDefinition $code
    #call static method
    [HelloWorld]::SayHello("belle")
    
    #create instance
    $instance = New-Object HelloWorld
    
    #call instance...