How to specify optional argument for a command operator invokation?

Welcome Forums General PowerShell Q&A How to specify optional argument for a command operator invokation?

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #163725

    Participant
    Topics: 24
    Replies: 65
    Points: 383
    Rank: Contributor

    I havent had much luck on SO since yesterday, so ill post here as well for potential help:

    https://stackoverflow.com/questions/56844111/how-to-specify-optional-argument-for-a-command-operator-invokation

    I have the following command call

    & ".\script1.ps1" -switch1 "$DBN" "$($Server[-1])" "$Creds" "$ConnectionID" "$ConnectionDataSource"

    I want to make ConnectionID and Connection DataSource optional

    How can I do that?

    This command is only working if I input something for ConnectionID and Connection Data Source. otherwise, they won't work if I don't input anything for these two params!

    here are the params in this script1.ps1 (which I am calling IN script2.ps1):

    param(
    [Parameter(Mandatory=$true)]
        [string]$DBN,
    
    [Parameter(Mandatory=$true)]
        [string]$Server,
    
    [Parameter(Mandatory=$false)]
        [string]$Creds,
    
    [Parameter(Mandatory=$false, HelpMessage="Enter a UserID for the connection")]
    [string]$ConnectionID, 
    
    [Parameter(Mandatory=$false, HelpMessage="Enter a Data Source for the connection. Must enter the ConnectionID first")]
    [string]$ConnectionDataSource
    )

    here are the params in this script2.ps1 (which I am calling script1.ps1 from):

    param(
    [Parameter(Mandatory=$true)]
        [string]$DBN,
    
    [Parameter(Mandatory=$false, HelpMessage="Enter a UserID for the connection")]
    [string]$ConnectionID, 
    
    [Parameter(Mandatory=$false, HelpMessage="Enter a Data Source for the connection. Must enter the ConnectionID first")]
    [string]$ConnectionDataSource
    )

    running script1.ps1 without these 2 inputs works just fine.

    i run it like this:

    PS> script1.ps1 DB1 Server1

    so I know I have to do something here in the call operator of this second script I'm calling script1 from

    I'm looking for something like this:

    & ".\script1.ps1" -switch1 "$DBN" "$($Server[-1])" "$Creds" [optional]"$ConnectionID" [optional]"$ConnectionDataSource"
  • #163752

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    You would use [AllowNull] attribute.

    param(
    [Parameter(Mandatory=$true)]
        [string]$DBN,
    
    [Parameter(Mandatory=$true)]
        [string]$Server,
    
    [Parameter(Mandatory=$false)]
        [string]$Creds,
    
    [Parameter(Mandatory=$false, HelpMessage="Enter a UserID for the connection")]
    [AllowNull()]
    [string]$ConnectionID, 
    
    [Parameter(Mandatory=$false, HelpMessage="Enter a Data Source for the connection. Must enter the ConnectionID first")]
    [AllowNull()]
    [string]$ConnectionDataSource
    )
    
  • #163776

    Participant
    Topics: 24
    Replies: 65
    Points: 383
    Rank: Contributor

    You would use [AllowNull] attribute.

    PowerShell
    19 lines

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    param(
    [Parameter(Mandatory=$true)]
    [string]$DBN,
    [Parameter(Mandatory=$true)]
    [string]$Server,
    [Parameter(Mandatory=$false)]
    [string]$Creds,
    [Parameter(Mandatory=$false, HelpMessage="Enter a UserID for the connection")]
    [AllowNull()]
    [string]$ConnectionID,
    [Parameter(Mandatory=$false, HelpMessage="Enter a Data Source for the connection. Must enter the ConnectionID first")]
    [AllowNull()]
    [string]$ConnectionDataSource
    )
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    thank you very much!

  • #163779
    js

    Participant
    Topics: 25
    Replies: 678
    Points: 1,629
    Helping Hand
    Rank: Community Hero

    Why don't you just go:

    .\script1.ps1 -switch1 $DBN $($Server[-1]) $Creds $ConnectionID $ConnectionDataSource
    
  • #167842

    Participant
    Topics: 24
    Replies: 65
    Points: 383
    Rank: Contributor

    Why don't you just go:

    1
    2
    .\script1.ps1 switch1 $DBN $($Server[-1]) $Creds $ConnectionID $ConnectionDataSource
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    i tried that ... it doesnt help, still asks for connection data source

You must be logged in to reply to this topic.