powershell - What is conceptually wrong with get-date|Write-Host($_) -
i'm trying understand powershell, find somethings not intuitive. understand of in pipeline objects passed, instead of traditionally text. , $_ refers current object in pipeline. then, why following not working:
get-date|write-host "$_"
the errormessage is:
write-host : input object cannot bound parameters command either because command not take pipeline input or input , properties not matc h of parameters take pipeline input. @ line:1 char:10 + get-date|write-host $_ + ~~~~~~~~~~~~~ + categoryinfo : invalidargument: (10-9-2014 15:17:00:psobject) [write-host], parameterbindingexception + fullyqualifiederrorid : inputobjectnotbound,microsoft.powershell.commands.writehostcommand
$_
current single item in pipeline. write each item in pipeline write
get-data | foreach { write-host $_ }
or in short form
get-data |% { write-host $_ }
conceptually, foreach cmdlet receives function parameter, pipeline input , applies function on each item of pipeline. can't write code $_
- need have function explicitly states agrees receive pipeline input
Comments
Post a Comment