Well, a couple of things. You need to keep watching the videos, maybe. I feel like you’ve maybe skipped ahead a bit and missed some of the basics about how PowerShell manages pipeline input. When you write a pipeline function or script, it has to have a particular form.
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[string[]]$MyParam
)
PROCESS {
foreach ($Thing in $MyParam) {
# $thing will be one string object.
}
}
The PROCESS block is needed to tell the shell what bit of your script is processing pipeline objects. But because your parameter is an array, you still need to enumerate it. Now, this assumes you’re piping strings into it. A CSV isn’t a string.
But it’s really tough to help further when I’m not seeing everything. For example, I’ve no idea why Out-String is being brought into this, and depending on what you’re doing, it could be resulting in you not getting what you want. You need to pay attention to the difference between dealing with ONE of something, and dealing with a COLLECTION of somethings. You also need to understand that PowerShell likes dealing with objects, not text. Out-String produces text. When you convert an object to text, you remove much of the shell’s ability to comprehend and manipulate that object.
You should try and get out of the habit of using Out-String unless you’re in a specific situation where you NEED it, and you KNOW you need it. For example, I would construct a verbose like this:
Write-Verbose "Now dealing with $thing"
If $thing contains a string, that’s all you need to do.
But this all comes down to input. For example, suppose I have a CSV file that has a ComputerName column. I could NOT Import-CSV that and pipe it to the script, using the parameters defined here. Import-CSV would produce objects having ComputerName properties - not strings. If that Import-CSV was the goal:
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[string[]]$MyParam
)
PROCESS {
foreach ($Thing in $MyParam) {
# $thing will be one string object.
}
}
Now the -MyParam parameter can accept incoming strings, or it can accept incoming objects that have a MyParam property. Changing that:
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[string[]]$ComputerName
)
PROCESS {
foreach ($Computer in $ComputerName) {
# $Computer will be one string object.
}
}
Now the -ComputerName parameter can accept incoming strings, or it can accept incoming objects that have a ComputerName property. So, if you had a CSV file that had a ComputerName column, you could:
Import-CSV data.csv | ./MyScript
Again, assuming that CSV had a ComputerName column.
“Learn PowerShell Toolmaking in a Month of Lunches” (not to make you buy another training thing) really goes through this entire progression, also.
BUT… it’s important when you post here to show an example of each step. Like, if I have no idea what the input looks like, it’s tough for me to look at your code snippets and figure out what PowerShell is trying to do. Just a sample of “here’s what the CSV file looks like,” for example, and “here’s what the first script is doing with it,” can help a lot!