Select a particular character & if else

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.

 

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?

$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*') {
        "$Server is in Cage1"
    }
    elseif ($Server -like '*dtc01*') {
        "$Server is in Cage2"
    }
    else {
        "$Server is in Cage $($Server.substring(8,1))"
    }
}

[quote quote=224856][/quote]
Exactly, awesome thank you so much.