none
Script powershell to export a vm and delete the oldest folder RRS feed

  • Question

  • Hi everyone!

    I'm creating a script in powershell  to export a virtual machine and delete the oldest folder.

    So, we need to run this script everyday and we need to keep just two folders of the exportation.

    I'm not an expert in scripting and if someone can help me it will be so good.

    Let me explain my "Logic"
     The script will export the virtual machine into the $basedir and the variable $today is to save the folder name with the date.
     The second part is to delete the oldest folder into the $basedir
     The third part is to export the VM
       - This part is not right, i Can export the VM, but i put the variable $today to insert the date with the name of the exportation. I mean "i have a vm called 'VM01' and after the export i would like that folder changes for VM01DATA 'VM01020217'

    Can you help me please?

    $basedir = "H:\TEST\VM-01\"
    $today = (Get-Date).ToString('mm_dd_yyyy')

    Get-ChildItem $basedir |
    sort-Object { $_.Name -as [Version] } |
    Select-Object -Last 1 |
    Remove-Item

    $location = New-Item -Path $basedir -Type Directory -Name $today
    Export-VM -name VM-01 -Path H:\TEST\VM01 + $today


    Na vida há dois caminhos mais só um que vai na direção certa

    Thursday, February 2, 2017 4:31 PM

Answers

  • Get-ChildItem $basedir |
    sort-Object { $_.Name -as [Version] } |
    Select-Object -Last 1 |
    Remove-Item

    I changed the option -Last for First

    Get-ChildItem $basedir |
    sort-Object { $_.Name -as [Version] } |
    Select-Object -First 1 |
    Remove-Item

    Thank you


    Na vida há dois caminhos mais só um que vai na direção certa

    • Marked as answer by Julio Melo Friday, February 3, 2017 10:10 AM
    Friday, February 3, 2017 10:10 AM

All replies

  • $location = New-Item -Path $basedir -Type Directory -Name $today $pathName = "H:\Test\VM01"+$today Export-VM -name VM-01 -Path $pathName

    This should work for you.  It's not liking the spaces in between H:\Test\VM01 and + $today
    • Proposed as answer by SYN_ACK_87 Thursday, February 2, 2017 4:38 PM
    Thursday, February 2, 2017 4:38 PM
  • Thank you my friend!

    May I ask another question?

    I don't know why, but, it's deleting the new folder this code

    Get-ChildItem $basedir |
    sort-Object { $_.Name -as [Version] } |
    Select-Object -Last 1 |
    Remove-Item

    For me it's normal.

    Can you help me please?

    Thank you


    Na vida há dois caminhos mais só um que vai na direção certa

    Friday, February 3, 2017 9:28 AM
  • Get-ChildItem $basedir |
    sort-Object { $_.Name -as [Version] } |
    Select-Object -Last 1 |
    Remove-Item

    I changed the option -Last for First

    Get-ChildItem $basedir |
    sort-Object { $_.Name -as [Version] } |
    Select-Object -First 1 |
    Remove-Item

    Thank you


    Na vida há dois caminhos mais só um que vai na direção certa

    • Marked as answer by Julio Melo Friday, February 3, 2017 10:10 AM
    Friday, February 3, 2017 10:10 AM