skip to Main Content

I am trying to automate the azure resource deployment using powershell.

Requirments : In a particular resource group(RG), there are multiple VMs. the naming convention are like,

XXX-XX-X-XXXXXX-VM-01,

XXX-XX-X-XXXXXX-VM-02,

XXX-XX-X-XXXXXX-VM-03.

I am trying to create a script which will look for the last sequence(is this case 03) and deploy the next VM with next sequence(here it should be 04).

I have the script to deploy the VM using powershell. Need help to fix the sequence logic.

2

Answers


  1. I have a similar requirement and have been working on down the following route. In my case the numeric value is on the third element of the name, but I put in your value in hope this works for you.

    # I created an Array to hold the values returned from splitting the Name string.
    $CurrentVal = @()
    
    # Used a ForEach loop to Get VM in required Resource Group 
    ForEach ($vmName in (Get-AzVM -ResourceGroupName <resourcegroupName>).Name){
       # Convert to Decimal the Split String
       $vmNum = [Decimal]($vmName.split('-')[5])
       #Add to the Array
       $CurrentVal = $Currentval + $vmNum
     } 
    
    #Then got the value.
    $nextVal = ($Currentval | measure -maximum).Maximum + 1
    

    With the returned value, I can then ceate the next VM.

    Regards

    Login or Signup to reply.
  2. As a general reusable solution:

    function Get-NextString {
        param ([Parameter(ValueFromPipeline = $true)][String]$InputObject)
        begin {
            $Last, $Name = $Null
        }
        process {
            $Name, $Sequence = $_ -Split '(?=d*$)', 2
            if ([int]$Sequence -gt $Last) { $Last = $Sequence }
        }
        end {
            $Name + (1 + $Last).ToString("D$($Last.Length)")
        }
    }
    

    usage:

    $List =
    'XXX-XX-X-XXXXXX-VM-01',
    'XXX-XX-X-XXXXXX-VM-02',
    'XXX-XX-X-XXXXXX-VM-03'
    
    $List |Get-NextString
    XXX-XX-X-XXXXXX-VM-04
    

    Explanations:

    • param ([Parameter(ValueFromPipeline = $true)][String]$InputObject) accepts (multiple) strings from the pipeline, each string is processed as $_, the current item
    • For the begin, process and end blocks, read about Functions Advanced Methods
    • $Name, $Sequence = $_ -Split '(?=d*$)', 2 splits the current sting in the prefix ($name) and the sequence number. See also: Regular Expression
    • if ([int]$Sequence -gt $Last) { $Last = $Sequence } catch the highest number
      • The [int] in front of the $Sequence will force an numeric comparison
    • $Name + (1 + $Last).ToString("D$($Last.Length)") outputs the new string.
      • (1 + $Last) increase the $Last number with 1. Note that the $Last string will be automatically cast to an integer as the operant at the left hand side of the operator is an integer (1).
      • ToString("D$($Last.Length)") will format the sequence to the same number of digits as contained by $Last, see also: Pad a Number with Leading Zeros
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search