Can I always use an array in –ComputerName parameter ?


A friend asked me something very interesting this week. He read in a blog that you can pass an array of servers for any cmdlet that has the computername parameter. Your question was if this is exactly how it works.

The answer is NO (or it depends – I just love to say that..I am DBA LOL)

It is true that most of the cmdlets that have the –computername paratemeter accepts an array of computer names , but not all of them. What it really matters is : . what counts to a cmdlet to receive an array it is the data type of the parameter in the cmdlet itself, and not  the caller (or “who”is calling the cmdlet) .

Lets  check the Get-Process cmdlet :

I can pass a computer or a list of computers in the –computername parameter :

Get-Process –computername Yoda

or

Get-Process –computername Yoda, R2D2, C3PO

or

$MyServers = ‘Yoda’, ‘R2D2’, ‘C3PO’

Get-Process –computername  $MyServers

or even a list of servers in a text file :

$Myservers = get-content c:\temp\myservers.txt
Get-Process –computername  $MyServers

or

Get-Process –computername (get-content c:\temp\myservers.txt)

But what makes this possible it is because the –computername parameter in the Get-Process cmdlet accepts an array of string. How can I know that ?

Simple , just use the get-help and check if the parameter have the [] in your data type (it means that you can pass an array). if we take a look at the Get-Process

image

We will see that .

But all cmdltes that have computername parameter is an array ? NO…lets run a script that looks for –computername parameters in all cmdlets and see what is it data type:

   1: Get-Command -CommandType Cmdlet -ParameterName ComPutername | % {

   2:     $CmdletName = $_.name

   3:     $CmdletParameters = ((Get-Command $CmdletName -ErrorAction SilentlyContinue).ParameterSets).Parameters |

   4:     Where-Object { $_.Name -eq 'ComputerName'} |

   5:     Select -First 1 ParameterType, Name

   6:     #Posh 3.0 and 2.0

   7:     New-Object Psobject -Property @{    CmdletName = $CmdletName;

   8:                                         CmdletParameterName = $CmdletParameters.Name

   9:                                         CmdletParameterType = $CmdletParameters.ParameterType

  10:                                     }

  11:  

  12:  

  13:     #Posh 3.0 Only

  14:     #[pscustomobject]@{CmdletName = $CmdletName;CmdletParameterName = $CmdletParameters.Name;CmdletParameterType = $CmdletParameters.ParameterType    }                             

  15:  

  16: }

and the ouptut is :
image

and we can see that NOT OF ALL computername parameters accepts an array. If you try, you will fail : Lets try with Write-EventLog

   1: New-EventLog  -Source 'TestComputerName' -LogName 'Application' -ComputerName yoda

   2: Write-EventLog -EntryType 'Error'   -LogName 'Application'  -Message 'TestComputerName' -Source 'TestComputerName'  -EventId 500 -ComputerName yoda,c3po,R2d2

and :

1

 

So..the answer is NOOOOOOOOOOOOOOOOOO !!!!!!

Always , Always use the help of the cmdlet. For a million of reasons, not only for that. Doing that, you also may check that you are using,unfounded,  a pipeline to where-object   , because there is a parameter that makes the filter to you.

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Powershell, SQL SERVER EM GERAL, Virtual Pass BR. Bookmark the permalink.

1 Response to Can I always use an array in –ComputerName parameter ?

  1. Have you ever thought about producing a great e-book or guest authoring on other personal blogs? I have a blog considering on the same thoughts you examine and so want to have you ever talk about a few stories/information. I know the visitors could enjoy your hard work. In case you are even remotely fascinated, y shoot me an email.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s