Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Pass Parameters to PowerShell Function

Posted on 2014-09-16
2
193 Views
Last Modified: 2014-09-16
Hi.  I wrote the following PowerShell script:

function MyFunction
{
    param( [string] $title   ,
           [string] $message )
           
    & Write-Host ('Title  : ' + $title  )
    & Write-Host ('Message: ' + $message)
}
MyFunction('My Title', 'My Message')

Open in new window


expecting the following output:

Title  : My Title
Message: My Message

Open in new window


Instead, however, I get the following output:

Title  : My Title My Message
Message: 

Open in new window


Could you please tell me what I am doing wrong?  Thank you.
0
Comment
Question by:deatc
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40325734
MyFunction ('My Title', 'My Message') is not the correct way to pass values to your function. In your case the function will consider ('My Title', 'My Message') as the single value and pass it to first parameter -title.

Try..
MyFunction 'My Title'  'My Message'

Open in new window

Or
MyFunction -title 'My Title' -message 'My Message'

Open in new window

Both will give you desired results..
0
 

Author Closing Comment

by:deatc
ID: 40326283
Thank you, Subsun!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question