Solved

Pass Parameters to PowerShell Function

Posted on 2014-09-16
2
191 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A short film showing how OnPage and Connectwise integration works.

930 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now