Windows PowerShell 3.0 Step By Step - Gbv.de

Transcription

Windows PowerShell3.0 Step by StepEd Wilson

ContentsChapter1ForewordxixIntroductionxxiOverview of Windows PowerShell 3.0UnderstandingUsingWindows PowerShell1cmdletsInstalling3Windows PowerShellDeploying Windowsoperating systemsUsing13PowerShell to down-level4command-line utilitiesSecurity5issues with Windows PowerShell6Controllingexecution of PowerShell cmdlets7Confirmingactions8Suspending confirmation of cmdletswith Windows PowerShellWorkingAccessingSupplying optionswith theWorkingExploring110the Windows PowerShell console11for cmdlets12help options13commands:quick10Windows 19reference22What do you think of this book? We want to hear from you!Microsoft is interested in hearing your feedback so webooks and learning resources for you. To participate incanacontinually improvebrief online survey,ourplease visit:microsoft.com/iearning/booksurvey

Chapter2UsingWindows PowerShell Cmdletsthe basics of cmdletsUnderstandingUsing2323the Get-Childltem cmdlet24Obtaining24FormattingUsingdirectory listingaadirectory listing usingthe Format-List cmdletthe Format-Wide cmdletFormattingaLeveraging the27directory listing usingFormatting output rd charactersUsing the Get-Member cmdletUsingthethe Get-Member cmdlet to examinepropertiesCreatingUsing the44New-Object cmdletand50the wshShellUsingObjectShow-Command cmdletWindows PowerShell cmdlet52naming helpsyou learn5455WorkingaWindows PowerShellall aliases forwith cmdlets:aprofile57particular objectstep-by-stepexercisesChapter 2 quick referenceUnderstandingUnderstandingandUsing PowerShell ProvidersPowerShellContents595963Understandingthe certificateUnderstandingthe environmentUnderstandingtheUnderstandingthe function6565providersUnderstanding the alias providerviii54Windows PowerShell verb distributionFinding350Windows PowerShell verb groupingCreatingChapter3644and methodsUsing2931power of Get-Commandfor cmdlets2666providerproviderfilesystem providerprovider68768085

Usingtheregistry providerThe tworegistrythe pter 43to manage the Windows97providerproviders: step-by-stepexercisesWindows PowerShelland Jobs107107remoting107remoting112WinRMUsingWindows PowerShelljobsUsingWindows PowerShellremoting: step-by-stepChapter 4 quickChapter5119exercisesreference131131write Windows PowerShell lingWindows PowerShellscripting support134Runningfrom command line toWindows PowerShellUnderstandingscriptscriptsvariables and constants136138141Use of constants146the While statement147ConstructingAthe While statement in PowerShellpractical exampleUsing specialUsing133Windows PowerShellTransitioningUsing127130Using PowerShell ScriptsWhy101106Using PowerShell strydrivesofusingthe While statementfeatures of Windows PowerShell148150150the Do.While statement151Using the range operator152Operating152Castingover anarrayto ASCII values152Contentsix

Usingthe Do.Until statementComparing153the PowerShell Do.Untilstatement with VBScript. .154the Windows PowerShell Do statementUsingThe For statement156Usingthe For statement156Usingthe Foreach statement158the Foreach statementExitingearlyThe If statementandcomparison operatorsEvaluating multiple conditions164the Switch statementControlling matchingCreating y-step exercisesreferenceUsingIncludingprovide171easeof codereusefunctions in the Windows PowerShell environmentUsing dot-sourced functions182for functions184here-string object for helpUsingaUsingtwoUsingamorethan twoinput parameterstypetoUse of functions toconstraint in afunctioninput parametersencapsulate business logicprovideeaseCreatingChaptera6function:of modificationstep-by-step exercisesquick reference184186Understanding filtersContents180180Use of functionsX178Using dot-sourcingAdding helpUsing171functionsfunctions to168170Working with FunctionsUnderstanding163164The Switch statementUsing159161Using assignmentChapter154190192194196201205208

Chapter7Creating Advanced Functions and ModulesThe [cmdletbinding] attribute209verbose messages210Automatic parameter checks211Adding support for214EasyAdding supportSpecifyingthe-whatif parameterfor the -confirm parameterthe default parameter set217The mandatory parameter property217The218position parameter propertyThe ParameterSetName parameter property219TheValueFromPipeline property220TheHelpMessage 22loading modules222available modules223modulesLoading225Installing modules227per-user Modules folderCreatingaWorkingwith the modulePath le drivefor modulemodule fromdependenciesa234share237238advanced function:step-by-stepexercisesquick referenceRunning the Windows PowerShell ISEWorkingTabthe Windows PowerShell ISEwith theexpansionscript245249Using the Windows PowerShell ISENavigating230232moduleCreatingChapter8215216The parameter attributeChapter209paneand IntelliSense251251252254256Contentsxi

WorkingwithWindows PowerShellWindows PowerShell ISEUsingCreatingnewChapter9snippetsto create codestep-by-stepSix Different g267the six different Windows PowerShellwhethera newaspecific profileone or moreUsingthe All Users, All ionalityinto a hapter 10 Using WMIUnderstanding283the WMI model284Working with objects and namespacesWMIWorking289with WMI classes289WMI293Obtaining service information: 270273fileprofile: step-by-stepa.268271profilesWhere to store esDesign considerations for profilesListing262265Examining the profile variable9261with Windows PowerShell ProfilesWorkingChapter259snippetsquick reference8Creating257Windows PowerShell ISE snippetsthe Commands add-on:Chapter257snippetsWindows PowerShell ISERemoving 5

Chapter11QueryingWMI307Alternate ways to connect to WMI307Selective data from all instances316Selecting multiple properties316Choosing specificUtilizinganinstances319321operatorWhere is the where?ShorteningWorking withChapter11Chapter 12 RemotingUsingWMIthe erence335WMI337against remote systemsSupplying337alternate credentials for the remote connectionUsingWindows PowerShell remoting toUsingCIM classes to query WMI classesWorking327runWMIwith remote resultsWMI jobsChapter13Using347WMI:352quick reference354WMI MethodsWMI Classes355WMI cmdlets to execute instance methods355CallingUsing12343350Using Windows PowerShell remoting andStep-by-step exercisesChapter341344Reducing data via Windows PowerShell parametersRunning338onUsingthe terminate methodUsingthe Invoke-WmiMethod cmdlet358Usingthe360[wmi] typedirectly357acceleratorWMI to work with static methodsExecutingChapterinstance methods:13quick i

Chapter14UsingUsing367the CIM Cmdletsexplorethe CIM cmdlets toUsing367367the -classname parameterFindingWMI class methods368by qualifier369classesFilteringRetrievingWMI classes371WMI instancesReducing returned properties and instancesCleaningWorkingChapter15WMI instances:14373379382383Working with Active DirectoryADSILDAPCreatingin Active383providers385an387names393usersWhat isuser accountWorkingwithcontrol?15Chapter 16 Working397units:step-by-stepexercisesquick referencethe ActiveInstallingthe 432435a userandunlocking421421the FSMO role holdersDiscoveringFinding419Directorystarted with the Activethe Active412418with the AD DS ModuleUnderstandingUsing396usersCreating multiple sexercisesstep-by-stepreferencequickCreating objectsxiv373up output from the commandwith r accounts436

Finding ive440user accountsDirectory objects: rsActive445Directory withWindows Server 2012Usingthe ActiveAddinga newAddingaDirectoryDomain controllerChapter17447module todomain controllerread-onlyquickdeployto ana newexistingdomainprerequisites: step-by-stepexercisesreferenceTracingtheinWindows PowerShellcmdletthescript471479Using Set-PSDebug-StrictUsing the Set-StrictModethe461467strict modeDebuggingerrors461467scriptStepping throughEnabling457461three different types ofSet-PSDebug453460Understanding debuggingthe447455Chapter 18 Debugging ScriptsUsingforestdomain ting breakpoints483Settingabreakpointon aline number483Settingabreakpointon avariable485Settingabreakpointon acommand489RespondingtobreakpointsListing breakpointsEnablinganddisabling breakpointsDeleting y-step exercisesreference490492494494494499Contentsxv

Chapter19501Handling Errors501Handling missing parametersCreatingdefault value foraa502parameterMaking the parameter mandatoryLimiting503choices504Using PromptForChoiceto limit selections504identify computer connectivityUsingTest-Connection toUsingthe -contains operator to examine contents ofUsing theanarray. .507-contains operator to test for properties509512Handling missing rightsand failAttempt512Checking for rights and exiting gracefullyHandling missingIncorrect 26errorsboundary-checking functionPlacing limitson526the parameter528529Using Try.Catch.FinallyCatching multipleUsing o limit selections:quick referenceManaging ExchangeExploringtheExchangewith remoteServer2010 4serversand the mailboxsettings539539540544544548Managing storage settings550Examining themailbox database550the mailbox ng recipient settingsCreating506

Managing Exchange logging553Managing auditing557Parsingthe audit XML fileCreatingChapteruser ce570AppendixAWindows PowerShell Core Cmdlets571AppendixBWindows PowerShell ModuleCoverage579Appendix CWindows PowerShell CmdletNaming583AppendixDWindows PowerShellAppendixEUseful WMI ClassesAppendixFBasicAppendixGGeneral PowerShell587FAQ597621Troubleshooting Tips625Scripting GuidelinesIndex633About the Author667What do you think of this book? We want to hear fromMicrosoft is interested in hearing your feedback so webooks and learning resources for you. To participate inyou!continually improve ourbrief online survey, please sxvii

Usingthe registryproviderto managetheWindowsregistry 87 Thetwo registrydrives 87 Understandingthevariable provider 97 Exploring PowerShell providers:step-by-step exercises 101 Chapter 3 quickreference 106 Chapter4 UsingPowerShell Remoting andJobs 107 UnderstandingWindows PowerShell remoting 107 Classic remoting 107 WinRM 112 UsingWindows PowerShelljobs 119 UsingWindows PowerShell remoting:step .