You can use a pipeline with the ForEach-Object
cmdlet (%
is its built-in alias) as follows:
"Node Id is $($nodeProps["NodeID"])" | % { Write-Host $_; LogWrite $_ }
Note how the reference to $nodeProps["NodeID"]
needs to be enclosed in $(...)
to work.
If you need this repeatedly, define a (simple) function:
function writeAndLog([string] $msg) { Write-Host $msg; LogWrite $msg }
writeAndLog "Node Id is $($nodeProps["NodeID"])"
P.S.: Prompted by campbell.rw's comments:
If you don't like the idea of using ForEach-Object
- which is typically used to loop over multiple input objects - on a single input item, you can use a script block, with &
, the call operator:
& { Write-Host $args[0]; LogWrite $args[0] } "Node Id is $($nodeProps["NodeID"])"
$args
is the collection of all (unbound) arguments, so $args[0]
refers to the first one. (In this particular case, just referencing $args
would have worked too.)