26
Inventory Your Network and Clients with Windows PowerShell Don Jones Senior Partner and Technologist Concentrated Technology, LLC SESSION CODE: WCL308

Don Jones Senior Partner and Technologist Concentrated Technology, LLC SESSION CODE: WCL308

Embed Size (px)

Citation preview

Inventory Your Network and Clientswith Windows PowerShellDon JonesSenior Partner and TechnologistConcentrated Technology, LLC

SESSION CODE: WCL308

IntroductionsMe: Don Jones, Concentrated Technology

Microsoft MVP Award recipientContributing Editor, TechNet MagazineAuthor of 45+ IT booksBlogger at http://ConcentratedTech.com

You: A Busy Windows AdministratorNeeding to inventory management information from one or more remote computers, both server and client (but especially client)

AgendaWindows PowerShell, BrieflyWindows Management Instrumentation, BrieflyExploring WMIPlaying with WMI on Your Local ComputerUsing WMI on Remote ComputersMaking a Flexible WMI Tool in Steps

Windows PowerShell, BrieflyThis session’s code will work with v1 and v2 (up until “advanced functions” at the very end – that’s v2 only)V1: Ships with Win2008 and Vista, available for WinXP and Win2003V2: Ships with Win2008R2 and Win7, available for WinXP, Win2003, Vista, and Win2008Primarily a command-line shell; has scripting capabilities, but we won’t be diving into them (much) – we’ll be using scripts as “batch files” mainlyNote: I make all scripts downloadable from my Web site, no need to take notes

Security NotesWindows PowerShell disables script execution (but not commands) by default; see Help about_execution* for detailsWindows PowerShell is subject to User Account Control (UAC); for our purposes, ensure you explicitly run “as Administrator” for necessary remote privileges

WMI, BrieflyPresent since Windows NT 4.0Does not require PowerShell on the remote computerUses Remote Procedure Calls (RPCs) for communication; does not require PowerShell v2 / WinRM “remoting” on remote machinesOrganized into:

Namespaces: Top-level, strictly organizational, usually product-relatedClasses: Represent potential manageable elementsInstances: Real-world occurrences of classesProperties & Methods: Elements of an instance

Properties contain management informationMethods perform actions (such as reconfiguration)

WMI SecurityConfigured (by default) on the root namespaceTypically, don’t mess with this – many things rely on WMI and can get angry if you fuss with the permissionsUse WMI Control MMC snap-in to modify (or view – great way for seeing what namespaces are installed)

WMI Security and Namespaces

DEMO

Differences from Machine to MachineWMI namespaces will vary based on the roles/products/technologies installed on a computerIndividual classes will vary based on product or Windows versionClasses’ properties and methods can vary from version to version, too

Core Windows OS and hardware stuff is in \root\CIMv2; most classes start with Win32_ or CIM_ prefix in the class name

Best to get a “WMI Explorer” or “WMI Browser” that can be focused at a remote computer to see what that computer hasYou don’t need a class on your computer in order to query it from a remote computer (very handy)

DocumentationEasiest: Plug class name into favorite search engineFirst 1-2 results usually msdn.microsoft.com/…, which will be what you wantOnly rely on “core” classes being well-documented – everything else is pretty inconsistently documentedUse search engines to find examples – even VBScript examples can usually provide some help

Pipe retrieved WMI objects to Get-Member or to Format-List * in order to see what properties/methods they have, and what values the properties contain

Exploring WMI

DEMO

Using PowerShell LocallyUse Get-WmiObject cmdletCannot specify alternate credentials for local connections (by design); make sure shell is running as AdministratorSpecify –class name to query; retrieves all instances and all properties by default

Filtering ResultsUse –filter parameter to narrow down instances returned (large result sets can take a lot of time and resources)-filter "property <comparison> value”

Numeric values are not delimitedString/date values are delimited in single quotesEasiest to enclose the entire filter in double quotes

Valid comparison operators:= > < <> <= => LIKE (use % as a wildcard)These are different from PowerShell’s native operators!

WMI in PowerShell, Locally

DEMO

Going RemoteUse –computerName parameter to specify remote computer(s)

-computername ”Client17”-computername (get-content names.txt)-computername “client17”,”client18”,”client19”

Other parameters allow specification of alternate credential-credential CONTOSO\AdministratorGraphically prompts for password – by designUse Get-Credential to create a reusable credential object that contains a password

Multiple ComputersRemote connections are synchronous and sequential (not parallel)

Use –AsJob to run in backgroundUse remoting (Invoke-Command) for parallel processing (not in this session)

Failure of one computer will not stop the command from continuingYou can trap errors and log them; we’ll see how later

WMI in PowerShell, Remotely

DEMO

The StepsMove command into a “batch file” for easier repetitionEnclose command in a function for parameterization

Add error handling / loggingUpgrade function to a filtering function to accept pipeline input

Enables scenarios like getting names from AD OR a text fileUpgrade function to an advanced function (v2 only) for cmdlet-like behaviorPackage as a Script Module (v2 only) for easier distribution and re-use by othersAdd comment-based help so you can tell what the heck it’s doing

(I’ll demo – you remind me of what step is next)

Building a WMI Tool

DEMO

Remember! I’ll save all my scripts and make them downloadable fromwww.ConcentratedTech.com in a few days – no need to take notes!

ConclusionWMI + PowerShell = Very Powerful, Very ConvenientAnd a great way to learn PowerShell incrementally!Build reusable, cmdlet-like tools

Now in the TechEd Bookstore:Windows PowerShell v2: TFM by Don Jones & Jeffery Hicks

Download this session’s scripts & slides fromwww.ConcentratedTech.com

Weekly, Monthly and Quarterly Rhythm of Topical Content

What is the Springboard Series?

To the IT pro, our goal is• Be the definitive resource for Desktop IT pros• Open, honest; show don’t tell• Information at right time, right level across Adoption Lifecycle

Inside of Microsoft we are• A turnkey IT pro engagement platform for depth and breadth• The program to mobilize MS marketing and field to

focus on desktop OS IT pros

Visit the Springboard Series on TechNet at www.microsoft.com/springboard

The Springboard Series IT pro experience offers dynamic content and structured guidance across the adoption lifecycle

DEPLOYPILOT MANAGEEXPLOREDISCOVER

Is it worth the pain?How does it change

my work? Is our environment ready? Is the organization ready?How do I maintain

and optimize?

one-Windows TechCenter in 10 languagesVirtual Roundtable Events

Springboard Technical Experts Panel Event Support

and Resources

Straight-talk Monthly Feature Articles and Overview Guides

TalkingAboutWindowsVideo Blogs

Resources

www.microsoft.com/teched

Sessions On-Demand & Community Microsoft Certification & Training Resources

Resources for IT Professionals Resources for Developers

www.microsoft.com/learning

http://microsoft.com/technet http://microsoft.com/msdn

Learning

Complete an evaluation on CommNet and enter to win!

Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31st

http://northamerica.msteched.com/registration

You can also register at the

North America 2011 kiosk located at registrationJoin us in Atlanta next year

© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to

be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

JUNE 7-10, 2010 | NEW ORLEANS, LA