filmov
tv
PowerShell S2E17 (ValueFromPipelineByPropertyName)
Показать описание
*How to use ValueFromPipelineByPropertyName with alias in powershell
*ValueFromPipelineByPropertyName
*pipeline
In this video I teach you how to accept input from the pipeline based on the property name of the object you passing on the pipeline. I also teach you a trick with Alias inside of the parameter, this makes it even more conveniant.
Code :
function test {
Param(
[parameter(ValueFromPipelineByPropertyName)]
[Alias('IPAddress','Server','CN','Computername2')]
[string[]]$Computername
)
echo "Do something on : $ComputerName"
}
$t0 = [PSCustomObject]@{
Computername2 = "server12"
prop2 = "nothing"
}
$t1 = [PSCustomObject]@{
Computername = "server1"
prop2 = "nothing"
}
$t2 = [PSCustomObject]@{
Server = "server2"
prop2 = "nothing"
}
$t3 = [PSCustomObject]@{
IPAddress = "10.0.0.5"
prop2 = "nothing"
}
$t4 = [PSCustomObject]@{
prop2 = "nothing"
}
$t0, $t1, $t2, $t3, $t4 | ForEach-Object {
$_ | test
}
*ValueFromPipelineByPropertyName
*pipeline
In this video I teach you how to accept input from the pipeline based on the property name of the object you passing on the pipeline. I also teach you a trick with Alias inside of the parameter, this makes it even more conveniant.
Code :
function test {
Param(
[parameter(ValueFromPipelineByPropertyName)]
[Alias('IPAddress','Server','CN','Computername2')]
[string[]]$Computername
)
echo "Do something on : $ComputerName"
}
$t0 = [PSCustomObject]@{
Computername2 = "server12"
prop2 = "nothing"
}
$t1 = [PSCustomObject]@{
Computername = "server1"
prop2 = "nothing"
}
$t2 = [PSCustomObject]@{
Server = "server2"
prop2 = "nothing"
}
$t3 = [PSCustomObject]@{
IPAddress = "10.0.0.5"
prop2 = "nothing"
}
$t4 = [PSCustomObject]@{
prop2 = "nothing"
}
$t0, $t1, $t2, $t3, $t4 | ForEach-Object {
$_ | test
}
Комментарии