Windows Power Shell

Source: Internet
Author: User

Http://baike.baidu.com/view/2838635.htm? Fromid = 58531

Windows Power shell ask for help editing encyclopedia business cards

Windows Power Shell

Windows powershell is a command line shell program and script environment that allows command line users and script writers to use
. NET Framework. It introduces many useful new concepts to further expand your knowledge and scripts created in the Windows Command Prompt and Windows Script Host environment.

Directory

Windows powershell Introduction
Ten Basic concepts of powershell scripts
Powershell V3 Overview
Advantages and disadvantages of powershell v3
Expand
Windows powershell Introduction
Ten Basic concepts of powershell scripts
Powershell V3 Overview
Advantages and disadvantages of powershell v3
Expand
Edit this section of Windows
Powershell Introduction Target AudienceWindows powershell is intended for IT professionals, programmers, and senior users who do not have any background in Windows powershell. Although it is helpful to have background knowledge in script and WMI, understanding this document does not assume or require you to have this knowledge. About windows powershellBy solving long-standing problems and adding new features, Windows powershell aims to improve the command line and script environments. Powershell uses. net
Framework is a platform that receives and returns. Net objects. This brings new methods and tools for managing and configuring Microsoft Systems. Discoverable featuresYou can easily find the features of Windows powershell. For example, to view and change the list of cmdlets for Windows Services, run: Start-> Run-> CMD and enter powershell under the command line.
Go to Windows powershell and enter the following command: Get-command *-service after you find the cmdlet that can complete the task, you can use the get-help cmdlet to learn more about the cmdlet. For example, to display help on the get-service cmdlet, type: Get-help get-service. To fully understand the output of the cmdlet, you can pass the output to the get-member cmdlet through the pipeline. For example, the following command displays information about the members output by the get-service cmdlet. Get-service | get-member ConsistencyA management system may be a complex task, and tools with unified interfaces will help to control its inherent complexity. However, whether it is a command line tool or a COM object that can write scripts, there is no such thing as consistency. Windows powershell consistency is one of its main advantages. For example, if you learned how to use the sort-object cmdlet, you can use this knowledge to sort the output of any cmdlet. You do not need to know the different sorting routines of each cmdlet. In addition, cmdlet developers do not have to design the ordering function for their cmdlet. Windows powershell provides them with a framework that provides basic functions and forces them to be consistent in many aspects of interfaces. Although the framework removes some options that are usually left for developers, developing robust and easy-to-use cmdlet is much easier in return. Interactive script EnvironmentWindows powershell combines the interactive environment with the script environment to allow you to access the command line tool and COM object, and to take advantage of the powerful functions of the. NET Framework class library (FCL. This environment improves the Windows command prompt, which provides an interactive environment with multiple command line tools. In addition, the Windows Script Host (wsh) script is improved, which allows you to use multiple command line tools and
Com automatic object, but no interactive environment is provided. By combining access to all of these features, Windows powershell extends the ability of interactive users and script writers to facilitate system management. Object-oriented although you can type commands in text to interact with Windows powershell, Windows powershell is object-based instead of text-based. Command output is an object. You can send an output object to another command as its input. Therefore, Windows
Powershell provides a familiar interface for people who have used other Shell programs, and introduces a new and powerful command line example. By allowing sending objects (rather than text), it extends the concept of sending data between commands. Easy transition to scriptWith Windows powershell, you can easily enter commands in interactive mode to create and run scripts. You can enter a command at the windows powershell command prompt to find the command that can execute the task. You can then save these commands to scripts or history records and copy them to a file to use them as scripts. Identify the provider you are about to use. by identifying the provider installed in powershell, you can understand what capabilities powershell provides by default. The provider can use a simple access method to expose data in different storage locations. It is as simple as browsing the directory structure on different disks.
The provider stores different information in locations and expresses it as a "Drive"-directory structure, which is easy to understand. Just as we want to upload the setup.exe file under the win32directory of the d Drive, We need to click the icon of the d drive through the browser, then select the Win32 Directory and double-click the same, if we want to access the data in the "Registry, then we just need to simply go to the Registry drive through the set-location command, and then get its sub-data with the GET-CHILDITEM command. Note: In fact, powershell accesses the disk drive through provider. Switching the drive is actually the same as switching other data containers. For example: set-Location D: \ This is to switch the drive set-location
HKLM: \ This is the HKLM key to switch to the Registry. In addition, you can run the get-psprovider command to view all providers that have been installed. Anyone familiar with. NET programming can write providers. When the new provider is installed, it is called snap-in. Snap-in is a DLL file of the dynamic Connection Library and can be installed in powershell. However, when a snap-in is installed, it cannot be uninstalled.
Get-psprovider: Name capabilities drives ---- ------------ alias shouldprocess {alias} environment shouldprocess {env} filesystem filter, shouldprocess {C, D, F, ...} function shouldprocess {function} registry shouldprocess {HKLM, hkcu} variable shouldprocess
{Variable} certificate shouldprocess {Cert} are the default installed providers on my machine. Use set-location and get-childitem to browse the data set-location to change the current directory and select the current provider. Get-childitem is used to obtain the sub-objects in the current directory or specified directory:
Example: set-location hkcu: \ Software get-childitem Example 2: GCI-path HKLM: \ Software There are two methods to connect to the WMI Service:L use get-wmiobject to easily connect to the WMI Service and obtain WMI objects. L use a COM Object, "wbemscripting. swbemlocator", to connect to the WMI Service. The swbemlocator object has only one method, that is, connectserver (). This method accepts five parameters: User Name, password, language code, and verification method (Kerberos,
NTLM, etc.), flag (timeout value ). In the following example, we use the new-object command to create an instance named "wbemscripting. swbemlocator. Then, the connectserver method of the instance is used to connect to a WMI namespace (Root \ cimv2). The connectserver method returns a wmiservice object and then uses the subclassesof () method of the object, returns a series of WMI class: $ strcomputer
= ". "$ Wmins =" \ Root \ cimv2 "$ strusr =" "# blank for current security. domain \ USERNAME $ strpwd = "" # blank for current security. $ strlocl = "ms_409" # us English. can leave blank for current language $ strauth = "" # If specify domain in strusr this must be blank
$ Iflag = "0" # Only two value allowed: 0 and 128. $ objlocator = new-object-comobject "wbemscripting. swbemlocator "$ ob1_miservice = $ objlocator. connectserver ($ strcomputer, '$ wmins, $ strusr, $ strpwd, $ strlocl, $ strauth, $ iflag) $ colitems = $ obw.miservice. subclassesof ()
Write-host "There are:" $ colitems. Count "classes in $ wmins" foreach ($ objitem in $ colitems) {$ objitem. Path _. Class} New scripting languageFor the following reason, Windows powershell uses its own language instead of reusing the existing language: Windows powershell needs the language used to manage. Net objects. This language must provide a consistent environment for using the cmdlet. This language needs to support complex tasks without making simple tasks more complex. The language must be consistent with the advanced language used in. NET Programming (such as C. Edit ten basic concepts of this powershell script 1. PS1 FileA powershell script [1] is actually a simple text file. This file contains a series of powershell commands, each of which is displayed as an independent line. For text files that are considered as powershell scripts, the file name must be used. PS1 extension.2. Execution permissionTo prevent execution of malicious scripts, powershell has an execution policy. By default, this policy is set to restricted, which means powershell scripts cannot be executed, you can use the following cmdlet command to determine the current execution policy: Get-executionpolicy you can choose to use the execution policy: Restricted-script cannot run. Remotesigned-locally created scripts can run, but those downloaded from the Internet cannot run (unless they have a digital signature signed by a trusted publisher ).
Allsigned-the script can only run if it is signed by a trusted publisher. Unrestricted-script execution is unrestricted, regardless of the source or signature. You can use the following cmdlet command to set the powershell execution policy: set-executionpolicy <Policy Name>3,If you want to run a script from the command line to run an executable file, a permanent way for many years is to go to the location where the execution file is located on the command line, enter the name of the execution file, but the old method is not applicable to powershell executable scripts. To execute a powershell script, you must enter the complete path and file name. For example, if you want to run a powershell script named. for the script of PS1, you can type: C: \ scripts \ aps1, the biggest exception is, if the powershell script file is located in your system directory, you can directly enter the script file name at the command prompt to run the script, such :. \. note that the preceding parameters must be added to ps1. \, which is exactly the same as how to execute shell scripts in Linux.4. PipelinesThe role of a pipeline is to use the output of one command as the input of another command. You only need to use the pipeline symbol (|) to connect two commands (or cmdlet. To help you understand how the pipeline works, let's take an example. Suppose you want to create a list of processes running on the server and sort them by the process ID, you can use the get-process cmdlet command to obtain the process list, but the list is not sorted by default. If you use a pipe to output the command, the process list is sorted by the process ID, for example, get-process | sort-Object ID.5,Although a variable can be delivered to another command by using a pipeline, the pipeline itself is restricted. When you use a pipeline to pass the output result from one command to another, the output result is used immediately. However, you may need to save the output result for a period of time so that it can be used (or reused) in the future. At this time, the pipeline should end and the variable will be used. It is easy to think of variables as a repository, but in powershell, variables can save the complete output of commands. For example, if you want to save the list of processes in the running state of the server, you can assign it to a variable, for example, $ A = Get-process. Here, the variable is named $ A. If you want to use this variable, you only need to simply call its name. For example, you can type $ A to print the variable content on the screen. You can assign the final output of multiple commands connected with pipelines to a variable. You only need to enclose the command with a pair of parentheses. For example, if you want to sort running processes by process ID and output the results to a variable, you can use the following command: $ A = (get-process | sort-Object ID)6. @ symbolBy using the @ symbol, you can convert the list content into an array. For example, the following code creates a variable named $ procs, which contains multiple lines of text (an array ): $ procs = @ {name = "Explorer", "svchost"} You can also use the @ symbol when using a variable. To ensure that it is processed as an array rather than a single value, for example, the following code runs get-process on the variable I defined earlier
Command: Get-process @ procs Windows displays all processes used by Windows Resource Manager and SVCHOST. Note the @ symbol before the variable, instead of the common $ symbol.7. SplitThe split operator Splits a text string based on the characters you specify. For example, if you want to split a sentence into an array composed of words, you can use the following command: "This is a test"-split "" the result after the split is as follows: This is a test8. JoinJust as split can split a text string into multiple blocks, the join operation is reverse, and multiple independent blocks are connected into a whole. For example, the following code creates a text string consisting of my first name and last name: "Brien ", the space between double quotation marks at the end of the "Posey"-join "Command tells windows to insert a space between two text strings.9,When a newly created powershell script is run at a breakpoint, if the script has a bug, unexpected consequences will occur. One way to protect yourself is to insert a breakpoint in the key position of the script, in this way, you can ensure that the script runs normally first and then handle possible problems. The simplest way to insert a breakpoint is to insert a breakpoint Based on the row number. For example, if you want to insert a breakpoint in row 10th, run the following command: New-psbreakpoint-script C: \ scripts \. PS1-Line 10 you can also bind the breakpoint to the variable. If you want your script to modify the content of a $ at any time, you can use the following command: new-psbreakpoint-script c: \ scripts \. PS1-variables a note that I did not include the dollar sign after the variable name. Verbs that can be used with psbreakpoint include new, get, enable, disable, and remove.10. StepWhen debugging a script, you may need to run the script line by line. You can use step-
Cmdlet command, which will execute the script one line at a time, whether or not the breakpoint is set, if you want to exit from this step-by-step running mode, use the step-out cmdlet command, note that the breakpoint is still valid after the step-out command is run. By the way, if your script uses a function, you may be more interested in the step-out cmdlet. The step-out method is the same as step-into. However, if a function is called, Windows will not execute it step by step, and the entire function will be executed at one time. Edit this powershell Section
V3 overview windows powershell V3 will be accompanied by Microsoft hyper-V 3.0 and windows
Server 2012 is released. Powershell V3 is a Windows Task automation framework. It consists of a command line shell and a programming language built into the. NET Framework. Powershell V3 uses the new cmdlet to enable administrators to drill deeper into system processes, which can be made into executable files or scripts ). A cmdlet is a lightweight command, which is called by Windows powershell during running time in an automated script environment. The cmdlet displays the get-location of the current directory, the get-content that accesses the file content, and the stop-process that ends the running process. Powershell V3 loads Windows Management Framework 3.0 in Windows Server 8. Powershell running time can also be embedded into other applications. Edit this powershell Section
Advantages and disadvantages of V3 powershell V3 advantages powershell V3 will create a majority of management on powershell, and provides GUI management options and command line Automation. V3 introduces some important new features.BetterRemote powershell remote processing has gradually become the main channel for managing and communicating on the network. More and more GUI management consoles rely on remote, so it is important for Microsoft to strengthen powershell remote. Now you can disconnect a remote session. Later, you can re-connect to the same session from the same or different computers. If the client computer crashes, the Community Technology preview version of V3 cannot disconnect the session. On the contrary, sessions are permanently closed. This is completely different from remote desktop. Remote Desktop sessions can be configured and enabled when the client crashes. In essence, powershell's new workflow construction can write something similar to the function, and use powershell to translate commands and script code to the Windows workflow technology WWF process. WWF can then manage the entire task, including fixing network faults and restarting the computer. It is a more effective and reliable way to orchestrate long-term running, complex, and multi-step tasks. If this function works with the system of the next version
Center orchestrator integration.Updatable helpPowershell struggles with errors in the Help file. Microsoft needs to release an operating system patch. The presence of online help based on the technet site has mitigated this problem. In V3, help files can be updated as needed, and new XML files can be downloaded from any Microsoft Server. Therefore, Microsoft can fix Errors Based on the problems found, without the need for OS packages or patches.Scheduled job)Owershell V2 introduces the concept of job extension over time. In V3, a new job is a scheduled job that can be created and run as planned, or an event. This is slightly different from Windows's task scheduler, but the end user can get this function from powershell.Better discoveryA difficult part of shell is how to use it. The powershell help system is very useful. You need to provide the name of the command you want to know, provide the plug-in name of the command, and remember to load the attachment to the memory. While powershell V3 searches for commands that contain all the commands of all the installation modules, shell runs commands that are not loaded. This can only be useful for those modules stored in the file path listed in the psmodulepath environment variable. If you want to include additional paths, you can modify the variables at any time.Additional features: CIMPowershell and Windows Management specifications WMI work well. WMI is a Microsoft technology and is more or less based on standard general information module CIM. In powershell
In V3, the WMI cmdlet adds the residual heat to the new CIM cmdlet set. Features seem to overlap: CIM cmdlet uses WS-MAN, which is behind the remoting feature of powershell, a new standard of Microsoft management functionality. WMI uses DCOM, which is officially deprecated by Microsoft, which means that new features will not be developed, but can be used all the time. CIM is the future direction. It not only provides additional development for known WMI, but also supports cross-platform management in the future. Powershell V3 disadvantages the new hyper-V 3.0 cmdlets cannot manage earlier versions of hyper-v. This means that the Administrator must use different scripts to manage different hyper-V hosts until the upgrade of all hosts is completed. Hyper-V 3.0 is incompatible with earlier versions. The administrator of hyper-V of earlier versions needs to download powershell library for hyper-V from codeplex.

Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.