I"m trying to write a script to delete a VM on a certain date. Currently, I want people to add this to the VM name "VMname_Delete-/1/1/14", I then search for the VM with:
Get-VM | where {$_.name -cmatch "Delete-\d\/\d\/\d"}
With that, I'm sure that the correct VM is selected, and I tested it with some VMs to make sure. Now comes the hard part (at least for me), I want to take the date that's part of the VM name, and convert it to a [DateTime] value, and then compare it to the current date. If they match, I want to delete the VM. I was trying something along the lines of:
Get-VM | where {$_.name -cmatch "Delete-\d\/\d\/\d"} | foreach { $_.name -split "-"}
That of course split the dash, and on testing, I had an output similar to:
VM_Delete
1/1/14
Of course, now I don't know how to finish the rest. I was thinking that the script would go something like:
Get-VM | where {$_.name -cmatch "Delete-\d\/\d\/\d"} | foreach {
if ( ($_.name -split "-") -as [datetime] -match (get-date -format "MM/dd/yy") )
{Remove-VM -VM $_ -DeletePermanently}
}
I can think of the logic in my head, but of course, I just can't write it up they way I think that it should go. Any help is appreciated.