none
How to pass 2 variables to a function in Powershell RRS feed

  • Question

  • Hi All,

    I am trying to pass the  two variables to a function in Powershell.
    The variables are being generated from two button clicks and they are called $ChosenItem1 and $ChosenItem2.
    It seems that I can only get one of the variables to pass in with below code:



    Function Give-Access($ChosenItem1,$ChosenItem2)
    {
    Add-MailboxPermission -Identity "$ChosenItem1" -User "$ChosenItem2" -AccessRights FullAccess -InheritanceType All
    }



    Using above code, variable $ChosenItem1 gets passed into the function but not $ChosenItem2.
    I can only pass on variable $ChosenItem2 by writing the funtion as below:



    Function Give-Access($ChosenItem2)
    {
    Add-MailboxPermission -Identity "$ChosenItem1" -User "$ChosenItem2" -AccessRights FullAccess -InheritanceType All
    }



    I have spent hours on this but just cannot get it to work.



    • Edited by sabedin77 Sunday, October 7, 2018 5:27 PM
    Sunday, October 7, 2018 3:13 PM

Answers

  • First I will format your code so it is easier to read and so that you can see the issue I am trying to show you.

    # Define Button 1  - This is the selection screen's OK button
    $Button1 = new-object System.Windows.Forms.Button
    $Button1.Location = new-object System.Drawing.Size(20,170)
    $Button1.Size = new-object System.Drawing.Size(70,30)
    $Button1.BackColor ="LightGray"
    $Button1.Text = "OK"
    $Button1.Add_Click({
        $ChosenItem1=$ListBox1.SelectedItem
        Display-Mailboxes
    })
      
    # Define Button 3 - This is the mailbox that needs the access
    $Button3 = New-Object System.Windows.Forms.Button
    $Button3.Location = New-Object System.Drawing.Size(50,220)
    $Button3.Size = New-Object System.Drawing.Size(70,30)
    $Button3.BackColor ="LightGray"
    $Button3.Text = "Button2"
    $Button3.Add_Click({
        $ChosenItem2=$ListBox1.SelectedItem
        Give-Access $ChosenItem1,$ChosenItem2  <<<<<WRONG SYNTAX AND SCOPE issues
    })

    Since you are a trained programmer you should now immediately understand why your code does not work.

    Passing variables to Exchange does NOT require quotes.  I have been programming Exchange since the first Microsoft version.

    The reason quotes are not needed is because of how arguments are managed in all programming languages including Java,  In most cases in PowerShell using them will not hurt but the habit will cause you problems in the future similar to the issues you are currently having.


    \_(ツ)_/

    • Marked as answer by sabedin77 Wednesday, October 10, 2018 6:49 PM
    Sunday, October 7, 2018 4:26 PM

All replies

  • First it is never necessary to place quotes around a variable tha tis being passed.

    Second - it is most likely you are trying to pass a variable that is out of scope.

     help about_scopes


    \_(ツ)_/

    Sunday, October 7, 2018 3:18 PM
  • Quotes are required as this is an Exchange Script and the variables are names with gaps in them.

    It is not out of scope as both variables can be "seen" within the function- just not both at once


    • Edited by sabedin77 Sunday, October 7, 2018 3:42 PM
    Sunday, October 7, 2018 3:41 PM
  • Quotes are required as this is an Exchange Script and the variables are names with gaps in them.

    It is not out of scope as both variables can be "seen" within the function- just not both at once


    That is not how variables in any programming language work.  This is something that shows you have no programming experience and have not learned the basics of Power Shell.

    I think you also need to spend a bit more time trying to understand scope. 

    It is also possible that you are just passing the variables incorrectly. Parameters must be passed with correct syntax.   How are you passing the variables into the function?


    \_(ツ)_/

    Sunday, October 7, 2018 3:51 PM
  • With all due respect I do have some programming experience(2-250 hours study in C/Java but 15 years ago) but nothing in the real world.

    I am not sure whether you know much about Exchange but without the quotes, this specific Powershell command would not work. Well aware that quotes are not required on a variable per se.

    Scripts to generate variables are below:

    # Define Button 1  - This is the selection screen's OK button

    $Button1 = new-object System.Windows.Forms.Button

    $Button1.Location = new-object System.Drawing.Size(20,170)

    $Button1.Size = new-object System.Drawing.Size(70,30)

    $Button1.BackColor ="LightGray"

    $Button1.Text = "OK"

    $Button1.Add_Click({$ChosenItem1=$ListBox1.SelectedItem;Display-Mailboxes})

    Above is variable $ChosenItem1

     

    # Define Button 3 - This is the mailbox that needs the access

    $Button3 = New-Object System.Windows.Forms.Button

    $Button3.Location = New-Object System.Drawing.Size(50,220)

    $Button3.Size = New-Object System.Drawing.Size(70,30)

    $Button3.BackColor ="LightGray"

    $Button3.Text = "Button2"

    $Button3.Add_Click({$ChosenItem2=$ListBox1.SelectedItem;Give-Access $ChosenItem1,$ChosenItem2})

    So this creates variable $ChosenItem2 and passes both variables to the Give-Access function.

    Like I say I can get either of the two variables "seen" in the Give-Access function but not both - this is why I do not think this is anything to do with scoping.

    I need both variables to be passed to allow the script to work.



    • Edited by sabedin77 Sunday, October 7, 2018 4:10 PM
    Sunday, October 7, 2018 4:07 PM
  • First I will format your code so it is easier to read and so that you can see the issue I am trying to show you.

    # Define Button 1  - This is the selection screen's OK button
    $Button1 = new-object System.Windows.Forms.Button
    $Button1.Location = new-object System.Drawing.Size(20,170)
    $Button1.Size = new-object System.Drawing.Size(70,30)
    $Button1.BackColor ="LightGray"
    $Button1.Text = "OK"
    $Button1.Add_Click({
        $ChosenItem1=$ListBox1.SelectedItem
        Display-Mailboxes
    })
      
    # Define Button 3 - This is the mailbox that needs the access
    $Button3 = New-Object System.Windows.Forms.Button
    $Button3.Location = New-Object System.Drawing.Size(50,220)
    $Button3.Size = New-Object System.Drawing.Size(70,30)
    $Button3.BackColor ="LightGray"
    $Button3.Text = "Button2"
    $Button3.Add_Click({
        $ChosenItem2=$ListBox1.SelectedItem
        Give-Access $ChosenItem1,$ChosenItem2  <<<<<WRONG SYNTAX AND SCOPE issues
    })

    Since you are a trained programmer you should now immediately understand why your code does not work.

    Passing variables to Exchange does NOT require quotes.  I have been programming Exchange since the first Microsoft version.

    The reason quotes are not needed is because of how arguments are managed in all programming languages including Java,  In most cases in PowerShell using them will not hurt but the habit will cause you problems in the future similar to the issues you are currently having.


    \_(ツ)_/

    • Marked as answer by sabedin77 Wednesday, October 10, 2018 6:49 PM
    Sunday, October 7, 2018 4:26 PM
  • Thanks for the above and apologies if I implied you did not understand Exchange.

    There probably is a much better way to program for Exchange but this is the only way I can get it to work for me at this stage

    Can you advise what you mean by wrong syntax?

    From what I can see both variables are being passed to the Give-Access function but only one can be "seen" at any one time by it.

    I was thinking along the lines of the Give-Access function itself having the wrong syntax - Function Give-Access($ChosenItem1,$ChosenItem2) but it seems this is not where the issue is?


    Sunday, October 7, 2018 5:19 PM
  • Script blocks called in a forms event are like functions.  They are called delegates and are execute in a separate scope.  All variables defined in a delegate are local only.  You cannot use a local variable in one function in a second function (delegate).

    "Parameters" are the things we define on a function.  We pass values to these parameters as arguments on a procedure call.

    All functions in PowerShell are called "commands".  They all use the same "command" syntax.  Arguments to all commands are passed by position or by name. 

    All lists of values or objects become arrays when separated by commas.  You are passing an array and not separate arguments.  The external argument being passed does not exist outside of the "delegate" that defined it.

    help about_scope


    \_(ツ)_/

    Sunday, October 7, 2018 5:33 PM
  • Thank you.

    Looks like I got a lot of reading to do before I can get this to work.

    Appreciate your input!



    • Edited by sabedin77 Sunday, October 7, 2018 8:32 PM
    Sunday, October 7, 2018 8:32 PM
  • Why?   Just start with a basic tutorial in PowerShell.  Everything else will be easy once you understand the basics of programming with PowerShell.

    As for scopes; scopes are pretty much the same in all languages. You cannot directly change the value of a variable defined in an outer scope.  In PowerShell we have the luxury of using scope  identifiers which are documented in the "help" I posted. Its a five minute read.

    Also variable created inside functions do not exist outside of the function.  You can set their visibility with the "scope"  identifier. 

    Here is a good simple and fun tutorial on PowerShell: Learn PowerShell  

    PowerShell syntax is mostly based on C#,  it is not exact and has more syntax features than C#.  Concepts from C and Java will only mislead you if you are not an experienced programmer.


    \_(ツ)_/

    Sunday, October 7, 2018 8:52 PM
  • Thank you so much as I now have the script working!

    Have now realized just how much I have forgotten from when I did C/Java around 15 years ago!

    This would have been 2nd nature to me back then.

    Wednesday, October 10, 2018 6:50 PM