Solved

Pass Parameters to PowerShell Function

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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