1
I want to run multiple custom processing steps using the pipeline in powershell.
In linux I could do something like this:
listServers.sh | <some commands to filter servers> | commandToApplyToEachServer.sh
And put code relevant to each step in different scripts (each command could take a number of parameters and at least the last one could be replaced by a different script entirely)
I would like to do something similar in powershell, without adding any "overhead" (having to source scripts before using their functions, having to re-source scripts if there have been changes etc)
Running the commands directly at the prompt is easy:
"server1","server2" | % {Get-Service -Name myservicename -ComputerName $_ | Set-Service -Status Stopped }
...but I want to enable users (and myself) an easier & more robust way of doing this and similar commands (basically wrapping each step in a script or function, whatever is the most powershelly way of doing things).
The closest I've gotten is this:
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
$env
)
Function global:srv {
Begin {
}
Process {
Get-Service -Name someServiceName -ComputerName $_
}
End {
}
}
Get-Content "$env.csv"
Which allows me to run things like:
.\listServers.ps1 appServers | srv
But unfortunately the srv function gets updated only AFTER the entire command has finished running (so any changes to the echo-pipe function will not take effect until the second time I run it, and of course I need to source listServers.ps1 before I can do anything at all, because srv doesnt exist until then)
UPDATE: I've gone forward using a single-script approach (using some parameters to handle filtering and control which command I want to be executed in the end, not using pipeline). But it would still be interesting to know if it is possible to use the pipeline.
Is there a reason that you can't pass the server names as parameters normally? – soandos – 2014-09-17T12:38:12.033
Yes, there could be 10+ servers and I want to read them from a file. – Cyberwiz – 2014-09-17T14:13:29.933
WHy not write a function that reads the file and then passes it as an array of server names? – soandos – 2014-09-17T14:20:56.610
I'm not sure how you mean, could you elaborate? – Cyberwiz – 2014-09-17T14:31:39.080