Automation

 View Only
  • 1.  Unable to get VM Name with 0 (Zero)

    Posted Dec 16, 2022 03:29 PM

    Hi,

    I am trying to validate the VM, if that name exists or not before I deploy a VM. My VM Name starts with mytestvm01, mytestvm02 etc but now I have a issue validating, If I execute the below script, I get unused VM name as mytestvm1 but I wanted to get like mytestvm0x for first 9 VMs

    Please help!!

    #Check VM_Name
    $nameRange = 01,20
    $name = $nameRange[0]
    do{
    $computer = "mytestvm$name"
    $name++
    }
    until(
    $name -gt $nameRange[1] -or -not (Test-Connection $computer -Count 1 -Quiet)
    )

    if($name -lt $namerange[1]){
    $found = $true
    }
    else{
    "No free VM found"
    $found = $false
    }

    # Check if an name is available
    if($found){
    # Use the value in $computer
    $freevm = $computer
    $freevm
    }
    else{
    Write-Error "No free VM found"
    }



  • 2.  RE: Unable to get VM Name with 0 (Zero)

    Posted Dec 16, 2022 03:52 PM

    Try something like this

    $prefix = 'mytestvm'
    
    $found = $false
    1..20 | ForEach-Object{
        $computer = "$prefix$('{0:d2}' -f $_)"
        if(-not (Test-Connection $computer -Count 1 -Quiet)){
            $found = $true
            break
        }
    }
    if($found){
        Write-Host "Found free computer name: $computer"
    }
    else{
        write-host "No free computer name found"
    }
    


  • 3.  RE: Unable to get VM Name with 0 (Zero)

    Posted Dec 16, 2022 04:16 PM

    LucD,

    I am getting blank output without any error



  • 4.  RE: Unable to get VM Name with 0 (Zero)
    Best Answer

    Posted Dec 16, 2022 05:27 PM

    My bad, I forgot that break doesn't work in a Foreach-Object code block.
    Try this alternative

    $prefix = 'mytestvm'
    
    $found = $false
    1..20 |  ForEach-Object -Process {
      $computer = "$prefix$('{0:d2}' -f $_)"
      if (-not $found -and -not (Test-Connection $computer -Count 1 -Quiet)) {
        $found = $true
        $freeName = $computer
      }
    }
    if ($found) {
      Write-Host "Found free computer name: $freeName"
    } else {
      Write-Host "No free computer name found"
    }


  • 5.  RE: Unable to get VM Name with 0 (Zero)

    Posted Dec 16, 2022 07:33 PM

    That worked as expected. Thank you very much LucD



  • 6.  RE: Unable to get VM Name with 0 (Zero)

    Posted Dec 16, 2022 08:23 PM

    I changed the scripts a little bit and get the following results, which will print out all the available name

    $prefix = 'mytestvm'
    
    $found = $false
    1..20 |  ForEach-Object -Process {
      $computer = "$prefix$('{0:d2}' -f $_)"
      if (-not $found -and -not (Test-Connection $computer -Count 1 -Quiet)) {
        $found = $true
        $freeName = $computer
     
    }
    if ($found) {
      Write-Host "Found free computer name: $computer"
    } else {
      Write-Host "No free computer name found"
    }}
    
    Found free computer name: mytestvm01
    Found free computer name: mytestvm02
    Found free computer name: mytestvm03
    Found free computer name: mytestvm04
    Found free computer name: mytestvm05
    Found free computer name: mytestvm06
    Found free computer name: mytestvm07
    Found free computer name: mytestvm08
    Found free computer name: mytestvm09
    Found free computer name: mytestvm10
    Found free computer name: mytestvm11
    Found free computer name: mytestvm12
    Found free computer name: mytestvm13
    Found free computer name: mytestvm14
    Found free computer name: mytestvm15
    Found free computer name: mytestvm16
    Found free computer name: mytestvm17
    Found free computer name: mytestvm18
    Found free computer name: mytestvm19
    Found free computer name: mytestvm20


  • 7.  RE: Unable to get VM Name with 0 (Zero)

    Posted Dec 16, 2022 08:48 PM

    Did you also check what the output shows when or more of those names are already in use?



  • 8.  RE: Unable to get VM Name with 0 (Zero)

    Posted Dec 16, 2022 09:28 PM
    $prefix = 'BDV2AESPRAPDD'
    
    $found = $false
    27..31 |  ForEach-Object -Process {
      $computer = "$prefix$('{0:d2}' -f $_)"
      if (-not $found -and -not (Test-Connection $computer -Count 1 -Quiet)) {
        $found = $true
        $freeName = $computer
     
    }
    if ($found) {
      Write-Host "Found free computer name: $computer"
    } else {
      Write-Host "No free computer name found"
    }}
    
    
    No free computer name found
    No free computer name found
    No free computer name found
    No free computer name found
    No free computer name found


  • 9.  RE: Unable to get VM Name with 0 (Zero)

    Posted Dec 16, 2022 11:01 PM

    For the result that you are likely looking for with the modified script, you also need to change its logic.
    With your modification, the if clause will never get executed again after setting $found to $true for the first time.

    $prefix = 'mytestvm'

    $found = $false
    1..20 | ForEach-Object -Process {
    $computer = "$prefix$('{0:d2}' -f $_)"
    if (-not (Test-Connection $computer -Count 1 -Quiet)) {
    $found = $true
    Write-Host "Found free computer name: $computer"
    }
    }

    if (-not $found) {
    Write-Host "No free computer name found"
    }

    André