case insensitive Powersell script

ZM 206 Reputation points
2022-09-26T15:50:00.773+00:00

The resource group from the initial deployment will be found, but a failure will occur while App Gateway backend names are created.

How can I make this part below case insensitive?

$BackendName = ($Client+"-"+$Env+"-"+$Role+"-BackEndPool")

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MotoX80 32,911 Reputation points
    2022-09-26T17:46:30.647+00:00

    There should no case sensitivity issue when doing variable assignments. You should be able to reference $Client as $client or $CLIENT.

    One problem that you might have is that $env is normally used to reference environment variables.
    IE, $env:COMPUTERNAME.

    https://www.computerperformance.co.uk/powershell/environmental-variables/

    If you are trying to define your own $env variable, then you might run into problems. I would recommend using a different variable name.

    Beyond that we would need more details about what error or what output you get and you expect to get.

    0 comments No comments

  2. Rich Matheisen 45,906 Reputation points
    2022-09-26T18:40:34.623+00:00

    Where would you require case sensitivity (or case compliance)? Should the contents of $BackendName be all upper-case or all lower-case? That's easy:

    $BackendName = ($Client+"-"+$Env+"-"+$Role+"-BackEndPool").ToLower()  
      
    OR  
      
    $BackendName = ($Client+"-"+$Env+"-"+$Role+"-BackEndPool").ToUpper()  
    

    Or should any of the items you're concatenating be all lowercase or uppercase? Or should the only the first letter of each word be in uppercase? Something like this might help:

    $Client = "IAmInCaMeLcAsE"  
    $Env = "envwasalllower-case"  
    $Role = "ROLEwasUpper-Case"  
      
    $text = "$Client $Env $Role"  
    ( Get-Culture ).TextInfo.ToTitleCase( $text.ToLower() )  
    

  3. Rich Matheisen 45,906 Reputation points
    2022-09-27T14:29:42.993+00:00

    You can't make the content case-insensitive -- that's up to the comparison operation. You'd need something to check the client and return what you want. Maybe something like this?

    $ListOfClients = @('MarkNew','MarkOld')                         # this is a list of clients with the 'correct' casing  
    $WantedCasing = @{'marknew'='MarkNew'; 'markold'='MarkOld'}     # hash keys are case insensitive  
      
    function CheckCasing {  
        param (  
            [string]$Client  
        )  
        If ($ListOfClients -ccontains $Client){     # case sensitive!  
            $Client  
        }  
        else{  
            if ($WantedCasing.ContainsKey($Client)){  
                $WantedCasing.$Client               # return wanted casing  
            }  
        }  
    }  
      
    # check for proper casing  
    $clients = "MarkNew","mArKnEw","MarkOld","markOLD"  
    ForEach ($x in $clients){  
        CheckCasing $x  
    }  
    
    0 comments No comments