locked
Generate array of hostnames RRS feed

  • Question

  • Hey folks,

    Im working on few scripts for which I need some user input. The input represents VM hostname to be configured/deployed.

    The hostnames for our purpose always goes in sequence. Like MYHOST0001,0002,0003,etc etc.

    I created following script to generate the hostnames array based on first hostname input.

    The difficulties I had was to split the letter part - MYHOST and the number part 0001. As its not possible to increment string which is what I get from read-host, I had to split the string to MYHOST and 0001, then convert 0001 string to integer and do the increment. Finally I have created array with generated hostnames.

    The script works well.

    My question is - is there any better way than what I did?

    THX

    #get first hostname
    $HNinput = Read-Host -Prompt "Enter first hostname - FQDN (MYHOST0001.my.domain)"
    
    #get how many hosts will be configured
    $HNcount = Read-Host -Prompt "enter how many hosts total"
    $HNcountfix = $HNcount-1
    #parse the hostname to get word part, suffix
    $HNsource = $HNinput.Split(".",2)
    $HNwordpart = $HNsource[0].Substring(0,8)
    $HNsuffix = "."+$HNsource[1]
    
    #parse the hostname to get number part
    $HNstartnumber = $HNinput.Substring(8,4)
    $strNum = $HNstartnumber
    $HNstartfrom = [convert]::ToInt32($strNum, 10)
    
    #create array with final FQDN hostnames
    $hnlast = $HNstartfrom+$HNcountfix
    
    $Counter = $HNstartfrom..$HNlast
    $HNFQDNlist = foreach ($Number in $Counter) {
        $HNwordpart+('{0:0###}' -f $Number)+$HNsuffix
        }
    
    
    Write-Host $HNFQDNlist


    --------------------- Leos

    Wednesday, May 24, 2017 8:06 PM

Answers

  • you had to ask.... (in 3 lines of code)

    PS>$hostinfo = Read-Host 'Enter first hostname, domain name, # of hosts'
    Enter first hostname, domain name, # of hosts: myhost,mydomain.com,20
    PS>$hostname, $domain,[int]$numhosts = $hostinfo -split ','
    PS>for($i=1;$i -le $numhosts;$i++){ '{0}{1:D5}.{2}' -f $hostname,$i,$domain}
    myhost00001.mydomain.com
    myhost00002.mydomain.com
    myhost00003.mydomain.com
    myhost00004.mydomain.com
    myhost00005.mydomain.com
    myhost00006.mydomain.com
    myhost00007.mydomain.com
    myhost00008.mydomain.com
    myhost00009.mydomain.com
    myhost00010.mydomain.com
    myhost00011.mydomain.com
    myhost00012.mydomain.com
    myhost00013.mydomain.com
    myhost00014.mydomain.com
    myhost00015.mydomain.com
    myhost00016.mydomain.com
    myhost00017.mydomain.com
    myhost00018.mydomain.com
    myhost00019.mydomain.com
    myhost00020.mydomain.com


    \_(ツ)_/


    • Edited by jrv Wednesday, May 24, 2017 8:22 PM
    • Marked as answer by Leoš Marek Wednesday, May 24, 2017 8:35 PM
    Wednesday, May 24, 2017 8:22 PM

All replies

  • you had to ask.... (in 3 lines of code)

    PS>$hostinfo = Read-Host 'Enter first hostname, domain name, # of hosts'
    Enter first hostname, domain name, # of hosts: myhost,mydomain.com,20
    PS>$hostname, $domain,[int]$numhosts = $hostinfo -split ','
    PS>for($i=1;$i -le $numhosts;$i++){ '{0}{1:D5}.{2}' -f $hostname,$i,$domain}
    myhost00001.mydomain.com
    myhost00002.mydomain.com
    myhost00003.mydomain.com
    myhost00004.mydomain.com
    myhost00005.mydomain.com
    myhost00006.mydomain.com
    myhost00007.mydomain.com
    myhost00008.mydomain.com
    myhost00009.mydomain.com
    myhost00010.mydomain.com
    myhost00011.mydomain.com
    myhost00012.mydomain.com
    myhost00013.mydomain.com
    myhost00014.mydomain.com
    myhost00015.mydomain.com
    myhost00016.mydomain.com
    myhost00017.mydomain.com
    myhost00018.mydomain.com
    myhost00019.mydomain.com
    myhost00020.mydomain.com


    \_(ツ)_/


    • Edited by jrv Wednesday, May 24, 2017 8:22 PM
    • Marked as answer by Leoš Marek Wednesday, May 24, 2017 8:35 PM
    Wednesday, May 24, 2017 8:22 PM
  • Uh wow... could you explain me a little the , convention and the for $i and the {} ? Sorry Im still missing some beggimer points :)

    --------------------- Leos

    Wednesday, May 24, 2017 8:38 PM
  • help about_for


    \_(ツ)_/

    Wednesday, May 24, 2017 8:48 PM
  • Further question. The hostname does not always start as 0001. It might be 0031 or 3001.

    How about then? You have to somehow get the starting number while the user enters MYHOST0501.


    --------------------- Leos

    Thursday, May 25, 2017 4:43 AM
  • I guess none of you kids learned how to think through a problem in school.  It is up to us old folks to do it all for you. 

    $start  = 53
    $numhosts = 121

    for($i=$start;$i -le ($numhosts+$start);$i++){ '{0}{1:D5}.{2}' -f $hostname,$i,$domain}



    \_(ツ)_/

    Thursday, May 25, 2017 4:47 AM
  • I thought this is a support forum where people are supposed to get helped with things they dont know themselves, not to listen they are stupid kids.... cool attitude man.

    Anyway, I dont see how this extracts the startnum from single user input in form myhost0301 so I guess my way of substring to get the correct number is correct.

    Thanks for your help


    --------------------- Leos

    Thursday, May 25, 2017 4:52 AM
  • Well.  It is not a support forum.  It s a forum for professional technicians who use scripting in the work.  It assumes that you know how to think about and solve a problem.

    You can live your life by asking others to do the thinking for you.  You will never be a tech beyond the help desk or a local desktop tech.  As systems move to the cloud help desk and desktop techs will no longer be needed.

    I realize that most schools only teach answer and not how to work out problems.  This is sad.  You should sue your schools for failing to teach you how to think.

    The world is not a multiple choice question.  It is a sea of challenges and problems that you need to learn to solve.

    You have every bit of information you need to solve the next step. Of course you can just wait for someone to do it for you which will be of no help to you or anyone else.

    Without any ability to read technical documentation with proficiency and retention this will all be very hard.  I wish you luck and an awakening but I fear you will remain asleep.

    The answer to your problem is in my fist post.  Figure out how what I did works and you will know 10 times more about PowerShell, logic and scripting  then you did yesterday.,

     


    \_(ツ)_/

    Thursday, May 25, 2017 5:02 AM
  • its really amazing how you can judge who I am, where I work and how Im unable to think based on single question, telling me what are my live options.

    Your behavior is far away from whats called professional.



    --------------------- Leos

    Thursday, May 25, 2017 5:05 AM
  • You started by asking a good question.  You asked if there was a better way to do what you were trying t do.  I challenged you to think in a new way.  I guess my challenge is to difficult.  Maybe you will think about it.  Don't get wimpy about a little tough challenge.  Ask why this seems to hard for you to figure out.  I know you can figure it out if you can just stop being sensitive and lazy. 

    Technology is a mental challenge.  You need to exercise the old brainpan and it will all get easier.

    Anyway you have all f the answer you just need to see it.


    \_(ツ)_/

    Thursday, May 25, 2017 5:19 AM
  • Yes you challenged me. And I was thinking about it.

    I think for such reasons there are things like training where you can get some explanation why things works in specific way. Its not very easy to learn everything yourself and sometimes you need to be pushed a little. Which is what you did.

    The only thing I dont understand/accept is your insults.

    cheers


    --------------------- Leos

    Thursday, May 25, 2017 5:31 AM
  • It got your attention...right?

    When I was taught number theory I was given a book ad told to start n page one and do all of the problems.  There was only a page or two of rules and then a book of problems.  Each one set the stage for the next.  It took me a few months but I learned number theory without anyone giving me any answers or allowing any questions.

    When you are a professional you really cannot rely on others to teach you everything.  All successful techs teach themselves with an occasional company pained training class.  No company will send you to training ro advance you until they are sure you are capable of learning on your own.  Professional life is not high school.

    If you go t a technical college you will be given a sort lecture, a book, a library and assignments.  The rest is up to you.

    Answers are much less important than questions and knowing how to ask a question gets you most of the way to an answer. Ask any scientist, mathematician or programmer.

    Figure out the three lines of code I posted and you will be much further along and you will realize that you can answer many of your questions by yourself.

    Believe me.  I was not trying to insult you or make you feel bad.  Just a little friendly shove. Don't your friends bust your chops?   I bet they do.


    \_(ツ)_/

    Thursday, May 25, 2017 5:41 AM