Lesson02-SQL2012 - AAM - PowerShell Lab
Lesson02-SQL2012 - AAM - PowerShell Lab
Scripting
Version 1.2
Microsoft Confidential
Conditions and Terms of Use
Microsoft Confidential - For Internal Use Only
This training package is proprietary and confidential, and is intended only for uses described in the training materials.
Content and software is provided to you under a Non-Disclosure Agreement and cannot be distributed. Copying or
disclosing all or any portion of the content and/or software included in such packages is strictly prohibited.
The contents of this package are for informational and training purposes only and are provided "as is" without
warranty of any kind, whether express or implied, including but not limited to the implied warranties of
merchantability, fitness for a particular purpose, and non-infringement.
Training package content, including URLs and other Internet Web site references, is subject to change without
notice. Because Microsoft must respond to changing market conditions, the content should not be interpreted to be a
commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after
the date of publication. Unless otherwise noted, the companies, organizations, products, domain names, e-mail
addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real
company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should
be inferred.
Microsoft Confidential
Copyright and Trademarks
© 2013 Microsoft Corporation. All rights reserved.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering
subject matter in this document. Except as expressly provided in written license agreement from Microsoft, the
furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other
intellectual property.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under
copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted
in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose,
without the express written permission of Microsoft Corporation.
Microsoft Confidential
Lesson 2: PowerShell and WMI Scripting 5
Objectives
After completing this lab, you will be able to:
Validate and Set the Execution Policy
Leverage WMI to pull server level Info
Use SMO instead of SQLPS to retrieve SQL Server Info
Build an Advanced Function to simulate a native PowerShell Cmdlet
Write collected information to a SQL Server Database
Build an HTML report based on the data collected
Microsoft Confidential
6 Lesson 2: PowerShell and WMI Scripting
If the execution policy is not set to RemoteSigned, you can use the Set-Execution
policy to set to RemoteSigned.
Set-ExecutionPolicy RemoteSigned
On the Execution Policy Change window, confirm the change by hitting Yes.
5. In the command pane type the following to view the available Modules that can be
loaded:
Get-Module -ListAvailable
Notice SQLPS is a Module that can be loaded
6. Let’s load the SQLPS module. Note, if you are using PowerShell v3.0 the SQL
Server PowerShell provider is loaded the first time you run a SQL Server cmdlet.
Type the following command in the Command Pane.
Import-Module sqlps -DisableNameChecking
Note: This module is loaded only for this session of PowerShell. If you open a new
PowerShell ISE, you will have to import the module again. You can use Profiles
which are outside the scope of this lesson to import the SQLPS module when you
launch PowerShell ISE.
To confirm the module is loaded, type the following and SQLPS should now be
listed.
Get-Module
7. Leave the PowerShell ISE session open for the next task
Microsoft Confidential
Lesson 2: PowerShell and WMI Scripting 7
Microsoft Confidential
8 Lesson 2: PowerShell and WMI Scripting
was unable to connect to the server. Update the script in the script pane with the
following (GetProcessorInfoWithErrorHandling.ps1)
cls
$Computer = 'BadServer'
$ErrorActionPreference = 'SilentlyContinue'
$Error.Clear()
$ProcessorConfig = Get-WmiObject -class Win32_Processor -
computername $Computer -namespace root\CIMV2 |Select
PSComputerName, Name, NumberOfCores, NumberOfLogicalProcessors
write-Debug $Error.Count
If ($Error.Count -gt 0)
{
$ProcessorConfig = New-Object psobject
$ProcessorConfig | Add-Member -type NoteProperty -name
PSComputerName ("$Computer-failed to connect")
$ProcessorConfig | Add-Member -type NoteProperty -name Name -
value 'Unable to get ProcessorInfo'
$ProcessorConfig | Add-Member -type NoteProperty -name
NumberOfCores -value $null
$ProcessorConfig | Add-Member -type NoteProperty -name
NumberOfLogicalProcessors -value $null
Write-Debug "Failed to connect to $Computer"
}
$ErrorActionPreference = 'Continue'
$ProcessorConfig | FT * -AutoSize
10. In the Command Pane change the value for $DebugPreference to Continue and re-run
the above script to see the Debug Message.
$DebugPreference = 'Continue'
You should now see the following message displayed in the Command Pane
DEBUG: Failed to connect to BadServer
Microsoft Confidential
Lesson 2: PowerShell and WMI Scripting 9
Microsoft Confidential
10 Lesson 2: PowerShell and WMI Scripting
9. Since no parameter for the SQLInstance was specified, you are now prompted. At the
Prompt type SQLAdmin11CluN1
SQLInstance: SQLAdmin11CluN1
You should see the DB Level information in the Command Pane now.
10. To take advantage of the SQLInstance Parameter for Get-DBLevelInfo to be able to
accept input from the pipeline type the following command which will read the
contents of a text file with a list of servers to scan, then inspect the database
properties of each server. In the command pane enter
get-content 'L:\Advanced\Labs\Lesson 2 -
PowerShell\ServerAudit\ServerList.txt' | Get-Dblevelinfo
Microsoft Confidential
Lesson 2: PowerShell and WMI Scripting 11
Microsoft Confidential