1

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

3
  • Please show us what you've done. Commented Feb 16, 2013 at 4:54
  • Actually I want 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 Commented Mar 13, 2013 at 15:29
  • You can add this new info to your question and it meets the criteria that you have tried something and people won't down vote it (maybe) Commented Mar 13, 2013 at 18:20

2 Answers 2

1

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"
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

My problem is that when the Powershell script is invoked from the Batch it asks for a continue because the script is not signed.I want to bypass that Confirmation
In my response, i have mentioned ExecutionPolicy rights to Unrestricted within argument batch file which will handle your case. you can either set it to Set-ExecutionPolicy AllSigned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.