Jump to content

Managing VMWare with AutoIT


 Share

Recommended Posts

With the below command I am able to revert a virtual machine in vmware workstation to a snapshot mentioned.

$sVMPath = <path of the vmx file of virtual machine>

$sSnapshot = <snapshot name>

$Ret = RunWait(@ComSpec & " /c VMRun.exe -T WS revertToSnapshot ""$sVMPath$"" ""$sSnapshot$"" nogui", "", @SW_HIDE)

But the problem here is the virtual machine may contain multiple snapshots with same name because multiple users use same workstation and eventually they are keeping same name sometimes like below or in attachment.

Both snapshots are with same name and in GUI we can use them with no issue.

Capture1.JPG

 

But with autoit or with vmrun command line it is showing error as 

Error: The name does not uniquely identify one snapshot

Could anyone please help me on this.

Edited by ur
Link to comment
Share on other sites

  • Moderators

@ur I have moved your thread to the appropriate forum; please be mindful of where you post.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Hi.

I don't use VM-Workstation, so I don't have a clue, if the VMware PowerCLI can be used with "Workstation" as well.

This script might give you a start to collect all the snapshots. Using the snapshot date, you can pick the latest one, and as powershell returns objects, you should be able to use the "youngest" snapshot later on to revert to that one.

if (get-pssnapin vmware* -erroraction silentlycontinue) {
    write "PowerCLI Extensions are available (OK)"
} else {
    write "PowerCLI Extensions don't seem to be available,"
    write "but required for this script!"
    exit
}


$VIserver="VirtualCenterServerName"

disconnect-viserver -confirm:$false -ea silentlycontinue
connect-viserver $VIserver

$SnapsExist=$true
$time=get-date -uformat "%Y-%m-%d@%Hh:%Mm:%Ss"
$TimeFile=$time -replace ":","'"
$TimeString=$time -replace ":"," "
$TimeString=$TimeString -replace "@"," um "
$TimeString = "Stand: " + $TimeString   
    
$Report = Get-VM | Get-Snapshot | Select VM,Name,Description,@{Label="Size";Expression={"{0:N2} GB" -f ($_.SizeGB)}},Created
If (-not $Report)
{  $Report = New-Object PSObject -Property @{
      VM = "No snapshots found on any VM's controlled by $VIServer"
      Name = ""
      Description = ""
      Size = ""
      Created = ""
   }
   $SnapsExist=$false
}
$Report = $Report | 
    Select VM,Name,Description,Size,Created | 
    ConvertTo-Html -Head $Header -PreContent $("<p><h2>Snapshot Report - $VIServer.<br>" + $TimeString + "</h2></p><br>")

$FTstyle = @"
<body>

<style type="text/css">
        .TFtable{
                width:100%;
                border-collapse:collapse;
        }
        .TFtable td{
                padding:7px; border:#4e95f4 1px solid;
        }
        /* provide some minimal visual accomodation for IE8 and below */
        .TFtable tr{
                background: #b8d1f3;
        }
        /*  Define the background color for all the ODD background rows  */
        .TFtable tr:nth-child(odd){
                background: #ffffff;
        }
        /*  Define the background color for all the EVEN background rows  */
        .TFtable tr:nth-child(even){
                background: #ccffcc;
        }
</style>
"@


$SaveTo=$($env:temp + "\VMs-mit-Snapshots_" + $TimeStamp + ".html")


$R2 = $Report -replace "<body>",$FTStyle
$R3 = $R2 -replace "<table>",'<table class="TFtable">'
$R3 | out-file $SaveTo
Write-host "VMs with snapshots are to be found in this HTML Report:"
write-host $SaveTo
. $SaveTo



if ($SnapsExist -and (get-pssnapin *exchange* -erroraction silentlycontinue)) {
    $smtpServer = "YourMailServerInternalIP"
    $att = new-object Net.Mail.Attachment($SaveTo)
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.From = "VMs-with-Snapshots@your-corp.local"
    $msg.To.Add(([adsi]"LDAP://$(whoami /fqdn)").mail)
    $msg.Subject = "VMs with snapshots, as of " + $TimeString
    $msg.Body = "open separately (The Outlook 2010 Preview Feature won't display this HTML properly)."
    $msg.Attachments.Add($att)
    $smtp.Send($msg)
    $att.Dispose()
}

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...