# Need help with function parms

**URL:** <https://forums.powershell.org/t/need-help-with-function-parms/19041>\
**Category:** PowerShell Help\
**Created:** [April 8, 2022, 6:44am UTC](https://forums.powershell.org/t/need-help-with-function-parms/19041 "2022-04-08T06:44:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hudzon](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@hudzon](https://forums.powershell.org/u/hudzon)\
**Post date:** [April 8, 2022, 6:44am UTC](https://forums.powershell.org/t/need-help-with-function-parms/19041/1 "2022-04-08T06:44:46Z")

</div>

I have a function that is using if statements for the parameter passed  
it works fine if the parameter and value are given  
but if the parameter is not passed and just the value given it will just error  
even though I have a default set.

Is there a way to make an if statement on the value itself?

like if the parameter value -match “@” it equals the parameter $email else it equals the parameter $SAN

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 8, 2022, 7:02am UTC](https://forums.powershell.org/t/need-help-with-function-parms/19041/2 "2022-04-08T07:02:04Z")

</div>

Hudzon,  
Welcome back to the forum. Long time no see. 👋🏽 😉

Please share your code and error messages you might get.

When you post code, sample data, console output or error messages please format it as code using the _preformatted text_ button ( **\</\>** ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) ← Click it !!

---

<div class="post-metadata">

**Author:** ![hudzon](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@hudzon](https://forums.powershell.org/u/hudzon)\
**Post date:** [April 8, 2022, 8:39pm UTC](https://forums.powershell.org/t/need-help-with-function-parms/19041/3 "2022-04-08T20:39:54Z")

</div>

Here is my little fuction, what I would like is for the user to be able to pass either or prameter without having to declare it and the scipt determine which one it is based on the email address having the @ symbol in the string.

```auto
function get-accinfo {
    [CmdletBinding()]
    Param
    (
        [parameter(Mandatory=$true,
        ParameterSetName="ID")]
        [string]$ID,
        
        [parameter(Mandatory=$true,
        ParameterSetName="EMAIL")]
        [string]$EMAIL

        
    )

if ($email) {
get-user $email | ForEach-Object {get-aduser $_.samaccountname -Properties * -Credential $creds} | ForEach-Object {
    $break = "#################################################################################`n"
  foreach ($Domain in "one.com,two.com,three.com,four.com".split(",")) {
    $en = $_.employeenumber  
    Get-ADUser -Server $Domain -LDAPFilter "(EmployeeNumber=$en)" -Properties * -Credential $creds |
    Select-Object @{N='Account';E={"$($_.samAccountName) on $Domain"}}, @{n='Samaccountname';e={(New-Object System.Security.Principal.SecurityIdentifier $_.Sid).Translate([System.Security.Principal.NTAccount]).Value }},DistinguishedName,Displayname, mail, EmployeeNumber, @{n='LikedMasterAccount';e={(New-Object System.Security.Principal.SecurityIdentifier $_.msExchMasterAccountSid).Translate([System.Security.Principal.NTAccount]).Value }} 
     
     $break    
  }} }

  if ($BRID) {
     ForEach-Object {
        $break = "#################################################################################`n"
      foreach ($Domain in "one.com,two.com,three.com,four.com".split(",")) {
        $en = $ID
        Get-ADUser -Server $Domain -LDAPFilter "(EmployeeNumber=$en)" -Properties * -Credential $creds |
        Select-Object @{N='Account';E={"$($_.samAccountName) on $Domain"}}, @{n='Samaccountname';e={(New-Object System.Security.Principal.SecurityIdentifier $_.Sid).Translate([System.Security.Principal.NTAccount]).Value }},DistinguishedName,Displayname, mail, EmployeeNumber, @{n='LikedMasterAccount';e={(New-Object System.Security.Principal.SecurityIdentifier $_.msExchMasterAccountSid).Translate([System.Security.Principal.NTAccount]).Value }} 
         
         $break    
      }} }
    }

```

---

<div class="post-metadata">

**Author:** ![laage](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/laage/32/43_2.png) [@laage](https://forums.powershell.org/u/laage)\
**Post date:** [April 8, 2022, 11:55pm UTC](https://forums.powershell.org/t/need-help-with-function-parms/19041/4 "2022-04-08T23:55:18Z")

</div>

To set a default value for a parameter you assign it as you would any other variable with “= ‘VALUE’” within the Param-block.  
If a user passes in a different value it overwrites the value you assigned previously.

Also if you set a default parameter you will have to either skip the Mandatory part of the parameter assignment as the user can choose to not assign a value when calling the script.

```auto
Param
    (
        [parameter(Mandatory=$false)]
        [string]$ID = 'ID'
        
        [parameter(Mandatory=$false)]
        [string]$EMAIL = 'EMAIL'

```

If you wish to limit the user to a couple of predefined values you can use ValidateSet in the Param-block like this:

```auto
        [parameter(Mandatory=$false)]
        [ValidateSet('ID','NAME','EMAIL')]
        [string]$ID = 'ID'

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [April 9, 2022, 12:48am UTC](https://forums.powershell.org/t/need-help-with-function-parms/19041/5 "2022-04-09T00:48:37Z")

</div>

I think the easiest way to achieve your goal is to limit to a generic parameter and just check the value yourself. You could even extend this to accept samaccountname, distinguishedname, etc. In order to improve readability I stored your calculated expressions/properties in a variable. I also encourage you to look into splatting as well.

```auto
function Get-AccountInfo {
    [CmdletBinding()]
    Param(
        [parameter(Mandatory=$true,Position=0,HelpMessage='Enter the employee number or email address')]
        [string]$User
    )

    $filter = if($User -match '@'){
        "EmailAddress -eq '$User'"
    }
    else{
        "EmployeeNumber -eq '$User'"
    }

    $selectprops = @{N='Account';E={"$($_.samAccountName) on $Domain"}},
                   @{n='Samaccountname';e={(New-Object System.Security.Principal.SecurityIdentifier $_.Sid).Translate([System.Security.Principal.NTAccount]).Value }},
                   'DistinguishedName',
                   'Displayname',
                   'mail',
                   'EmployeeNumber',
                   @{n='LikedMasterAccount';e={(New-Object System.Security.Principal.SecurityIdentifier $_.msExchMasterAccountSid).Translate([System.Security.Principal.NTAccount]).Value }} 

    Get-ADUser -Server $Domain -Filter $filter -Properties mail,employeenumber,msExchMasterAccountSid -Credential $creds |
        Select-Object $selectprops

}

```

My final bit of advice is to limit the properties you query with Get-ADUser to those you actually use. The increased load on active directory In small environments this isn’t really an issue, but can be very disruptive in larger organizations.

---

<div class="post-metadata">

**Author:** ![Kira-Shadow](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@Kira-Shadow](https://forums.powershell.org/u/Kira-Shadow)\
**Post date:** [April 9, 2022, 8:27am UTC](https://forums.powershell.org/t/need-help-with-function-parms/19041/6 "2022-04-09T08:27:49Z")

</div>

Hi,

The supplied script will accept either “ID” or “EMAIL” and only one of them not both.

You may also want to add a default parameterset to cmdletbinding():-

```auto
[CmdletBinding(DefaultParameterSetName="ID")]

```

and this logic can detect which parameterset was used:-

```auto
switch ($PsCmdlet.ParameterSetName) {
    "EMAIL" {

    }
    "ID" {

    }
}

```

You could also use a vailidate pattern regex for the email parameter:-

```auto
[ValidatePattern('@')]

```

you could go more complex on the regex but this would limit the portability of the script.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 9, 2022, 8:37am UTC](https://forums.powershell.org/t/need-help-with-function-parms/19041/7 "2022-04-09T08:37:01Z")

</div>

> [@Kira-Shadow](#):
>
> The supplied script will accept either “ID” or “EMAIL” and only one of them not both.

Hmmm … that wouldn’t accomplish the requirement to determine the provided parameter only based on the input of the value … without the parameter name.

---

<div class="post-metadata">

**Author:** ![Kira-Shadow](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@Kira-Shadow](https://forums.powershell.org/u/Kira-Shadow)\
**Post date:** [April 9, 2022, 8:45am UTC](https://forums.powershell.org/t/need-help-with-function-parms/19041/8 "2022-04-09T08:45:40Z")

</div>

For that KrzyDoug has the best solution, I’m just covering alternatives 🙂

But I would suggest that if you have a mixed list of IDs and EMAILs then there is something wrong with the source,

Alternativly use regex to determine the parameter (Assuming no IDs contain ‘@’)

```auto
#Email
'@'

#ID
'^[^@]*$'

```

if this was my function I would add a properties parameter:-

```auto
[Parameter()]
        [String[]]
        $Properties

```

and use logic to process the results output

```auto
process {
        if ($Properties) {
            Get-ADUser -Filter 'UserPrincipalName -eq $email' -Properties $Properties
        }else{
            Get-ADUser -Filter 'UserPrincipalName -eq $email'
        }
        
    }

```

(This is from a function that finds AD users from a list of email addesses, and work on the pipeline)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:18pm UTC](https://forums.powershell.org/t/need-help-with-function-parms/19041/9 "2024-05-16T20:18:04Z")

</div>

Closed on May 16, 2024, 8:18pm UTC


