Jump to content

Recommended Posts

Posted (edited)

I want to run an if statement based on the first 2 characters of the computer name and nothing else. The kicker is that the first 2 characters could exist elsewhere in the computer name.  That presents a problem when I want to generalize this script and not worry about the entire computer name. For example, I want my if statement to function something like this.

  • If computer name starts with “AB”, then do Thing1.
  • ElseIf computer name starts with “CD”, then do Thing2.
  • ElseIf computer name starts with “EF”, then do Thing3.
  • EndIf.

I already have a script to perform this task but it only looks for if the string contains the characters, not starts with. Here’s the actual code:

If StringInStr ( @computername , "AB" ) Then
    RunWait ( Thing1 )

ElseIf  StringInStr ( @computername , "CD" ) Then
    RunWait ( Thing2 )

ElseIf  StringInStr ( @computername , "EF" ) Then
    RunWait ( Thing3 )

EndIf

This presents a problem when the list of computers I run this on looks like this:

  • ABAB1234
  • CDAB1234
  • EFCD1234

How can I write a script that looks for only the first 2 characters of the computer name? Should I just break down and use AutoIt to run a batch file instead?

Edited by Troubleshooter5000
Posted
8 hours ago, Troubleshooter5000 said:

How can I write a script that looks for only the first 2 characters of the computer name?

Switch StringLeft(@ComputerName, 2)
    Case "AB"
        ; RunWait(Thing1)
    Case "CD"
        ; RunWait(Thing2)
    Case "EF"
        ; RunWait(Thing3)
    Case Else
        ConsoleWrite('! >>> Computername not found' & @CRLF)
EndSwitch

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...