Author Posts

August 20, 2013 at 10:14 am

Hi all,
I'm new to powershell so maybe it's easy.

I have a parameter ComputerName which has the following parameter properties:

# Computer Name oder mehrere Computer Namen
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[Alias("hostname","Name")]
[String[]]$ComputerName

Now I learnt (I do hope that I get this right) that when passing objects via the pipeline the process block will be automatically looped for every object coming from the pipeline. Do I have this right?

However, when I use get-something -ComputerName client1, client2 etc I need to loop through the "array" of $ComputerName.

Can I determine in the Begin block if the script is used as a pipeline "target"?

Thanks

August 20, 2013 at 10:18 am

Correct. When input is piped in, $Computername will only contain one item at a time. If provided on the parameter, PROCESS executes once and $ComputerName contains all items.

You don't need to determine it, though. Just build the PROCESS block to handle both.

PROCESS {
 foreach($computer in $computername) {
   # use $computer
 }
}

In the pipeline mode, the foreach is redundant but doesn't add much extra time since it only has to loop once. Either way, $computer will only contain one name at a time, this way.

August 20, 2013 at 11:31 am

OK.
Took me some time from home to further explain my problem and maybe I have found some info for myself.

If I use this command:

$myVar = Get-ADComputer -Filter {OperatingSystem -NotLike "*server*"} -Properties * | select Name

The content of $myVar | gm shows this:

ypeName: Selected.Microsoft.ActiveDirectory.Management.ADComputer

Name MemberType Definition
—- ———- ———-
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Name NoteProperty System.String Name=SomeName

So a piped Get-ADComputer -Filter {OperatingSystem -NotLike "*server*"} -Properties * | select Name still has something like "Name=SomeName" in it.
Doing a $myVar[-1].Name | gm will give me only the string name and not "Name=SomeName".

What would I need to change either in the process code block or in the calling pipe command?

Get-ADComputer -Filter {OperatingSystem -NotLike "*server*"} -Properties * | select Name | something | myscript

Thanks a lot because I now understand that foreach loop and why it would still be functioning.

Oh, here's what I did:

Get-ADComputer -Filter {OperatingSystem -NotLike "*server*"} -Properties * |Select -ExpandProperty Name| myscript
Is this the right way?

August 20, 2013 at 12:16 pm

When you use Select-Object, you're still producing a complex object with properties, as you've seen.

Get-ADComputer -filter * | Select -Expand Name | My-Function

Is one option. Extract (expand) the contents of the Name property so that it hits your function ByValue. Or,

Get-ADComputer -filter * | Select @{n='computername';e={$_.name}} | My-Function

Produces an object having only a ComputerName property, which hits your function ByPropertyName.

All in "Learn Windows PowerShell 3 in a Month of Lunches," if you're interested :).

August 20, 2013 at 8:57 pm

Thanks a lot 🙂

August 20, 2013 at 10:52 pm

It was late yesterday and now I could see the ByValue and ByPropertyName!
Will check this one: Get-ADComputer -filter * | Select @{n=’computername’;e={$_.name}} | My-Function

By the way, couldn't resist buying the book. I have watched the 2 introduction events from Jeffrey Snower and Jason Helmick and for sure this was mentioned 🙂

Many more questions for new threads but I may solve some on my own 😉