Muistiinpano
Tämän sivun käyttö edellyttää valtuutusta. Voit yrittää kirjautua sisään tai vaihtaa hakemistoa.
Tämän sivun käyttö edellyttää valtuutusta. Voit yrittää vaihtaa hakemistoa.
This appendix provides concise answers to the review questions found at the end of each chapter. Use it to validate your understanding and reinforce key concepts.
Chapter 1 - Getting started with PowerShell
- Use the
$PSVersionTableautomatic variable. - Only when you need to bypass User Account Control (UAC) for tasks that require elevation on the local computer.
- The default execution policy on Windows client systems is
Restricted, which prevents running scripts. - Use
Get-ExecutionPolicyto determine the current execution policy. - Use
Set-ExecutionPolicy(for example,Set-ExecutionPolicy -ExecutionPolicy RemoteSigned).
Chapter 2 - The Help system
- No. The DisplayName parameter of
Get-Serviceis named, not positional. Get-Processhas six parameter sets.- Use
Get-Command -Noun EventLog. - Use
Get-Process -Name powershell. - Run
Update-Help(elevated as an administrator in Windows PowerShell) to download and install the latest help content.
Chapter 3 - Discovering objects, properties, and methods
Get-Processproduces aSystem.Diagnostics.Processobject.- Pipe the command to
Get-Member. - Check whether the object has a method that performs the action.
- Use the command's
PassThruparameter, if it has one. - Run the command once and store the results in a variable to avoid repeatedly generating large amounts of output while prototyping.
Chapter 4 - One-Liners and the pipeline
- A PowerShell one-liner is a single continuous pipeline, regardless of how many physical lines it spans.
- Natural line breaks can occur at characters such as the pipe (
|), comma (,), opening brackets ([ ]), braces ({ }), and parentheses (( )). - You should filter left to improve performance and efficiency by reducing the amount of data passed through the pipeline.
- A command can accept pipeline input by value (by type) or by property name.
- Because most content in the PowerShell Gallery is community-contributed and may not be vetted, it should be reviewed and tested before use.
Chapter 5 - Formatting, aliases, providers, comparison
- Because formatting cmdlets produce format objects, which break the pipeline and can't be used by most other commands.
- Use
Get-Alias -Name %to determine the actual cmdlet. - Because aliases reduce readability and portability, making scripts more difficult for others to understand.
- Use
Get-ChildItem -Path HKLM:\, HKCU:\to list registry keys in both hives. - The
-replaceoperator is case-insensitive by default, whereas the.Replace()method is case-sensitive.
Chapter 6 - Flow control
ForEach-Objectprocesses items one at a time from the pipeline (streaming), while theforeachstatement processes items from a collection that's already loaded into memory.- A
whileloop evaluates its condition before running, so it may not run at all if the condition is false, unlikedo whileanddo until, which run at least once. breakexits the loop entirely, whilecontinueskips the current iteration and proceeds to the next one.
Chapter 7 - Working with WMI
- WMI cmdlets (for example,
Get-WmiObject) are older and use DCOM, while CIM cmdlets (for example,Get-CimInstance) are newer and use WSMan by default. - WSMan (Windows Remote Management).
- CIM sessions allow reuse of connections, support alternate credentials, improve performance, and simplify managing multiple remote connections.
- Create a session option with
New-CimSessionOption, for example, to use DCOM, and pass it toNew-CimSession, then use that session withGet-CimInstance. - Use
Remove-CimSession.
Chapter 8 - PowerShell remoting
- Use
Enable-PSRemoting. - Use
Enter-PSSession. - It allows you to use a persistent session instead of specifying the computer name and credentials with each command.
- Yes, you can use a PowerShell session (PSSession) in a one-to-one interactive remoting scenario.
- Locally run cmdlets return live objects with methods, while remote commands return deserialized objects without methods.
Chapter 9 - Functions
- Use
Get-Verb. - Add the
[CmdletBinding()]attribute to the function. - When the function makes changes to system state or performs potentially impactful actions.
- Specify
-ErrorAction Stop. - To document how to use the function so you and others can easily understand it and access help
with
Get-Help.
Chapter 10 - Script modules
- Create a
.psm1file and place your functions in it. - Using approved verbs ensures consistency, avoids warnings, and improves discoverability.
- Use
New-ModuleManifest. - Use
Export-ModuleMemberin the.psm1file or specify functions in theFunctionsToExportfield of the.psd1file. - The module must be in a folder named the same as the module, located in a path listed in
$env:PSModulePath, and contain the appropriate module file (.psm1or manifest).
Final Notes
- These answers are intentionally concise to reinforce key concepts.
- Revisit the chapters for deeper understanding.
Tee yhteistyötä kanssamme GitHubissa
Tämän sisällön lähde on GitHubissa, jossa voit myös luoda ja tarkastella ongelmia ja pull-pyyntöjä. Katso lisätietoja osallistujan oppaasta.
PowerShell