Windows PowerShell: complete the operation through commands instead of scripts

Source: Internet
Author: User

Cognition has always been the biggest problem we face when Windows PowerShell strives to be accepted by administrators. For a long time, the Administrator's perception of the shell is that it is similar to VBScript as a "scripting language ". Although scripting languages can be used by many administrators to complete a large number of operations, many administrators are also discouraged by their complexity and steep learning curves.

This is a pity. The shell supports powerful script-based functions, but it also supports simpler and command-oriented functions. The real attraction of this shell is that you can use any of the above methods to do a lot of the same work.

Just a script

The following function accepts the computer name from the command line in the form of a string or the "ComputerName" attribute of the input object; it also uses Windows Management Instrumentation (WMI) retrieve BIOS and OS information from each computer.

function Get-Inventory{   [CmdletBinding()]   Param(       [Parameter(Mandatory=$true,                 ValueFromPipeline=$true,                 ValueFromPipelineByPropertyName=$true)]       [string] $computername   )   Process {      $os = gwmi win32_operatingsystem -computername $computername      $bios = gwmi win32_bios -computername $computername      $obj = new-object psobject      $obj | add-member noteproperty ComputerName $computername      $obj | add-member noteproperty OSBuild ($os.buildnumber)      $obj | add-member noteproperty SPVersion ($os.servicepackmajorversion)      $obj | add-member noteproperty BIOSSerial ($bios.serialnumber)      Write-output $obj   }}

Note that parentheses force shell to execute an expression, for example, obtaining the BuildNumber attribute from the $ OS variable object), and return the result of this expression as the third parameter value of Add-Member.

You can also enter a static computer name in the MPs queue to run this function:

'localhost','server2' | Get-Inventory

Alternatively, you can run this function by sending the content of a text file containing a computer name in each line.

Get-Content names.txt | Get-Inventory

Alternatively, you can even retrieve computer objects from Active Directory, change the name attribute to ComputerName, and transmit the following content through a pipeline:

Import-Module ActiveDirectoryGet-ADComputer –filter * | Select-Object @{Label='ComputerName';Expression={$_.Name}} | Get-Inventory

In addition, I use parentheses to encapsulate executable code. $ _ Placeholder represents the Object for which the Select-Object cmdlet is input through a pipeline. The results of the preceding operations are concise tables with four columns. I can easily redirect the above output to a file, printer, or grid, or even filter and sort the results before they are displayed. For example,

Get-Content names.txt | Get-Inventory | Where { $_.BuildNumber –eq 7600 } | Sort ComputerName

Again, parentheses encapsulate an executable code block, that is, the expression I want to filter, and the $ _ placeholder represents the object passed in through the pipeline.

Command Performance

Similar scripts do not have errors, but they need to handle a lot of work. Many administrators consider this scripting method too heavy. The same task can be completed using a slightly complex command. Fighting Spirit:

Get-WmiObject Win32_OperatingSystem -computername (get-content names.txt) | Select-object @{Label="ComputerName";Expression={$_.__SERVER}},             @{Label="OSBuild";Expression={$_.BuildNumber}},             @{Label="SPVersion";Expression={$_.ServicePackMajorVersion}},             @{Label="BIOSSerial";Expression={(gwmi win32_bios -comp $_.__server).serialnumber}}

Many operations are required here. The following is a detailed description of the operation:

To some extent, this syntax is more difficult to read than the script I started. It is compact and uses a large number of punctuation marks. You can use it as a template and modify it as needed. If you do not understand why it cannot work, please ask questions on my ConcentratedTech.com blog, and I will answer questions for you.

Do not call it a script

I mean we don't have to use Windows PowerShell as a scripting language. The commands I demonstrated may be complex, but they will not be more complex than the lengthy commands that my administrator has compiled for the legacy Cmd.exe shell. Although some training is required, this command is much simpler than writing a complete script or function after you are familiar with its syntax.

Therefore, you do not want to use the shell if you see the "script language. You can choose to write simple functions using scripts.

Don Jones He is the founder of Concentrated Technology and will answer questions about Windows PowerShell and other technologies at ConcentratedTech.com. He is also a contributor to Nexus.Realtimepublishers.com. Many of his works are also provided on his website in electronic form.

Original article address

View more articles

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.