TRACE-COMMND Trace Pipeline parameter passing

Source: Internet
Author: User

Today, I saw a very interesting question in powershell.com. The problem is as follows, there is a CSV file, presumably the content is like this

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7D/70/wKioL1bov12Cq2-NAAAJxMn4ZLE508.png "style=" float: none; "title=" 1.PNG "alt=" Wkiol1bov12cq2-naaajxmn4zle508.png "/>


Then why does this order not work?


Import-csv Name.csv | Get-service

Error:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/70/wKioL1bov2HwbrdZAABRYtMWCx8609.png "title=" 2.PNG " Style= "Float:none;" alt= "Wkiol1bov2hwbrdzaabrytmwcx8609.png"/>


Theoretically the pipeline should be able to pass through Byvalue or Bypropertyname, Get-service have-computername this option, the previous import CSV file also has comutername this attribute, theoretically should automatically match to AH? So where does the problem go?

Basic implementation of the pipeline see http://beanxyz.blog.51cto.com/5570417/1678609



We can use the Trace-command command to track exactly what happened.

Trace-command-pshost-name parameterbinding-expression{import-csv name.csv | get-service}

The first is Import-csv's order, no problem.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/73/wKiom1bowFCB3uaOAABnVGaWRSM955.png "title=" 3.PNG " alt= "Wkiom1bowfcb3uaoaabnvgawrsm955.png"/>

And then the pipeline is going to pass the content he gets to Get-service.


Look, I thought it would automatically match the ComputerName does not automatically match in, but it is the name of the parameter matching in! Although his first match was ignored because of a different type, the second time he automatically made a type conversion, forcing and name matching succeeded

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7D/74/wKiom1bo0huQADg0AAGFJlLHdwk665.png "title=" 4.PNG " alt= "Wkiom1bo0huqadg0aagfjllhdwk665.png"/>


What is the reason? Take a look at Get-service's help documentation. Tip: I usually use-showwindows or-online, so I can open a new window to search, but Win10 and PS5 have a bug,-show content is not perfect so I use online view.

Help Get-service-online


To search for byvalue, we can see that there are two parameters accepted, namely

-inputobject<servicecontroller[]>-name<string[]>


Search for Bypropertyname, there are also two parameters to accept this way:

-computername<string[]>-name<string[]>


Then look at the parameters of the position, found that-name is set to 1, that is, by default if we do not specify the parameters, he would think the automatic use of-name to match the corresponding string!


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7D/74/wKiom1bo0qbxFvbHAABcEQEMmxA214.png "title=" 8.PNG " alt= "Wkiom1bo0qbxfvbhaabceqemmxa214.png"/>


When the pipeline tries to match, because we do not specify the parameters, he will automatically use the-name parameter to match, although his type is not, he will automatically convert the Pscustomobject into a string to try, the type is the same, He automatically matches-name instead of-computername, because the latter doesn't have a chance to match.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/71/wKioL1bowrzSsGkbAAAnQXvGXhs280.png "title=" 7.PNG " alt= "Wkiol1bowrzssgkbaaanqxvgxhs280.png"/>


How to solve it? Very simple we need to tell him exactly which parameters and attributes to pair.

Like what

Import-csv Name.csv | Get-service-name *-computername {$_.computername}


Or

Get-service-computername ((Import-csv name.csv). ComputerName)

That's fine.


There is also a similar example (http://powershell.com/cs/blogs/donjones/archive/2011/12/10/ troubleshooting-pipeline-parameter-binding-by-peeking-inside.aspx)


This command does not work, even though the pipe is preceded and followed by the same name "ComputerName"

Get-adcomputer-filter * | Select @{n= ' computername '; E={$_.name}} |invoke-command-scriptblock {dir}


Why? Because

    1. Invoke-command's computername doesn't support plumbing at all.

    2. His-inputobject accepts any type!! This means that any pipe parameters that are attempted to be passed to Invoke-command will be taken over, and the other pipe parameter settings are useless.


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/72/wKioL1bo2ZeQRS9UAADzoUyIqBw239.png "style=" float: none; "title=" 9.PNG "alt=" Wkiol1bo2zeqrs9uaadzouyiqbw239.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/72/wKioL1bo2g6hN0zqAABVv-HXO9U776.png "title=" 10.PNG "alt=" Wkiol1bo2g6hn0zqaabvv-hxo9u776.png "/>

How to solve? Just run away from the top, and don't use the pipe.

invoke-command-scriptblock {dir}-computername (Get-adcomputer-filter * | select-expand name)


This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1751700

TRACE-COMMND Trace Pipeline parameter passing

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.