none
Powershell: options for a function, troubleshoot RRS feed

  • Question

  • I've got this script get-userrds which searches for a user on a remote desktop server farm.
    It should run with a samaccount.

    Therefore I have two options get-userrds -user and get-userrds -FindSamAccountName
    *The -user will just query the user on rds-server
    *The -FindSamAccountName will show a table in which all users are listed with a number before. The user can then pick a number and the query will be executed to that users samaccountname

    Now I fail to make this function work
    There are two parameters $username and $FindUserSamAccountName

    when I use it like this and run get-userrds -username doe
    if ($FindUserSamAccountName -neq '')
    {}

    if ($username -neq '')
    {}

    It just runs the first option, the -FindUserSamAccountName instead.

    Please advise.
    J.

    Jan Hoedt

    Thursday, April 21, 2016 1:21 PM

Answers

  • It sounds like you are asking about parameter sets, but this is a bit of an advanced topic.


    PS C:\> help about_Functions_Advanced_Parameters

    You can also find information by searching; e.g.:

    http://www.google.com/search?&q=powershell+parameter+set

    Unfortunately a forum is not interactive enough for us to give you a comprehensive tutorial on how to use parameter sets.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by janhoedt Thursday, April 21, 2016 3:30 PM
    Thursday, April 21, 2016 2:14 PM
    Moderator
  • Thanks, was not that difficult.

        param
        (
            [Parameter(Mandatory=$False, ParameterSetName = 'UserName')]
            [string]$UserName = '*',


            [Parameter(Mandatory=$False, ParameterSetName = 'SamAccountName')]
            [string]$FindUserSamAccountName
            
        )

     if ($PSCmdlet.ParameterSetName -eq 'UserName')
         {}

     if ($PSCmdlet.ParameterSetName -eq 'SamAccountName')
         {}


    Jan Hoedt

    • Marked as answer by janhoedt Thursday, April 21, 2016 4:21 PM
    Thursday, April 21, 2016 3:30 PM

All replies

  • As usual we have no idea what you are trying to ask. You have too many different names foe the parameters and a very weak description of your problem,.


    \_(ツ)_/

    Thursday, April 21, 2016 1:54 PM
  • Ok, I'm working on the correct definition of my question(s).

    If I want to run a function get-rdsuser with 2 possible options:
    1.get-rdsuser -user samaccountname which will just run the command
    2.get-rdsuser -findsamaccountname, let's say get-rdsuser -findsamaccountname doe which will trigger a list of all users starting with doe, then picking the number will execute the command

    It would look like
    Please select your user
    1.John Doe Jdoe
    2.Erik Doe EDoe
    3.Frank Doel FDoel

    The two options don't work in the same function and that's what I want to make work by using:

    if ($FindUserSamAccountName -neq '') {thenrun the option to show the menu}
    if{$user -neq '') {then run the default option}

    To put it different: how can I make a function with 2 parameters work in that way that when I chose parameter 1 it will run only that parameter not parameter 2 and vice versa?

    I hope this is clear. Otherwise I'll have to draw my conclusion and figure it out myself again I guess.


    Jan Hoedt

    Thursday, April 21, 2016 2:08 PM
  • It sounds like you are asking about parameter sets, but this is a bit of an advanced topic.


    PS C:\> help about_Functions_Advanced_Parameters

    You can also find information by searching; e.g.:

    http://www.google.com/search?&q=powershell+parameter+set

    Unfortunately a forum is not interactive enough for us to give you a comprehensive tutorial on how to use parameter sets.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by janhoedt Thursday, April 21, 2016 3:30 PM
    Thursday, April 21, 2016 2:14 PM
    Moderator
  • Thanks, was not that difficult.

        param
        (
            [Parameter(Mandatory=$False, ParameterSetName = 'UserName')]
            [string]$UserName = '*',


            [Parameter(Mandatory=$False, ParameterSetName = 'SamAccountName')]
            [string]$FindUserSamAccountName
            
        )

     if ($PSCmdlet.ParameterSetName -eq 'UserName')
         {}

     if ($PSCmdlet.ParameterSetName -eq 'SamAccountName')
         {}


    Jan Hoedt

    • Marked as answer by janhoedt Thursday, April 21, 2016 4:21 PM
    Thursday, April 21, 2016 3:30 PM