header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Named input parameters
Last Post 26 Oct 2007 07:27 PM by Josiahwww. 9 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
JosiahwwwUser is Offline
New Member
New Member
Posts:15
Avatar

--
26 Oct 2007 05:42 PM  
Does PS support named inupt paramters or is these a feature only available to commandlets.

it would be nice to call the following script using -comp "computer name"

param ([string] $computer = $(throw "computer name is required"))
Write-Host $computer
bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
26 Oct 2007 05:52 PM  
You just did it :)

Using the Param statement at the beginning of the script.

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
JosiahwwwUser is Offline
New Member
New Member
Posts:15
Avatar

--
26 Oct 2007 07:08 PM  

What?

i want to run my script using PS C:\test.ps1 -comp "computer name"

using the following code does not support this
param ([string] $computer = $(throw "computer name is required"))
Write-Host $computer

Am I missing something please explain.

Thanks
Josiah

JosiahwwwUser is Offline
New Member
New Member
Posts:15
Avatar

--
26 Oct 2007 07:11 PM  

Maybe this makes more sense. How do I capture the value of -comp

Josiah

bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
26 Oct 2007 07:15 PM  
Script test.ps1
######################
param ([string] $computer = $(throw "computer name is required"))
Write-Host $computer
######################

Now call it like you want
PS C:\test.ps1 -comp "computer name"
Computer Name

Maybe I dont understand your question.
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
JosiahwwwUser is Offline
New Member
New Member
Posts:15
Avatar

--
26 Oct 2007 07:17 PM  

ok, i got it. It is using the name of the variable you are passing it to.

so if you have the following
param ([string] $myname = $(throw "computer name is required"))
Write-Host $myname

then you need to use myscript.ps1 -myname "myname"

or if you have 
param ([string] $myfile = $(throw "computer name is required"))
Write-Host $myfile

myscript.ps1 -myfile "myfile"

My original questions had just -comp and the variable name was $computer not $comp. This is why i was confused.

Josiah

bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
26 Oct 2007 07:21 PM  
That is correct, but positional rules still apply and you only have to type enought to be unique.

So -Comp is the same as -Computer

So you can do any of the following assume Param($computer = ...)
PS C:\test.ps1 -computer "computer name"
PS C:\test.ps1 -comp "computer name"
PS C:\test.ps1 -c "computer name"
PS C:\test.ps1 "computer name"
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
JosiahwwwUser is Offline
New Member
New Member
Posts:15
Avatar

--
26 Oct 2007 07:22 PM  
If when i called it using just -comp it was throwing the exception. It need to match the variable name. Or at least this is what i'm finding in my testing.

Script test.ps1
######################
param ([string] $computer = $(throw "computer name is required"))
Write-Host $computer
######################

Now call it like you want
PS C:\test.ps1 -comp "computer name"
Computer Name

THIS WILL FAIL

It appears you need to call it like this
PS C:\test.ps1 -computer "computer name"
Computer Name

Please correct if i'm wrong but this it what is happening on my end.

Josiah
JosiahwwwUser is Offline
New Member
New Member
Posts:15
Avatar

--
26 Oct 2007 07:25 PM  

Ok, that is very cool. It is working as you describe. I'm not sure why that first example failed when i ran. Otherwise i would not have posted this.

Josiah

JosiahwwwUser is Offline
New Member
New Member
Posts:15
Avatar

--
26 Oct 2007 07:27 PM  
i should probably take a break now.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer