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

Viewing 4 reply threads
  • Author
    Posts
    • #163725
      Participant
      Topics: 24
      Replies: 65
      Points: 394
      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: 1159
      Points: 4,050
      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: 394
      Rank: Contributor

      You would use [AllowNull] attribute.

      PowerShell
      19 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 57.5978px; top: 306px;” spellcheck=”false” wrap=”off”></textarea>

      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: 28
      Replies: 751
      Points: 2,094
      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: 394
      Rank: Contributor

      Why don’t you just go:

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

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

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

Viewing 4 reply threads
  • The topic ‘How to specify optional argument for a command operator invokation?’ is closed to new replies.