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"
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
)
[quote quote=163752]You would use [AllowNull] attribute.
<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
[/quote]
thank you very much!
Why don’t you just go:
.\script1.ps1 -switch1 $DBN $($Server[-1]) $Creds $ConnectionID $ConnectionDataSource
[quote quote=163779]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>
.\script1.ps1 -switch1 $DBN $($Server[-1]) $Creds $ConnectionID $ConnectionDataSource
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]
i tried that ... it doesnt help, still asks for connection data source