Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

User input -> reference object

8 views
Skip to first unread message

naugiedoggie

unread,
Jan 28, 2012, 12:47:41 PM1/28/12
to
Hello,

I have a function that requires the user to pass in the name of a
hashtable. This is easy if the user just uses the commandline option
to specify it, i.e. functionname -modifiers $modifiers, where
$modifiers is the name of an existing hash.

My question is, how can I prompt the user if she fails to provide the
required hash name? A prompt of the format like this returns a
string, which even though it is the name of the hash variable, is not
actually variable reference to it.

param ([string]$infile,[string]$outfile,[hashtable]$modifiers=$(read-
host "Enter the hash name"))

I tried a few things like, ${(read-host "enter hash name")} but I
can't figure out how to get past the fact that read-host returns a
string and I can't get from that string to the actual reference
variable of the same name.

Thanks for any help.

mp

David Trimboli

unread,
Feb 7, 2012, 11:42:46 AM2/7/12
to
Why are you asking for a name rather than a reference to the hash table
itself? MyFunction -HashTable $MyHashTable

PowerShell version 2 introduced metadata attributes for functions. The
Scripting Guys have a page on it:
<http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/22/use-powershell-to-make-mandatory-parameters.aspx>


Function MyFunction
{
Param(
[Parameter(Mandatory=$true)] [hashtable] $HashTable
)

# body of function
}


If you call the function without supplying the $MyHashTable parameter,
you will be prompted to enter it.

I'm not entirely sure I understood your question; I hope this helped.

--
David Trimboli
Windows Systems Analyst
Cold Spring Harbor Laboratory

naugiedoggie

unread,
Feb 10, 2012, 3:32:02 PM2/10/12
to
On Feb 7, 11:42 am, David Trimboli <trimb...@cshl.edu> wrote:
> On 1/28/2012 12:47 PM, naugiedoggie wrote:
>
>
>
>
>
>
>
>
>
> > I have a function that requires the user to pass in the name of a
> > hashtable.  This is easy if the user just uses the commandline option
> > to specify it, i.e. functionname -modifiers $modifiers, where
> > $modifiers is the name of an existing hash.
>
> > My question is, how can I prompt the user if she fails to provide the
> > required hash name?  A prompt of the format like this returns a
> > string, which even though it is the name of the hash variable, is not
> > actually variable reference to it.
>
> > param ([string]$infile,[string]$outfile,[hashtable]$modifiers=$(read-
> > host "Enter the hash name"))
>
> > I tried a few things like, ${(read-host "enter hash name")} but I
> > can't figure out how to get past the fact that read-host returns a
> > string and I can't get from that string to the actual reference
> > variable of the same name.
>
> Why are you asking for a name rather than a reference to the hash table
> itself? MyFunction -HashTable $MyHashTable
>
> PowerShell version 2 introduced metadata attributes for functions. The
> Scripting Guys have a page on it:
> <http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/22/use-pow...>
>
> Function MyFunction
> {
>    Param(
>      [Parameter(Mandatory=$true)] [hashtable] $HashTable
>    )
>
>    # body of function
>
> }
>
> If you call the function without supplying the $MyHashTable parameter,
> you will be prompted to enter it.
>
> I'm not entirely sure I understood your question; I hope this helped.
>
> --
> David Trimboli
> Windows Systems Analyst
> Cold Spring Harbor Laboratory

Hello,

Thanks for the reply. I believe that you have provided me with the
information I needed. I am not familiar with the new parameter
attribute and it looks like it does what I asked -- get me a reference
to the hashtable object.

The point was to enable the function to be run even if the user
neglected to input all the necessary information.

Thank you.

mp
0 new messages