Solved

Pass Parameters to PowerShell Function

Posted on 2014-09-16
2
194 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
[X]
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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Set OWA language and time zone in Exchange for individuals, all users or per database.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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