Solved

Pass Parameters to PowerShell Function

Posted on 2014-09-16
2
190 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
Comment Utility
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
Comment Utility
Thank you, Subsun!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Synchronize a new Active Directory domain with an existing Office 365 tenant
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

10 Experts available now in Live!

Get 1:1 Help Now