Select a particular character & if else

Welcome Forums General PowerShell Q&A Select a particular character & if else

Viewing 2 reply threads
  • Author
    Posts
    • #224835
      Participant
      Topics: 3
      Replies: 2
      Points: 25
      Rank: Member

      Hello everyone,

      The scenario i have is that, i have list of objects in the array for the most part each object in the array has the same character length (trxc-fa01-tc01,trxc-fa02-tc01) but then there are a few different ones such as (trxc-pvc05 and trxc-dtc01) so i am trying something very basic; pretty much with if else statement but having hard time to extract the character number [8] from my value.

      Sample script:

      $MyServers = @(“trxc-fa06-tc01”, “trxc-fa05-tc01”, “trxc-fa04-tc01”, “trxc-fa03-tc01”, “trxc-pvc05”, “trxc-fa01-tc01”, “trxc-dtc01”, “trxc-sa01-tc01”)

      ForEach ($Server in $MyServers) {
      if ($Server -like ‘*pvc05*’) {

      Write-Host “$Server is in Cage1”
      }
      elseif ($Server -like ‘*dtc01*’) {

      Write-Host “$Server is in Cage2”
      }
      else {

      Write-Host “$Server is in Cage [character 8]
      # character 8 would be like trxc-fa06-tc01 so it would print “trxc-fa06-tc01″ is in cage 6
      }
      }

       

      Does it make sense?

      Thank you.

       

    • #224856
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      JustLearning, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code or error messages or sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      Do you mean something like this?

    • #224880
      Participant
      Topics: 3
      Replies: 2
      Points: 25
      Rank: Member

      Exactly, awesome thank you so much.

Viewing 2 reply threads
  • The topic ‘Select a particular character & if else’ is closed to new replies.