Everything in this script I wrote works with if and ifelse statements but not the else statements. Could someone please review the following and help me figure out why? The desired outcome is if I type aconda, a conda environment launches, aconda -d deactivates conda and alters the path, aconda -a activates conda and alters the path . Is there a way to specify the outcome of no parameter?
function aconda {
param(
[Alias('d')]
[Parameter(ParameterSetName='Deactivate')]
[switch] $Deactivate
,
[Alias('a')]
[Parameter(ParameterSetName='Activate')]
[switch] $Activate
,
[Alias("h")]
[Parameter(ParameterSetName='help')]
[switch] $help
)
if ($Activate) {
$Environment = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
foreach ($path in ($Environment).Split(";"))
{
if ($path -like "C:\Program Files\Python310\Scripts\")
{
$Environment = $Environment.Replace($Path ,"")
}
if ($path -like "C:\Program Files\Python310\")
{
$Environment = $Environment.Replace($Path ,"")
}
}
$env:PATH = $Environment
. C:\Anaconda3\shell\condabin\conda-hook.ps1 ; conda activate 'C:\Anaconda3'
}
elseif ($Deactivate)
{
. C:\Anaconda3\shell\condabin\conda-hook.ps1 ; conda deactivate 'C:\Anaconda3'
$env:PATH = [Environment]::GetEnvironmentVariable('Path', 'Machine'),
[Environment]::GetEnvironmentVariable('Path', 'User') -join ';'
}
elseif ($help)
{ Write-Output "
-a activate conda
-d deactivate conda
-? Help. This is the same as not typing any options"
}
else { . C:\Anaconda3\shell\condabin\conda-hook.ps1 ; conda deactivate 'C:\Anaconda3'
}
}
function aconda {
[CmdletBinding(DefaultParameterSetName = 'Activate')]
param(
[Alias('d')]
[Parameter(ParameterSetName='Deactivate')]
[switch] $Deactivate
,
[Alias('a')]
[Parameter(ParameterSetName='Activate')]
[switch] $Activate
,
[Alias("h")]
[Parameter(ParameterSetName='help')]
[switch] $help
)
$noInput = !$Deactivate -and !$Activate
if ($PSBoundParameters.ContainsKey('Activate') -or $noInput ) {
$Environment = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
foreach ($path in ($Environment).Split(";"))
{
if ($path -like "C:\Program Files\Python310\Scripts\")
{
$Environment = $Environment.Replace($Path ,"")
}
if ($path -like "C:\Program Files\Python310\")
{
$Environment = $Environment.Replace($Path ,"")
}
}
$env:PATH = $Environment
. C:\Anaconda3\shell\condabin\conda-hook.ps1 ; conda activate 'C:\Anaconda3'
}
elseif ($Deactivate)
{
. C:\Anaconda3\shell\condabin\conda-hook.ps1 ; conda deactivate 'C:\Anaconda3'
$env:PATH = [Environment]::GetEnvironmentVariable('Path', 'Machine'),
[Environment]::GetEnvironmentVariable('Path', 'User') -join ';'
}
elseif ($help)
{ Write-Output "
-a activate conda
-d deactivate conda
-? Help. This is the same as not typing any options"
}
else { . C:\Anaconda3\shell\condabin\conda-hook.ps1 ; conda deactivate 'C:\Anaconda3'
}
}
This does not, the only difference being if ($PSBoundParameters.ContainsKey('Activate') -or $noInput ) vs elseif ($noInput) { . C:\Anaconda3\shell\condabin\conda-hook.ps1 ; conda deactivate 'C:\Anaconda3' }:
function aconda {
[CmdletBinding(DefaultParameterSetName = 'Activate')]
param(
[Alias('d')]
[Parameter(ParameterSetName='Deactivate')]
[switch] $Deactivate
,
[Alias('a')]
[Parameter(ParameterSetName='Activate')]
[switch] $Activate
,
[Alias("h")]
[Parameter(ParameterSetName='help')]
[switch] $help
)
$noInput = !$Deactivate -and !$Activate
if ($PSBoundParameters.ContainsKey('Activate') ) {
$Environment = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
foreach ($path in ($Environment).Split(";"))
{
if ($path -like "C:\Program Files\Python310\Scripts\")
{
$Environment = $Environment.Replace($Path ,"")
}
if ($path -like "C:\Program Files\Python310\")
{
$Environment = $Environment.Replace($Path ,"")
}
}
$env:PATH = $Environment
. C:\Anaconda3\shell\condabin\conda-hook.ps1 ; conda activate 'C:\Anaconda3'
}
elseif ($Deactivate)
{
. C:\Anaconda3\shell\condabin\conda-hook.ps1 ; conda deactivate 'C:\Anaconda3'
$env:PATH = [Environment]::GetEnvironmentVariable('Path', 'Machine'),
[Environment]::GetEnvironmentVariable('Path', 'User') -join ';'
}
elseif ($help)
{ Write-Output "
-a activate conda
-d deactivate conda
-? Help. This is the same as not typing any options"
}
elseif ($noInput) { . C:\Anaconda3\shell\condabin\conda-hook.ps1 ; conda deactivate 'C:\Anaconda3'
}
}
Write-Hostwith the name of the current block to see what happens with each possible value? ///// also, theswitchstructure is a handy alternative for anif/elseif/elsecascade. it has adefaultsection for "no matches found" ... [grin]aconda : Parameter set cannot be resolved using the specified named parameters.If you don't see that, your$ErrorActionPreferencemight be set toSilentlyContinue, which is bad for debugging. To fix your function, you need to identify a default parameter set so Powershell know what to do if it can't meet the requirements of any of the 3 parameter set defined. This can be accomplished by adding[CmdletBinding(DefaultParameterSetName = 'Activate')]before theparamblock of your func.aconda -a:$false, which would end up in theElseblock too because of the way your code is structured. to account for that, you'd need to replace theif($Activate)byif ($PSBoundParameters.ContainsKey('Activate')and then, in your condition, you deal with$Activatebut know that it could be set to$falseif you didaconda -a:$false, which is a very valid way of working with switch parameters."[Parameter(ParameterSetName='default')] [switch] $default"defaultshowing up in the auto completion.