none
Powershell dealing with a range of computer names. RRS feed

  • Question

  • Hello all.  Abridged description, I'm a lab admin trying to re-write a VBS login script into Powershell.  I need to be able to run commands determined by computer name and number...for instance, I have complabA01 through complabA70.  01-25 need to do an action and copy a shared file, 26-40 need a different action and the same shared file, and 41-70 get a different action still and said shared file.  I've got the base code laid out and functional to determine the computer name and specific lines based on that:

    $cn = $env:computername

    If($cn -like 'complabA*'){do stuff}

    If($cn -like 'complabB*'){do other stuff} so on. 

    Is there a way go a layer deeper on that and say "if your number 01-25 do this"?  The problem well may be that I'm coming at this with slightly deeper roots in VBS... so I may well be over/under thinking this.

    In case it'll help, in VBS we handle it by turning the numbers at the end of the name into a separate variable and using said secondary variable in a select case.

    Any thoughts on how that can be done in Powershell?


    • Edited by swalker5906 Wednesday, March 26, 2014 9:43 PM Wording
    Wednesday, March 26, 2014 9:43 PM

Answers

  • You can do this using a regular expression match. For example:


    $computerName = "complabB04"
    
    $computerName | select-string '^complab([\D])([\d]+)' | foreach-object {
      $labCode = $_.Matches[0].Groups[1].Value
      $computerCode = $_.Matches[0].Groups[2].Value
    }
    
    "Lab code: $labCode"  # B
    "Computer code: $computerCode"  # 04
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Peter Ziganki Thursday, March 27, 2014 7:20 AM
    • Marked as answer by swalker5906 Thursday, March 27, 2014 11:47 AM
    Wednesday, March 26, 2014 9:59 PM
    Moderator

All replies

  • You can do this using a regular expression match. For example:


    $computerName = "complabB04"
    
    $computerName | select-string '^complab([\D])([\d]+)' | foreach-object {
      $labCode = $_.Matches[0].Groups[1].Value
      $computerCode = $_.Matches[0].Groups[2].Value
    }
    
    "Lab code: $labCode"  # B
    "Computer code: $computerCode"  # 04
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Peter Ziganki Thursday, March 27, 2014 7:20 AM
    • Marked as answer by swalker5906 Thursday, March 27, 2014 11:47 AM
    Wednesday, March 26, 2014 9:59 PM
    Moderator
  • Thanks for the quick reply.  Seems to be the ticket!

    Lab/Citrix Infrastructure Administrator

    Thursday, March 27, 2014 11:48 AM