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

Running PowerShell scripts


It is now time to run your first script!

Through shell or through ISE

You can run ad hoc commands through the shell or through the Integrated Scripting Environment (ISE).

To use the PowerShell console, you can launch the shell by opening Start | All Programs | Accessories | Windows PowerShell | Windows PowerShell. Often when managing your servers, you may need to run this as Administrator (right-click on the PowerShell icon and select Run as Administrator).

Once the console is ready, you can type your commands and press Enter to see the results. For example, to display ten (10) running processes, you can use the Get-Process cmdlet, as shown in the following screenshot:

You can also use the ISE, and to launch the ISE, go to Start | All Programs | Accessories | Windows PowerShell | Windows PowerShell ISE. Similar to the shell, you can type your command and press the Run button (green arrow icon).

Note

More details about the ISE are covered in Chapter 1, Getting Started with SQL Server and PowerShell.

Typically, you would save your commands in a script file with the .ps1 extension, and run them from the shell in few different ways:

  1. From PowerShell console, using the call operator (&):

    PS C:\ > & "C:\PowerShell\My Script.ps1"
    
  2. From PowerShell console, using dot sourcing. Dot sourcing simply means you prepend a dot and space to your invocation. You would invoke your script by using dot sourcing to persist variables and functions in your session:

    PS C:\PowerShell > . ".\My Script.ps1"
    PS C:\> . "C:\PowerShell\My Script.ps1"
    
  3. From a command prompt:

    C:\>powershell.exe -ExecutionPolicy RemoteSigned -File "C:\PowerShell\My Script.ps1"
    

Execution policy

PowerShell scripts are not authorized to just run.

Remember the "I Love You" virus? It took off because it was so easy to launch a script just by double-clicking the .vbs file.

To avoid problems such as this, PowerShell scripts by default are blocked from running. This means you cannot just accidentally double-click a PowerShell script and execute it.

The rules that determine which PowerShell scripts can run are contained in the Execution Policy. This will need to be set ahead of time. The different settings are:

Execution Policy

Description

Restricted

Default execution policy

PowerShell will not run any scripts

AllSigned

PowerShell will run only signed scripts

RemoteSigned

PowerShell will run signed scripts, or locally created scripts

Unrestricted

PowerShell will run any scripts, signed or not

Bypass

PowerShell will not block any scripts, and will prevent any prompts or warnings

Undefined

PowerShell will remove set execution policy in current user scope

To determine what your current setting is, you can use Get-ExecutionPolicy:

PS C:\>Get-ExecutionPolicy

If you try to run a script without setting the proper execution policy, you may get an error similar to this:

File C:\Sample Script.ps1 cannot be loaded because the execution of scripts is disabled on this system. For more information, see about_execution_policies.

To change the execution policy, use Set-ExecutionPolicy:

PS C:\>Set-ExecutionPolicy RemoteSigned

Typically, if you need to run a script that does a lot of administrative tasks, you will need to run the script as administrator.

To learn more about execution policies, run:

help about_execution_policies

For more information about how to sign your script, use:

help about_signing
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