Automation

 View Only
  • 1.  Delete VM Based on VM Name

    Posted Mar 09, 2021 03:35 AM

    I am trying to write a PowerCLI script that will delete a VM based on it's naming convention. For example, the VM name is 'DTLA_exp_del_2021.09.13', what would be the best approach to accomplish this task.

    Any help is greatly appreciated, thank you.



  • 2.  RE: Delete VM Based on VM Name

    Posted Mar 09, 2021 08:00 AM

    If you want an exact match, I would opt for the match operator.
    This allows using a RegEx expression with the anchors ^ and $

    Make sure to use the Escape method to 'escape' the special characters in the name, like the . (dot) in your example.

    $vmName = 'DTLA_exp_del_2021\.09\.13'
    
    Get-VM | Where-Object {$_.Name -match [RegEx]::Escape("^$($vmName)$"} |
    Remove-VM -Confirm:$false -DeletePermanently
    

     



  • 3.  RE: Delete VM Based on VM Name

    Posted Mar 09, 2021 09:34 PM

    Thank you for replying to my question. If I decided to adjust the script to remove a VM that is grater than 30 days, would this be a correct approach-

    Get-Content c:\temp\servers.txt | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item -Recurse -Force

     



  • 4.  RE: Delete VM Based on VM Name

    Posted Mar 10, 2021 06:44 AM

    Not sure what you are trying to do?
    Removing files?
    That definitely does not remove VMs.



  • 5.  RE: Delete VM Based on VM Name

    Posted Mar 10, 2021 07:19 AM

    What I'm trying to do is delete VMs that are powered off that have a specific naming convention. For example, if the VM is named 'DTLA_exp_del_2021.2.9' and it's greater than 30 days then the script will delete the powered-off VM. 

    I'm not trying to delete files, only VM's that are older than 30 days.



  • 6.  RE: Delete VM Based on VM Name

    Posted Mar 10, 2021 08:15 AM

    The Remove-Item cmdlet is definitely for removing VMs, you have to remove the Remove-VM cmdlet for that.
    As I showed in my earlier snippet.

    Also, a VM object, which is returned by Get-VM, does not have a LastWriteTime property.
    Such  a VM object, since vSphere 6.7, does have a CreateDate property that you could use.



  • 7.  RE: Delete VM Based on VM Name

    Posted Mar 17, 2021 03:20 AM

    Since VM object does not have a LastWriteTime property. How can I delete VMs based on it's actual name. For example, I ran command below and stored the output into a variable named $vNames -

    $vNames = Get-VM | Where-Object{$_.Name -like "*exp_del*"}

    I then ran this command - 

    $vmsEXP = $vNames | %{$_.Name} 

    When I run $vmsEXP, the output is below -

    DTLA_exp_del_2021.03.13
    DTLA_exp_del_2020.02.7
    DTLA_exp_del_2021.01.17
    DTLA_exp_del_2020.08.03
    DTLA_exp_del_2020.12.11

    Keep in mind there is over 70 VMs with different date ranges. What I'm trying to accomplish is to delete the VMs that are 30 days old based on the VM name. Do I need to place $vmsEXP into an Array and some how figure out a way to remove the VMs. I'm not sure and I'm stuck at this point since VM object does not have a LastWriteTime property.

     

     

     



  • 8.  RE: Delete VM Based on VM Name

    Posted Mar 17, 2021 06:59 AM

    You could do something like this

    $tgtDate = (Get-Date).AddDays(-30)
    
    Get-VM | Where-Object{$_.Name -like "*exp_del*"} |
    ForEach-Object -Process {
        if([DateTime]($_.Split('_')[-1]) -le $tgtDate){
            Remove-VM -VM $_ -Confirm:$false
        }
    }