Reputation: 233
I want to make a argument Mandatory based on the Switch Condition Something Like
param(
[string]$para1,
[switch]$choice="Upgrade"
[string]$paraUpg
[string]$paraInstall
)
now if the choice is Upgrade
I want to make $paraUpg
mandatory and if $choice
is Install
then $paraInstall
has to be mandatory
Upvotes: 1
Views: 1855
Reputation: 486
You could try something like the following. Setup all of your parameters and if $choice="Upgrade"
and $paraUpg
is null or empty throw an exception. Repeat similarly for $choice="Install"
. You could then conditionally choose what to do after the parameter check based on the parameters chosen.
param(
[string]$para1,
[string]$choice="Upgrade",
[string]$paraUpg,
[string]$paraInstall
)
#check if choice is "Upgrade" and paraUpg is empty
if(($choice -eq "Upgrade") -and ([string]::IsNullOrEmpty($paraUpg)))
{
throw "paraUpg is a required parameter"
}
#check if choice is "Install" and paraInstall is empty
elseif (($choice -eq "Install") -and ([string]::IsNullOrEmpty($paraInstall)) )
{
throw "paraInstall is a required parameter"
}
Upvotes: 1
Reputation: 3486
first of all its PowerShell
which comes along with windows 7 as one of its added utilities.
you have not given any detail about what have you done so far. I suspect you want something like this.
in your PowerShell
script declare params preferably at top.
param([string]$UserName, [string]$Password, [string]$MachineName)
use these parmas within your powershell script wherever required. Now create a batch file from where you can pass values to these params like this.
@powershell -ExecutionPolicy Unrestricted -FILE YourPowerShellScript.ps1 "UserName" "Password" "MachineName"
i have specified Policy rights within batch file so you don't have to mention each time when you run the script on different machines. Hope it helps.
Upvotes: 0