Windows Powershell 3 QuickStart

Source: Internet
Author: User

Learn Windows PowerShell 3 in a Month of lunches (2rd)DON JONES.

This book is based on PowerShell 3.0, which is good for beginners, and can be yimushihang if you have a Linux Shell foundation. The following is an excerpt note, which records only a few interesting points of interest, covering most chapters.
Grammar is seldom involved in the book because, according to the author, This is the main use mode of PS (Powershell-ish ways), rather than the general scripting, flow control, and so on, which is just the glue of the command.
Read it over and highlight the impression on PS:

    • Modern, style-consistent design
      Should learn from Linux shell, Python, C #, etc., consistency reduces the cost of learning to use
    • Object-oriented,. NET Framework-based
      All objects, eliminate the drawbacks of unstructured text; you can use. NET objects directly, which is really powerful
    • Command type
      Developing an iterative model: Run-result-modify-retry
CH2. Meet PowerShell (first knowledge PS)

PowerShell consists of two components:

    1. PS Console
      Brisk, non-supported double-byte character set, not fully
    2. PS ISE (Integrated scripting environment)
      Based on WPF, supports Chinese, powerful help and complements

View version number: $PSVersionTable

CH3. Using the Help system

GUI-Comparable CLI Help system: can quickly find commands and understand their usage (wildcard characters are supported)

    • Get-command Find a matching command
    • Get-help Viewing command usage
    • Get-member viewing the type and members of an object
    • Help is function, equivalent to GET-HELP | More
      View Example: Help Get-eventlog-example
      More information: Help-full
    • Show-command with GUI help fill in

4 Command Command:cmdlet (naming convention verbs+nouns), Function,workflow,externapp. Example: Get-service-name e,s*
Help system upgrade First: Update-help (-source can specify local update source)
Multiple parameter collocation paths

CH4. Running Commands (command)

Cmdlets: Native commands, written for. Net languages

  • The optional optional parameter is enclosed in square brackets [-ComputerName <string[]>] , where the array is represented by a comma-delimited list, and can be uniquely identified with the parameter abbreviation, such as-comp
  • Otherwise, the mandatory parameter is forced,[-LogName] <string>
  • If the parameter name is also in square brackets, the position positional parameter

Externapp (External command, but cmdlet should be preferred)
$exe = "C:\mount.exe"
$host = "SRV"
$user = "Nick"
& $exe-H $host-U $user
New wording: -- C:\mount.exe -h srv -u nick , will be passed directly to CMD, do not parse

Ch5. Working with providers

Psprovider adapters, in the manner of class file systems, navigate and manage the data store (dynamic structure), such as the registry. View available: Get-psprovider
The noun of the associated cmdlet often has "Item" (a separate object, such as a file)
ItemProperty indicates that the item property, such as read-only, generally has a-path property, supports wildcards; ChildItem refers to sub-objects

Ch6. The pipeline:connecting commands (pipeline) Ch7. Adding commands (PS extension mechanism)

Two types: module and snap-in (Dll+xml)
Get-pssnapin-registered Registration
Add-pssnapin SQL Servercmdletsnapin100,sqlserverprovidersnapin100
Get-command-pssnapin sqlservercmdletsnapin100
module does not register, according to the $PSModulePath automatic discovery, on-demand loading
Get-command-module dnsclient

Some specific management shells are actually started with parameters Powershell.exe-noexit-command Import-module activedirectory

Ch8. Objects:data by another name (object)

View: get-process | Get-member

a collection of objects as a big in-memory table ofinformation, with propertiesas the columns and individual objects the rows.

PS objects generally come from the. NET Framework , but the ETS (extensible Type System) adds additional attributes (Scriptproperty,noteproperty,aliasproperty) to facilitate use.
Object has a lot of property, but Action/method is seldom used because it is usually implemented by cmdlets
get-process | Sort-object Vm,id-desc
get-process | Select-object name,id,vm,pm
Select-objectis is used to select or create a new properties (which will result in an object), and a specific row,-expandproperty can get a list of values. The where-object removes or filters the objects in the pipeline according to the specified conditions.
Each step of pipeline may produce different objects, so use GM to determine

Ch9. The Pipeline,deeper (Deep pipeline)

Pipeline parameter binding:
Try1. Byvalue: match input to parameter type, parameter must support Pipelineinputbyvalue (can be viewed by help-full); can only match one parameter (can be specified manually?). )
Try2. Bypropertyname: name matching only; support Pipinbypropertyname; multi-parameter matching
Parentheses command usage:
Get-wmiobject-class win32_bios-computername (get-content. \comput

Ch10. Formatting-and why it's done with the right (formatted) Ch11. Filtering and comparisons (filter and Compare)
    • Filter Left (command to support-filter parameter)
    • Where-object (using a similar comparison character)
      Get-Service | Where { $_.Status -eq ‘Running‘ } --> Get-Service | Where Status -eq ‘Running‘(single condition can simplify)
      (5-GT)-and (10-GT 100)
      $_. Responding-eq $False
Ch12. A Practical Interlude (practice)
  • Requirement: Remove all print jobs from a local printer "Accounting" every 3 a.m.
  • Find a commandhelp *task*;get-command -module scheduledtasks
  • Learning usagehelp new-scheduledtask -full
Ch13. Remote Control:one to one, and one to manych14. Using Windows Management Instrumentation (WMI)

WMI can get a lot of system information, but it doesn't work (lack of unified planning, lack of documentation). For PS It is a borrowed external technology and we recommend that you use the CIM Encapsulation command to interact with it.

WMI must know that it must

-namespace For example, root\cimv2 contains all the system and hardware information
-class represents a management component, one instance corresponds to a real component
Software: WMI Explorer

    • Get-wmiobject (Retrieve all instances of this class from the computer specified,legacy! ) + Invoke-wmimethod
      View Get-wmiobject-namespace root\cimv2-list | where name-like 'dis'
      Gwmi-class win32_desktop-filter "name= ' Company\administrator '"
    • Get-ciminstance + Invoke-cimmethod
      View Get-cimclass-namespace root\cimv2
      Get-ciminstance-classname Win32_LogicalDisk
Ch15. Multitasking with background jobs (multi-tasking in the background)

Help *-parameter Asjob

Ch16. Working with many objects, one at a time (bulk operation)

Three ways to batch operations: Batch cmdlets, Wmimethods, and object enumeration.

  • Get-service-name Bits,spooler,w32time-computer Server1,server2,server3 | Set-service-passthru-startuptype Automatic
    Stop-Service -name *B*
    The disadvantage is that there is generally no output during batch processing, and you can use-passthru to view the processed input
  • GWMI win32_networkadapterconfiguration-filter "description like '%intel% '" | Invoke-wmimethod-name EnableDHCP
    Output a result object that can see the returnvalue of each operation, but cannot tell
  • GWMI Win32Service-filter "name = ' BITS '" | foreach-object {$. Change ($null, $null, $null, $null, $null, $null, $ NULL, "[email protected]")}
Ch17. Security alertch18. Variables:a place to store your stuff (variable)

All objects
${my Variable} = ' server-r2 ', ' SERVER1 ', ' localhost '
[int] $number = read-host "Enter A number" (type declaration)

‘$‘ is a cue to the shell that what follows is going to be a variable name, and that we want to access the contents of that variable.

Ability to store multiple objects of different types
Single quote-a literal string
Double quotes-expansion string, but only when initial parsing is replaced!
Escape character-˜
subexpression-$ (cmd) ==> string

Ch19. Input and Outputch20. Sessions:remote Control with less work

Long connections
$iis _servers = New-pssession-comp web1,web2,web3

    • Using sessions with enter-pssession (interactive)
    • Using Sessions with Invoke-command
      Invoke-command-command {get-wmiobject-class Win32_Process}-session (Get-pssession-comp loc*)
    • Disconnect & Reconnect Session
Ch21. Scripting?

How to write a Help document
One script, one pipeline

 within a script, you only has one pipeline to work with. Normally, your scripts should strive to only output one kind of object, so that PowerShell can produce sensible text OUTPU T.  
Ch22. Improving your parameterized Script (command line options) ch23. Advanced Remoting Configurationch24. Using Regular expressions to parse text filesch25. Additional random tips, tricks, and techniquesch26. Using someone else ' s scriptch27. Never the end

Advanced Learning:

  • PowerShell ' s simplified scripting language
  • Scope
  • Functions, and the ability to build multiple tools into a single script file
  • Error Handling
  • Writing Help
  • Debugging
  • Custom formatting views
  • Custom type extensions
  • Script and Manifest modules
  • Using databases
  • Workflows
  • Pipeline Troubleshooting
  • Complex Object Hierarchies
  • Globalization and Localization
  • gui-based PowerShell Tools
  • Proxy functions
  • Constrained Remoting and delegated administration
  • Using. NET

Windows Powershell 3 QuickStart

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.