Advertisement
Guest User

VMWare's vmrun.exe wrapper

a guest
May 8th, 2018
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 68.83 KB | None | 0 0
  1.     ''' ----------------------------------------------------------------------------------------------------
  2.     ''' <summary>
  3.     ''' A wrapper of VmWare's vmrun.exe application.
  4.     ''' </summary>
  5.     ''' ----------------------------------------------------------------------------------------------------
  6.     ''' <example> This is a code example that demonstrates how to get the running virtual machines, then run a program on each guest operating system.
  7.     ''' <code>
  8.     ''' Private vmRun As VmRunWrapper
  9.     '''
  10.     ''' Private Async Sub Test()
  11.     '''
  12.     '''     Me.vmRun = New VmRunWrapper("C:\Program Files (x86)\VMWare\VMware VIX\vmrun.exe")
  13.     '''
  14.     '''     Dim vmCount As Integer = Await Me.vmRun.GetRunningVmCountAsync()
  15.     '''     If (vmCount > 0) Then
  16.     '''
  17.     '''         Dim vms As ReadOnlyCollection(Of VMWareVirtualMachine) = Await Me.vmRun.GetRunningVmsAsync()
  18.     '''
  19.     '''         For Each vm As VMWareVirtualMachine In vms
  20.     '''
  21.     '''             ' Check whether VMWare-Tools are installed in the VM.
  22.     '''             ' The VmWare-Tools are required by some of the functionalities of vmrun.exe program.
  23.     '''             Dim isVMWareToolsInstalled As Boolean = Await Me.vmRun.IsVmWareToolsInstalledAsync(vm)
  24.     '''             Console.WriteLine("VM Name: {0}; IsVMWareToolsInstalled: {1}'", vm.DisplayName, isVMWareToolsInstalled)
  25.     '''
  26.     '''             If Not isVMWareToolsInstalled Then
  27.     '''                 Me.vmRun.InstallVmWareTools(vm)
  28.     '''                 Continue For
  29.     '''             End If
  30.     '''
  31.     '''             ' A valid guest username and password (if any) is required in order to use some of the functionalities of vmrun.exe program.
  32.     '''             vm.GuestOsCredential.Username = "guest username"
  33.     '''             vm.GuestOsCredential.Password = "guest password"
  34.     '''
  35.     '''             Try
  36.     '''                 ' Run a random program on the guest operating system.
  37.     '''                 Me.vmRun.ProcessRun(vm, "C:\program.exe", VmRunProgramFlags.NoWait Or VmRunProgramFlags.ActiveWindow Or VmRunProgramFlags.Interactive, "")
  38.     '''
  39.     '''             Catch ex As VmRunException
  40.     '''                 Throw
  41.     '''
  42.     '''             Catch ex As Exception
  43.     '''                 Throw
  44.     '''
  45.     '''             End Try
  46.     '''
  47.     '''         Next
  48.     '''
  49.     '''     End If
  50.     '''
  51.     ''' End Sub
  52.     ''' </code>
  53.     ''' </example>
  54.     ''' ----------------------------------------------------------------------------------------------------
  55.     <ImmutableObject(True)>
  56.     Public NotInheritable Class VmRunWrapper : Implements IDisposable
  57.  
  58. #Region " Properties "
  59.  
  60.         ''' ----------------------------------------------------------------------------------------------------
  61.         ''' <summary>
  62.         ''' Gets the <c>VmRun.exe</c> file path.
  63.         ''' </summary>
  64.         ''' ----------------------------------------------------------------------------------------------------
  65.         Public ReadOnly Property FilePath As String
  66.  
  67.         ''' ----------------------------------------------------------------------------------------------------
  68.         ''' <summary>
  69.         ''' Gets a value indicating whether the <c>VmRun.exe</c> file Exists.
  70.         ''' </summary>
  71.         ''' ----------------------------------------------------------------------------------------------------
  72.         Public ReadOnly Property Exists As Boolean
  73.             Get
  74.                 Return File.Exists(Me.FilePath)
  75.             End Get
  76.         End Property
  77.  
  78.         ''' ----------------------------------------------------------------------------------------------------
  79.         ''' <summary>
  80.         ''' Gets the <c>VmRun.exe</c> <see cref="Diagnostics.Process"/> instance.
  81.         ''' </summary>
  82.         ''' ----------------------------------------------------------------------------------------------------
  83.         Public ReadOnly Property Process As Process
  84.  
  85. #End Region
  86.  
  87. #Region " Constructors "
  88.  
  89.         ''' ----------------------------------------------------------------------------------------------------
  90.         ''' <summary>
  91.         ''' Prevents a default instance of the <see cref="VmRunWrapper"/> class from being created.
  92.         ''' </summary>
  93.         ''' ----------------------------------------------------------------------------------------------------
  94.         Private Sub New()
  95.         End Sub
  96.  
  97.         ''' ----------------------------------------------------------------------------------------------------
  98.         ''' <summary>
  99.         ''' Initializes a new instance of the <see cref="VmRunWrapper"/> class.
  100.         ''' </summary>
  101.         ''' ----------------------------------------------------------------------------------------------------
  102.         ''' <param name="filepath">
  103.         ''' The <c>VmRun.exe</c> filepath.
  104.         ''' </param>
  105.         ''' ----------------------------------------------------------------------------------------------------
  106.         <DebuggerStepThrough>
  107.         Public Sub New(ByVal filepath As String)
  108.  
  109.             Me.FilePath = filepath
  110.  
  111.             Me.Process =
  112.                 New Process With {.StartInfo =
  113.                     New ProcessStartInfo With {
  114.                         .FileName = filepath,
  115.                         .CreateNoWindow = True,
  116.                         .UseShellExecute = False,
  117.                         .RedirectStandardError = False,
  118.                         .RedirectStandardOutput = True
  119.                     }
  120.                 }
  121.  
  122.         End Sub
  123.  
  124. #End Region
  125.  
  126. #Region " Public Methods "
  127.  
  128.         ''' ----------------------------------------------------------------------------------------------------
  129.         ''' <summary>
  130.         ''' Gets the amount of virtual machines that are running on the host operating system.
  131.         ''' </summary>
  132.         ''' ----------------------------------------------------------------------------------------------------
  133.         ''' <returns>
  134.         ''' The amount of virtual machines that are running on the host operating system.
  135.         ''' </returns>
  136.         ''' ----------------------------------------------------------------------------------------------------
  137.         <DebuggerStepThrough>
  138.         Public Function GetRunningVmCount() As Integer
  139.             Dim stdOut As String = Me.RunProcess("-T ws list")
  140.             Dim line As String = stdOut.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).First()
  141.             Dim count As Integer = CInt(line.Replace("Total running VMs: ", ""))
  142.  
  143.             Return count
  144.         End Function
  145.  
  146.         ''' ----------------------------------------------------------------------------------------------------
  147.         ''' <summary>
  148.         ''' Asynchronously gets the amount of virtual machines that are running on the host operating system.
  149.         ''' </summary>
  150.         ''' ----------------------------------------------------------------------------------------------------
  151.         ''' <returns>
  152.         ''' The amount of virtual machines that are running on the host operating system.
  153.         ''' </returns>
  154.         ''' ----------------------------------------------------------------------------------------------------
  155.         <DebuggerStepThrough>
  156.         Public Async Function GetRunningVmCountAsync() As Task(Of Integer)
  157.  
  158.             Dim t As New Task(Of Integer)(AddressOf Me.GetRunningVmCount)
  159.             t.Start()
  160.             Await t
  161.             Return t.Result
  162.  
  163.         End Function
  164.  
  165.         ''' ----------------------------------------------------------------------------------------------------
  166.         ''' <summary>
  167.         ''' Gets the file paths of the virtual machines that are running on the host operating system.
  168.         ''' </summary>
  169.         ''' ----------------------------------------------------------------------------------------------------
  170.         ''' <returns>
  171.         ''' The file paths of the virtual machines that are running on the host operating system.
  172.         ''' </returns>
  173.         ''' ----------------------------------------------------------------------------------------------------
  174.         <DebuggerStepThrough>
  175.         Public Function GetRunningVms() As ReadOnlyCollection(Of VMWareVirtualMachine)
  176.  
  177.             Dim vms As New Collection(Of VMWareVirtualMachine)
  178.  
  179.             Dim stdOut As String = Me.RunProcess("-T ws list")
  180.  
  181.             For Each line As String In stdOut.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
  182.                 If Not line.StartsWith("Total running VMs:", StringComparison.OrdinalIgnoreCase) Then
  183.                     Dim filepath As String = line
  184.                     Dim vm As New VMWareVirtualMachine(filepath, isSharedVm:=False)
  185.                     vms.Add(vm)
  186.                 End If
  187.             Next line
  188.  
  189.             Return New ReadOnlyCollection(Of VMWareVirtualMachine)(vms)
  190.  
  191.         End Function
  192.  
  193.         ''' ----------------------------------------------------------------------------------------------------
  194.         ''' <summary>
  195.         ''' Asynchronously gets the file paths of the virtual machines that are running on the host operating system.
  196.         ''' </summary>
  197.         ''' ----------------------------------------------------------------------------------------------------
  198.         ''' <returns>
  199.         ''' The file paths of the virtual machines that are running on the host operating system.
  200.         ''' </returns>
  201.         ''' ----------------------------------------------------------------------------------------------------
  202.         <DebuggerStepThrough>
  203.         Public Async Function GetRunningVmsAsync() As Task(Of ReadOnlyCollection(Of VMWareVirtualMachine))
  204.  
  205.             Dim t As New Task(Of ReadOnlyCollection(Of VMWareVirtualMachine))(AddressOf Me.GetRunningVms)
  206.             t.Start()
  207.             Await t
  208.             Return t.Result
  209.  
  210.         End Function
  211.  
  212.         ''' ----------------------------------------------------------------------------------------------------
  213.         ''' <summary>
  214.         ''' Gets the amount of snapshots created in the specified virtual machine.
  215.         ''' </summary>
  216.         ''' ----------------------------------------------------------------------------------------------------
  217.         ''' <param name="vm">
  218.         ''' The VMWare virtual machine.
  219.         ''' </param>
  220.         ''' ----------------------------------------------------------------------------------------------------
  221.         ''' <returns>
  222.         ''' The amount of snapshots created.
  223.         ''' </returns>
  224.         ''' ----------------------------------------------------------------------------------------------------
  225.         <DebuggerStepThrough>
  226.         Public Function GetSnapshotCount(ByVal vm As VMWareVirtualMachine) As Integer
  227.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws listSnapshots ""{0}""", vm.VmxFile.FullName))
  228.             Dim line As String = stdOut.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).First()
  229.             Dim count As Integer = CInt(line.Replace("Total snapshots: ", ""))
  230.  
  231.             Return count
  232.         End Function
  233.  
  234.         ''' ----------------------------------------------------------------------------------------------------
  235.         ''' <summary>
  236.         ''' Asynchronously gets the amount of snapshots created in the specified virtual machine.
  237.         ''' </summary>
  238.         ''' ----------------------------------------------------------------------------------------------------
  239.         ''' <param name="vm">
  240.         ''' The VMWare virtual machine.
  241.         ''' </param>
  242.         ''' ----------------------------------------------------------------------------------------------------
  243.         ''' <returns>
  244.         ''' The amount of snapshots created.
  245.         ''' </returns>
  246.         ''' ----------------------------------------------------------------------------------------------------
  247.         <DebuggerStepThrough>
  248.         Public Async Function GetSnapshotCountAsync(ByVal vm As VMWareVirtualMachine) As Task(Of Integer)
  249.  
  250.             Dim t As New Task(Of Integer)(Function() Me.GetSnapshotCount(vm))
  251.             t.Start()
  252.             Await t
  253.             Return t.Result
  254.  
  255.         End Function
  256.  
  257.         ''' ----------------------------------------------------------------------------------------------------
  258.         ''' <summary>
  259.         ''' Gets the names of the snapshots created in the specified virtual machine.
  260.         ''' </summary>
  261.         ''' ----------------------------------------------------------------------------------------------------
  262.         ''' <param name="vm">
  263.         ''' The VMWare virtual machine.
  264.         ''' </param>
  265.         ''' ----------------------------------------------------------------------------------------------------
  266.         ''' <returns>
  267.         ''' The snapshot names.
  268.         ''' </returns>
  269.         ''' ----------------------------------------------------------------------------------------------------
  270.         <DebuggerStepThrough>
  271.         Public Function GetSnapshotNames(ByVal vm As VMWareVirtualMachine) As ReadOnlyCollection(Of String)
  272.  
  273.             Dim names As New Collection(Of String)
  274.  
  275.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws listSnapshots ""{0}""", vm.VmxFile.FullName))
  276.  
  277.             For Each line As String In stdOut.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
  278.                 If Not line.StartsWith("Total snapshots:", StringComparison.OrdinalIgnoreCase) Then
  279.                     Dim name As String = line
  280.                     names.add(name)
  281.                 End If
  282.             Next line
  283.  
  284.             Return New ReadOnlyCollection(Of String)(names)
  285.  
  286.         End Function
  287.  
  288.         ''' ----------------------------------------------------------------------------------------------------
  289.         ''' <summary>
  290.         ''' Asynchronously gets the names of the snapshots created in the specified virtual machine.
  291.         ''' </summary>
  292.         ''' ----------------------------------------------------------------------------------------------------
  293.         ''' <param name="vm">
  294.         ''' The VMWare virtual machine.
  295.         ''' </param>
  296.         ''' ----------------------------------------------------------------------------------------------------
  297.         ''' <returns>
  298.         ''' The snapshot names.
  299.         ''' </returns>
  300.         ''' ----------------------------------------------------------------------------------------------------
  301.         <DebuggerStepThrough>
  302.         Public Async Function GetSnapshotNamesAsync(ByVal vm As VMWareVirtualMachine) As Task(Of ReadOnlyCollection(Of String))
  303.  
  304.             Dim t As New Task(Of ReadOnlyCollection(Of String))(Function() Me.GetSnapshotNames(vm))
  305.             t.Start()
  306.             Await t
  307.             Return t.Result
  308.  
  309.         End Function
  310.  
  311.         ''' ----------------------------------------------------------------------------------------------------
  312.         ''' <summary>
  313.         ''' Gets a tree-view of the snapshots created in the specified virtual machine.
  314.         ''' </summary>
  315.         ''' ----------------------------------------------------------------------------------------------------
  316.         ''' <param name="vm">
  317.         ''' The VMWare virtual machine.
  318.         ''' </param>
  319.         ''' ----------------------------------------------------------------------------------------------------
  320.         ''' <returns>
  321.         ''' A tree-view of the snapshots.
  322.         ''' </returns>
  323.         ''' ----------------------------------------------------------------------------------------------------
  324.         <DebuggerStepThrough>
  325.         Public Function GetSnapshotsTreeview(ByVal vm As VMWareVirtualMachine) As String
  326.  
  327.             Dim sb As New StringBuilder()
  328.  
  329.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws listSnapshots ""{0}"" showTree", vm.VmxFile.FullName))
  330.  
  331.             For Each line As String In stdOut.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
  332.                 If Not line.StartsWith("Total snapshots:", StringComparison.OrdinalIgnoreCase) Then
  333.                     sb.AppendLine(line)
  334.                 End If
  335.             Next line
  336.  
  337.             Return sb.ToString()
  338.  
  339.         End Function
  340.  
  341.         ''' ----------------------------------------------------------------------------------------------------
  342.         ''' <summary>
  343.         ''' Asynchronously gets a tree-view of the snapshots created in the specified virtual machine.
  344.         ''' </summary>
  345.         ''' ----------------------------------------------------------------------------------------------------
  346.         ''' <param name="vm">
  347.         ''' The VMWare virtual machine.
  348.         ''' </param>
  349.         ''' ----------------------------------------------------------------------------------------------------
  350.         ''' <returns>
  351.         ''' A tree-view of the snapshots.
  352.         ''' </returns>
  353.         ''' ----------------------------------------------------------------------------------------------------
  354.         <DebuggerStepThrough>
  355.         Public Async Function GetSnapshotsTreeviewAsync(ByVal vm As VMWareVirtualMachine) As Task(Of String)
  356.  
  357.             Dim t As New Task(Of String)(Function() Me.GetSnapshotsTreeview(vm))
  358.             t.Start()
  359.             Await t
  360.             Return t.Result
  361.  
  362.         End Function
  363.  
  364.         ''' ----------------------------------------------------------------------------------------------------
  365.         ''' <summary>
  366.         ''' Gets a value that determine whether VmWare Tools is installed in the specified virtual machine.
  367.         ''' </summary>
  368.         ''' ----------------------------------------------------------------------------------------------------
  369.         ''' <param name="vm">
  370.         ''' The VMWare virtual machine.
  371.         ''' </param>
  372.         ''' ----------------------------------------------------------------------------------------------------
  373.         ''' <returns>
  374.         ''' <see langword="True"/> if VmWare Tools is installed in the specified virtual machine; otherwise, <see langword="False"/>.
  375.         ''' </returns>
  376.         ''' ----------------------------------------------------------------------------------------------------
  377.         <DebuggerStepThrough>
  378.         Public Function IsVmWareToolsInstalled(ByVal vm As VMWareVirtualMachine) As Boolean
  379.  
  380.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws checkToolsState ""{0}""", vm.VmxFile.FullName))
  381.  
  382.             Return stdOut.Equals("installed", StringComparison.OrdinalIgnoreCase) OrElse
  383.                    stdOut.Equals("running", StringComparison.OrdinalIgnoreCase)
  384.  
  385.         End Function
  386.  
  387.         ''' ----------------------------------------------------------------------------------------------------
  388.         ''' <summary>
  389.         ''' Asynchronously gets a value that determine whether VmWare Tools is installed in the specified virtual machine.
  390.         ''' </summary>
  391.         ''' ----------------------------------------------------------------------------------------------------
  392.         ''' <param name="vm">
  393.         ''' The VMWare virtual machine.
  394.         ''' </param>
  395.         ''' ----------------------------------------------------------------------------------------------------
  396.         ''' <returns>
  397.         ''' <see langword="True"/> if VmWare Tools is installed in the specified virtual machine; otherwise, <see langword="False"/>.
  398.         ''' </returns>
  399.         ''' ----------------------------------------------------------------------------------------------------
  400.         <DebuggerStepThrough>
  401.         Public Async Function IsVmWareToolsInstalledAsync(ByVal vm As VMWareVirtualMachine) As Task(Of Boolean)
  402.  
  403.             Dim t As New Task(Of Boolean)(Function() Me.IsVmWareToolsInstalled(vm))
  404.             t.Start()
  405.             Await t
  406.             Return t.Result
  407.  
  408.         End Function
  409.  
  410.         ''' ----------------------------------------------------------------------------------------------------
  411.         ''' <summary>
  412.         ''' Start the specified virtual machine.
  413.         ''' </summary>
  414.         ''' ----------------------------------------------------------------------------------------------------
  415.         ''' <param name="vm">
  416.         ''' The VMWare virtual machine.
  417.         ''' </param>
  418.         '''
  419.         ''' <param name="noGui">
  420.         ''' A value indicating whether to start the virtual machine with VmWare GUI or not.
  421.         ''' </param>
  422.         ''' ----------------------------------------------------------------------------------------------------
  423.         <DebuggerStepThrough>
  424.         Public Sub VmStart(ByVal vm As VMWareVirtualMachine, ByVal noGui As Boolean)
  425.  
  426.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws start ""{0}"" {1}", vm.VmxFile.FullName, If(noGui, "nogui", "gui")))
  427.  
  428.             vm.Refresh()
  429.  
  430.         End Sub
  431.  
  432.         ''' ----------------------------------------------------------------------------------------------------
  433.         ''' <summary>
  434.         ''' Stop the specified virtual machine.
  435.         ''' </summary>
  436.         ''' ----------------------------------------------------------------------------------------------------
  437.         ''' <param name="vm">
  438.         ''' The VMWare virtual machine.
  439.         ''' </param>
  440.         '''
  441.         ''' <param name="hardStop">
  442.         ''' A value indicating whether to perform a hard or soft stop.
  443.         ''' </param>
  444.         ''' ----------------------------------------------------------------------------------------------------
  445.         <DebuggerStepThrough>
  446.         Public Sub VmStop(ByVal vm As VMWareVirtualMachine, ByVal hardStop As Boolean)
  447.  
  448.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws stop ""{0}"" {1}", vm.VmxFile.FullName, If(hardStop, "hard", "soft")))
  449.  
  450.             vm.Refresh()
  451.  
  452.         End Sub
  453.  
  454.         ''' ----------------------------------------------------------------------------------------------------
  455.         ''' <summary>
  456.         ''' Reset the specified virtual machine.
  457.         ''' </summary>
  458.         ''' ----------------------------------------------------------------------------------------------------
  459.         ''' <param name="vm">
  460.         ''' The VMWare virtual machine.
  461.         ''' </param>
  462.         '''
  463.         ''' <param name="hardReset">
  464.         ''' A value indicating whether to perform a hard or soft reset.
  465.         ''' </param>
  466.         ''' ----------------------------------------------------------------------------------------------------
  467.         <DebuggerStepThrough>
  468.         Public Sub VmReset(ByVal vm As VMWareVirtualMachine, ByVal hardReset As Boolean)
  469.  
  470.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws reset ""{0}"" {1}", vm.VmxFile.FullName, If(hardReset, "hard", "soft")))
  471.  
  472.             vm.Refresh()
  473.  
  474.         End Sub
  475.  
  476.         ''' ----------------------------------------------------------------------------------------------------
  477.         ''' <summary>
  478.         ''' Suspend the specified virtual machine.
  479.         ''' </summary>
  480.         ''' ----------------------------------------------------------------------------------------------------
  481.         ''' <param name="vm">
  482.         ''' The VMWare virtual machine.
  483.         ''' </param>
  484.         '''
  485.         ''' <param name="hardSuspend">
  486.         ''' A value indicating whether to perform a hard or soft suspend.
  487.         ''' </param>
  488.         ''' ----------------------------------------------------------------------------------------------------
  489.         <DebuggerStepThrough>
  490.         Public Sub VmSuspend(ByVal vm As VMWareVirtualMachine, ByVal hardSuspend As Boolean)
  491.  
  492.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws suspend ""{0}"" {1}", vm.VmxFile.FullName, If(hardSuspend, "hard", "soft")))
  493.  
  494.             vm.Refresh()
  495.  
  496.         End Sub
  497.  
  498.         ''' ----------------------------------------------------------------------------------------------------
  499.         ''' <summary>
  500.         ''' Pause the specified virtual machine.
  501.         ''' </summary>
  502.         ''' ----------------------------------------------------------------------------------------------------
  503.         ''' <param name="vm">
  504.         ''' The VMWare virtual machine.
  505.         ''' </param>
  506.         ''' ----------------------------------------------------------------------------------------------------
  507.         <DebuggerStepThrough>
  508.         Public Sub VmPause(ByVal vm As VMWareVirtualMachine)
  509.  
  510.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws pause ""{0}""", vm.VmxFile.FullName))
  511.  
  512.             vm.Refresh()
  513.  
  514.         End Sub
  515.  
  516.         ''' ----------------------------------------------------------------------------------------------------
  517.         ''' <summary>
  518.         ''' Resume (unpause) the specified virtual machine.
  519.         ''' </summary>
  520.         ''' ----------------------------------------------------------------------------------------------------
  521.         ''' <param name="vm">
  522.         ''' The VMWare virtual machine.
  523.         ''' </param>
  524.         ''' ----------------------------------------------------------------------------------------------------
  525.         <DebuggerStepThrough>
  526.         Public Sub VmResume(ByVal vm As VMWareVirtualMachine)
  527.  
  528.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws unpause ""{0}""", vm.VmxFile.FullName))
  529.  
  530.             vm.Refresh()
  531.  
  532.         End Sub
  533.  
  534.         ''' ----------------------------------------------------------------------------------------------------
  535.         ''' <summary>
  536.         ''' Deletes the specified virtual machine.
  537.         ''' </summary>
  538.         ''' ----------------------------------------------------------------------------------------------------
  539.         ''' <param name="vm">
  540.         ''' The VMWare virtual machine.
  541.         ''' </param>
  542.         ''' ----------------------------------------------------------------------------------------------------
  543.         <DebuggerStepThrough>
  544.         Public Sub VmDelete(ByVal vm As VMWareVirtualMachine)
  545.  
  546.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws deleteVm ""{0}""", vm.VmxFile.FullName))
  547.  
  548.         End Sub
  549.  
  550.         ''' ----------------------------------------------------------------------------------------------------
  551.         ''' <summary>
  552.         ''' Create a snapshot of the specified virtual machine.
  553.         ''' </summary>
  554.         ''' ----------------------------------------------------------------------------------------------------
  555.         ''' <param name="vm">
  556.         ''' The VMWare virtual machine.
  557.         ''' </param>
  558.         '''
  559.         ''' <param name="name">
  560.         ''' The snapshot name.
  561.         ''' </param>
  562.         ''' ----------------------------------------------------------------------------------------------------
  563.         <DebuggerStepThrough>
  564.         Public Sub SnapshotCreate(ByVal vm As VMWareVirtualMachine, ByVal name As String)
  565.  
  566.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws snapshot ""{0}"" ""{1}""", vm.VmxFile.FullName, name))
  567.  
  568.             vm.Refresh()
  569.  
  570.         End Sub
  571.  
  572.         ''' ----------------------------------------------------------------------------------------------------
  573.         ''' <summary>
  574.         ''' Delete a snapshot from the specified virtual machine.
  575.         ''' </summary>
  576.         ''' ----------------------------------------------------------------------------------------------------
  577.         ''' <param name="vm">
  578.         ''' The VMWare virtual machine.
  579.         ''' </param>
  580.         '''
  581.         ''' <param name="name">
  582.         ''' The snapshot name.
  583.         ''' </param>
  584.         ''' ----------------------------------------------------------------------------------------------------
  585.         <DebuggerStepThrough>
  586.         Public Sub SnapshotDelete(ByVal vm As VMWareVirtualMachine, ByVal name As String)
  587.  
  588.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws deleteSnapshot ""{0}"" ""{1}""", vm.VmxFile.FullName, name))
  589.  
  590.             vm.Refresh()
  591.  
  592.         End Sub
  593.  
  594.         ''' ----------------------------------------------------------------------------------------------------
  595.         ''' <summary>
  596.         ''' Revert the state of the specified virtual machine to the state of the specified snapshot.
  597.         ''' </summary>
  598.         ''' ----------------------------------------------------------------------------------------------------
  599.         ''' <param name="vm">
  600.         ''' The VMWare virtual machine.
  601.         ''' </param>
  602.         '''
  603.         ''' <param name="name">
  604.         ''' The snapshot name.
  605.         ''' </param>
  606.         ''' ----------------------------------------------------------------------------------------------------
  607.         <DebuggerStepThrough>
  608.         Public Sub SnapshotRestore(ByVal vm As VMWareVirtualMachine, ByVal name As String)
  609.  
  610.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws revertToSnapshot ""{0}"" ""{1}""", vm.VmxFile.FullName, name))
  611.  
  612.             vm.Refresh()
  613.  
  614.         End Sub
  615.  
  616.         ''' ----------------------------------------------------------------------------------------------------
  617.         ''' <summary>
  618.         ''' Determine whether a file exists in the guest operating system of the specified virtual machine.
  619.         ''' </summary>
  620.         ''' ----------------------------------------------------------------------------------------------------
  621.         ''' <param name="vm">
  622.         ''' The VMWare virtual machine.
  623.         ''' </param>
  624.         '''
  625.         ''' <param name="filepath">
  626.         ''' The full path to a existing file in the guest operating system.
  627.         ''' </param>
  628.         ''' ----------------------------------------------------------------------------------------------------
  629.         ''' <returns>
  630.         ''' <see langword="True"/> if the file exists in the guest operating system; otherwise, <see langword="False"/>.
  631.         ''' </returns>
  632.         ''' ----------------------------------------------------------------------------------------------------
  633.         <DebuggerStepThrough>
  634.         Public Function FileExists(ByVal vm As VMWareVirtualMachine, ByVal filepath As String) As Boolean
  635.  
  636.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" fileExistsInGuest ""{2}"" ""{3}""",
  637.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  638.                                                                vm.VmxFile.FullName, filepath))
  639.  
  640.             Return stdOut.Equals("The file exists.", StringComparison.OrdinalIgnoreCase)
  641.  
  642.         End Function
  643.  
  644.         ''' ----------------------------------------------------------------------------------------------------
  645.         ''' <summary>
  646.         ''' Asynchronously determine whether a file exists in the guest operating system of the specified virtual machine.
  647.         ''' </summary>
  648.         ''' ----------------------------------------------------------------------------------------------------
  649.         ''' <param name="vm">
  650.         ''' The VMWare virtual machine.
  651.         ''' </param>
  652.         '''
  653.         ''' <param name="filepath">
  654.         ''' The full path to a existing file in the guest operating system.
  655.         ''' </param>
  656.         ''' ----------------------------------------------------------------------------------------------------
  657.         ''' <returns>
  658.         ''' <see langword="True"/> if the file exists in the guest operating system; otherwise, <see langword="False"/>.
  659.         ''' </returns>
  660.         ''' ----------------------------------------------------------------------------------------------------
  661.         <DebuggerStepThrough>
  662.         Public Async Function FileExistsAsync(ByVal vm As VMWareVirtualMachine, ByVal filepath As String) As Task(Of Boolean)
  663.  
  664.             Dim t As New Task(Of Boolean)(Function() Me.FileExists(vm, filepath))
  665.             t.Start()
  666.             Await t
  667.             Return t.Result
  668.  
  669.         End Function
  670.  
  671.         ''' ----------------------------------------------------------------------------------------------------
  672.         ''' <summary>
  673.         ''' Determine whether a directory exists in the guest operating system of the specified virtual machine.
  674.         ''' </summary>
  675.         ''' ----------------------------------------------------------------------------------------------------
  676.         ''' <param name="vm">
  677.         ''' The VMWare virtual machine.
  678.         ''' </param>
  679.         '''
  680.         ''' <param name="directoryPath">
  681.         ''' The full path to a existing file in the guest operating system.
  682.         ''' </param>
  683.         ''' ----------------------------------------------------------------------------------------------------
  684.         ''' <returns>
  685.         ''' <see langword="True"/> if the directory exists in the guest operating system; otherwise, <see langword="False"/>.
  686.         ''' </returns>
  687.         ''' ----------------------------------------------------------------------------------------------------
  688.         <DebuggerStepThrough>
  689.         Public Function DirectoryExists(ByVal vm As VMWareVirtualMachine, ByVal directoryPath As String) As Boolean
  690.  
  691.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" directoryExistsInGuest ""{2}"" ""{3}""",
  692.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  693.                                                                vm.VmxFile.FullName, directoryPath))
  694.  
  695.             Return stdOut.Equals("The directory exists.", StringComparison.OrdinalIgnoreCase)
  696.  
  697.         End Function
  698.  
  699.         ''' ----------------------------------------------------------------------------------------------------
  700.         ''' <summary>
  701.         ''' Asynchronously determine whether a directory exists in the guest operating system of the specified virtual machine.
  702.         ''' </summary>
  703.         ''' ----------------------------------------------------------------------------------------------------
  704.         ''' <param name="vm">
  705.         ''' The VMWare virtual machine.
  706.         ''' </param>
  707.         '''
  708.         ''' <param name="directoryPath">
  709.         ''' The full path to a existing file in the guest operating system.
  710.         ''' </param>
  711.         ''' ----------------------------------------------------------------------------------------------------
  712.         ''' <returns>
  713.         ''' <see langword="True"/> if the directory exists in the guest operating system; otherwise, <see langword="False"/>.
  714.         ''' </returns>
  715.         ''' ----------------------------------------------------------------------------------------------------
  716.         <DebuggerStepThrough>
  717.         Public Async Function DirectoryExistsAsync(ByVal vm As VMWareVirtualMachine, ByVal directoryPath As String) As Task(Of Boolean)
  718.  
  719.             Dim t As New Task(Of Boolean)(Function() Me.DirectoryExists(vm, directoryPath))
  720.             t.Start()
  721.             Await t
  722.             Return t.Result
  723.  
  724.         End Function
  725.  
  726.         ''' ----------------------------------------------------------------------------------------------------
  727.         ''' <summary>
  728.         ''' Delete a file from the guest operating system of the specified virtual machine.
  729.         ''' </summary>
  730.         ''' ----------------------------------------------------------------------------------------------------
  731.         ''' <param name="vm">
  732.         ''' The VMWare virtual machine.
  733.         ''' </param>
  734.         '''
  735.         ''' <param name="filepath">
  736.         ''' The full path of the file to be deleted in the guest operating system.
  737.         ''' </param>
  738.         ''' ----------------------------------------------------------------------------------------------------
  739.         <DebuggerStepThrough>
  740.         Public Sub FileDelete(ByVal vm As VMWareVirtualMachine, ByVal filepath As String)
  741.  
  742.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" deleteFileInGuest ""{2}"" ""{3}""",
  743.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  744.                                                                vm.VmxFile.FullName, filepath))
  745.  
  746.         End Sub
  747.  
  748.         ''' ----------------------------------------------------------------------------------------------------
  749.         ''' <summary>
  750.         ''' Delete a directory from the guest operating system of the specified virtual machine.
  751.         ''' </summary>
  752.         ''' ----------------------------------------------------------------------------------------------------
  753.         ''' <param name="vm">
  754.         ''' The VMWare virtual machine.
  755.         ''' </param>
  756.         '''
  757.         ''' <param name="directoryPath">
  758.         ''' The full path of the directory to be deleted in the guest operating system.
  759.         ''' </param>
  760.         ''' ----------------------------------------------------------------------------------------------------
  761.         <DebuggerStepThrough>
  762.         Public Sub DirectoryDelete(ByVal vm As VMWareVirtualMachine, ByVal directoryPath As String)
  763.  
  764.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" deleteDirectoryInGuest ""{2}"" ""{3}""",
  765.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  766.                                                                vm.VmxFile.FullName, directoryPath))
  767.  
  768.         End Sub
  769.  
  770.         ''' ----------------------------------------------------------------------------------------------------
  771.         ''' <summary>
  772.         ''' Create a directory in the guest operating system of the specified virtual machine.
  773.         ''' </summary>
  774.         ''' ----------------------------------------------------------------------------------------------------
  775.         ''' <param name="vm">
  776.         ''' The VMWare virtual machine.
  777.         ''' </param>
  778.         '''
  779.         ''' <param name="directoryPath">
  780.         ''' The full path of the directory to be created in the guest operating system.
  781.         ''' </param>
  782.         ''' ----------------------------------------------------------------------------------------------------
  783.         <DebuggerStepThrough>
  784.         Public Sub DirectoryCreate(ByVal vm As VMWareVirtualMachine, ByVal directoryPath As String)
  785.  
  786.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" createDirectoryInGuest ""{2}"" ""{3}""",
  787.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  788.                                                                vm.VmxFile.FullName, directoryPath))
  789.  
  790.         End Sub
  791.  
  792.         ''' ----------------------------------------------------------------------------------------------------
  793.         ''' <summary>
  794.         ''' Installs VmWare Tools on the guest operating system of the specified virtual machine.
  795.         ''' </summary>
  796.         ''' ----------------------------------------------------------------------------------------------------
  797.         ''' <param name="vm">
  798.         ''' The VMWare virtual machine.
  799.         ''' </param>
  800.         ''' ----------------------------------------------------------------------------------------------------
  801.         <DebuggerStepThrough>
  802.         Public Sub InstallVmWareTools(ByVal vm As VMWareVirtualMachine)
  803.  
  804.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" installTools ""{0}""",
  805.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  806.                                                                vm.VmxFile.FullName))
  807.  
  808.             vm.Refresh()
  809.  
  810.         End Sub
  811.  
  812.         ''' ----------------------------------------------------------------------------------------------------
  813.         ''' <summary>
  814.         ''' Rename a file in the guest operating system of the specified virtual machine.
  815.         ''' </summary>
  816.         ''' ----------------------------------------------------------------------------------------------------
  817.         ''' <param name="vm">
  818.         ''' The VMWare virtual machine.
  819.         ''' </param>
  820.         '''
  821.         ''' <param name="srcFilepath">
  822.         ''' The full path to a existing file in the guest operating system.
  823.         ''' </param>
  824.         '''
  825.         ''' <param name="dstFilepath">
  826.         ''' The new destination path for the file set in the <paramref name="srcFilepath"/> parameter.
  827.         ''' </param>
  828.         ''' ----------------------------------------------------------------------------------------------------
  829.         <DebuggerStepThrough>
  830.         Public Sub FileRename(ByVal vm As VMWareVirtualMachine, ByVal srcFilepath As String, ByVal dstFilepath As String)
  831.  
  832.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" renameFileInGuest ""{2}"" ""{3}"" ""{4}""",
  833.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  834.                                                                vm.VmxFile.FullName, srcFilepath, dstFilepath))
  835.  
  836.         End Sub
  837.  
  838.         ''' ----------------------------------------------------------------------------------------------------
  839.         ''' <summary>
  840.         ''' Copies a file from the host operating system to the guest operating system of the specified virtual machine.
  841.         ''' </summary>
  842.         ''' ----------------------------------------------------------------------------------------------------
  843.         ''' <param name="vm">
  844.         ''' The VMWare virtual machine.
  845.         ''' </param>
  846.         '''
  847.         ''' <param name="hostFilePath">
  848.         ''' The source file path; the file to be copied from the host operating system.
  849.         ''' </param>
  850.         '''
  851.         ''' <param name="guestFilePath">
  852.         ''' The destination file path; the file being copied into the guest operating system.
  853.         ''' </param>
  854.         ''' ----------------------------------------------------------------------------------------------------
  855.         <DebuggerStepThrough>
  856.         Public Sub FileCopyFromHostToGuest(ByVal vm As VMWareVirtualMachine, ByVal hostFilePath As String, ByVal guestFilePath As String)
  857.  
  858.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" CopyFileFromHostToGuest ""{2}"" ""{3}"" ""{4}""",
  859.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  860.                                                                vm.VmxFile.FullName, hostFilePath, guestFilePath))
  861.  
  862.         End Sub
  863.  
  864.         ''' ----------------------------------------------------------------------------------------------------
  865.         ''' <summary>
  866.         ''' Copies a file from the guest operating system of the specified virtual machine to the host operating system.
  867.         ''' </summary>
  868.         ''' ----------------------------------------------------------------------------------------------------
  869.         ''' <param name="vm">
  870.         ''' The VMWare virtual machine.
  871.         ''' </param>
  872.         '''
  873.         ''' <param name="guestFilePath">
  874.         ''' The source file path; the file to be copied from the guest operating system.
  875.         ''' </param>
  876.         '''
  877.         ''' <param name="hostFilePath">
  878.         ''' The destination file path; the file being copied into the host operating system.
  879.         ''' </param>
  880.         ''' ----------------------------------------------------------------------------------------------------
  881.         <DebuggerStepThrough>
  882.         Public Sub FileCopyFromGuestToHost(ByVal vm As VMWareVirtualMachine, ByVal guestFilePath As String, ByVal hostFilePath As String)
  883.  
  884.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" CopyFileFromGuestToHost ""{2}"" ""{3}"" ""{4}""",
  885.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  886.                                                                vm.VmxFile.FullName, guestFilePath, hostFilePath))
  887.  
  888.         End Sub
  889.  
  890.         ''' ----------------------------------------------------------------------------------------------------
  891.         ''' <summary>
  892.         ''' Capture the screen of the guest operating system of the specified virtual machine
  893.         ''' and save it to a image file (in .PNG format) in the host operating system.
  894.         ''' </summary>
  895.         ''' ----------------------------------------------------------------------------------------------------
  896.         ''' <param name="vm">
  897.         ''' The VMWare virtual machine.
  898.         ''' </param>
  899.         '''
  900.         ''' <param name="hostFilePath">
  901.         ''' The full path to the image file that will be created in the host operating system.
  902.         ''' </param>
  903.         ''' ----------------------------------------------------------------------------------------------------
  904.         <DebuggerStepThrough>
  905.         Public Sub CaptureScreen(ByVal vm As VMWareVirtualMachine, ByVal hostFilePath As String)
  906.  
  907.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" captureScreen ""{2}"" ""{3}""",
  908.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  909.                                                                vm.VmxFile.FullName, hostFilePath))
  910.  
  911.         End Sub
  912.  
  913.         ''' ----------------------------------------------------------------------------------------------------
  914.         ''' <summary>
  915.         ''' Gets the IP address of the guest operating system on the specified virtual machine.
  916.         ''' </summary>
  917.         ''' ----------------------------------------------------------------------------------------------------
  918.         ''' <param name="vm">
  919.         ''' The VMWare virtual machine.
  920.         ''' </param>
  921.         ''' ----------------------------------------------------------------------------------------------------
  922.         ''' <returns>
  923.         ''' The resulting <see cref="IPAddress"/>.
  924.         ''' </returns>
  925.         ''' ----------------------------------------------------------------------------------------------------
  926.         <DebuggerStepThrough>
  927.         Public Function GetIpAddress(ByVal vm As VMWareVirtualMachine) As IPAddress
  928.  
  929.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" getGuestIPAddress ""{2}"" -wait",
  930.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  931.                                                                vm.VmxFile.FullName))
  932.  
  933.             Return IPAddress.Parse(stdOut)
  934.  
  935.         End Function
  936.  
  937.         ''' ----------------------------------------------------------------------------------------------------
  938.         ''' <summary>
  939.         ''' Asynchronously gets the IP address of the guest operating system on the specified virtual machine.
  940.         ''' </summary>
  941.         ''' ----------------------------------------------------------------------------------------------------
  942.         ''' <param name="vm">
  943.         ''' The VMWare virtual machine.
  944.         ''' </param>
  945.         ''' ----------------------------------------------------------------------------------------------------
  946.         ''' <returns>
  947.         ''' The resulting <see cref="IPAddress"/>.
  948.         ''' </returns>
  949.         ''' ----------------------------------------------------------------------------------------------------
  950.         <DebuggerStepThrough>
  951.         Public Async Function GetIpAddressAsync(ByVal vm As VMWareVirtualMachine) As Task(Of IPAddress)
  952.  
  953.             Dim t As New Task(Of IPAddress)(Function() Me.GetIpAddress(vm))
  954.             t.Start()
  955.             Await t
  956.             Return t.Result
  957.  
  958.         End Function
  959.  
  960.         ''' ----------------------------------------------------------------------------------------------------
  961.         ''' <summary>
  962.         ''' Send keystrokes to the guest operating system of the specified virtual machine.
  963.         ''' </summary>
  964.         ''' ----------------------------------------------------------------------------------------------------
  965.         ''' <param name="vm">
  966.         ''' The VMWare virtual machine.
  967.         ''' </param>
  968.         '''
  969.         ''' <param name="keystrokes">
  970.         ''' The keystrokes to send.
  971.         ''' </param>
  972.         ''' ----------------------------------------------------------------------------------------------------
  973.         <DebuggerStepThrough>
  974.         Public Sub SendKeystrokes(ByVal vm As VMWareVirtualMachine, ByVal keystrokes As String)
  975.  
  976.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" typeKeystrokesInGuest ""{2}"" ""{3}""",
  977.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  978.                                                                vm.VmxFile.FullName, keystrokes))
  979.  
  980.         End Sub
  981.  
  982.         ''' ----------------------------------------------------------------------------------------------------
  983.         ''' <summary>
  984.         ''' Enable shared folders features in the guest operating system of the specified virtual machine.
  985.         ''' </summary>
  986.         ''' ----------------------------------------------------------------------------------------------------
  987.         ''' <param name="vm">
  988.         ''' The VMWare virtual machine.
  989.         ''' </param>
  990.         ''' ----------------------------------------------------------------------------------------------------
  991.         <DebuggerStepThrough>
  992.         Public Sub SharedFoldersEnable(ByVal vm As VMWareVirtualMachine)
  993.  
  994.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" enableSharedFolders ""{2}""",
  995.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  996.                                                                vm.VmxFile.FullName))
  997.  
  998.             vm.Refresh()
  999.  
  1000.         End Sub
  1001.  
  1002.         ''' ----------------------------------------------------------------------------------------------------
  1003.         ''' <summary>
  1004.         ''' Enable shared folders features in the guest operating system of the specified virtual machine.
  1005.         ''' </summary>
  1006.         ''' ----------------------------------------------------------------------------------------------------
  1007.         ''' <param name="vm">
  1008.         ''' The VMWare virtual machine.
  1009.         ''' </param>
  1010.         ''' ----------------------------------------------------------------------------------------------------
  1011.         <DebuggerStepThrough>
  1012.         Public Sub SharedFoldersDisable(ByVal vm As VMWareVirtualMachine)
  1013.  
  1014.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" disableSharedFolders ""{2}""",
  1015.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  1016.                                                                vm.VmxFile.FullName))
  1017.  
  1018.             vm.Refresh()
  1019.  
  1020.         End Sub
  1021.  
  1022.         ''' ----------------------------------------------------------------------------------------------------
  1023.         ''' <summary>
  1024.         ''' Adds a new shared folder in the guest operating system of the specified virtual machine.
  1025.         ''' </summary>
  1026.         ''' ----------------------------------------------------------------------------------------------------
  1027.         ''' <param name="vm">
  1028.         ''' The VMWare virtual machine.
  1029.         ''' </param>
  1030.         '''
  1031.         ''' <param name="shareName">
  1032.         ''' The name to assign to the shared folder.
  1033.         ''' </param>
  1034.         '''
  1035.         ''' <param name="hostDirectoryPath">
  1036.         ''' The full path to the directory on the host operating system being shared.
  1037.         ''' </param>
  1038.         ''' ----------------------------------------------------------------------------------------------------
  1039.         <DebuggerStepThrough>
  1040.         Public Sub SharedFolderAdd(ByVal vm As VMWareVirtualMachine, ByVal shareName As String, ByVal hostDirectoryPath As String)
  1041.  
  1042.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" addSharedFolder ""{2}"" ""{3}"" ""{4}""",
  1043.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  1044.                                                                vm.VmxFile.FullName, shareName, hostDirectoryPath))
  1045.  
  1046.             vm.Refresh()
  1047.  
  1048.         End Sub
  1049.  
  1050.         ''' ----------------------------------------------------------------------------------------------------
  1051.         ''' <summary>
  1052.         ''' Adds a new shared folder in the guest operating system of the specified virtual machine.
  1053.         ''' </summary>
  1054.         ''' ----------------------------------------------------------------------------------------------------
  1055.         ''' <param name="vm">
  1056.         ''' The VMWare virtual machine.
  1057.         ''' </param>
  1058.         '''
  1059.         ''' <param name="sharedFolder">
  1060.         ''' The sharedFolder being added.
  1061.         ''' </param>
  1062.         ''' ----------------------------------------------------------------------------------------------------
  1063.         <DebuggerStepThrough>
  1064.         Public Sub SharedFolderAdd(ByVal vm As VMWareVirtualMachine, ByVal sharedFolder As VmSharedFolderInfo)
  1065.  
  1066.             Me.SharedFolderAdd(vm, sharedFolder.Name, sharedFolder.HostDirectory.FullName)
  1067.  
  1068.         End Sub
  1069.  
  1070.         ''' ----------------------------------------------------------------------------------------------------
  1071.         ''' <summary>
  1072.         ''' Adds a new shared folder in the guest operating system of the specified virtual machine.
  1073.         ''' </summary>
  1074.         ''' ----------------------------------------------------------------------------------------------------
  1075.         ''' <param name="vm">
  1076.         ''' The VMWare virtual machine.
  1077.         ''' </param>
  1078.         '''
  1079.         ''' <param name="shareName">
  1080.         ''' The name of the shared folder being removed.
  1081.         ''' </param>
  1082.         ''' ----------------------------------------------------------------------------------------------------
  1083.         <DebuggerStepThrough>
  1084.         Public Sub SharedFolderRemove(ByVal vm As VMWareVirtualMachine, ByVal shareName As String)
  1085.  
  1086.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" removeSharedFolder ""{2}"" ""{3}""",
  1087.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  1088.                                                                vm.VmxFile.FullName, shareName))
  1089.  
  1090.             vm.Refresh()
  1091.  
  1092.         End Sub
  1093.  
  1094.         ''' ----------------------------------------------------------------------------------------------------
  1095.         ''' <summary>
  1096.         ''' Adds a new shared folder in the guest operating system of the specified virtual machine.
  1097.         ''' </summary>
  1098.         ''' ----------------------------------------------------------------------------------------------------
  1099.         ''' <param name="vm">
  1100.         ''' The VMWare virtual machine.
  1101.         ''' </param>
  1102.         '''
  1103.         ''' <param name="sharedFolder">
  1104.         ''' The sharedFolder being removed.
  1105.         ''' </param>
  1106.         ''' ----------------------------------------------------------------------------------------------------
  1107.         <DebuggerStepThrough>
  1108.         Public Sub SharedFolderRemove(ByVal vm As VMWareVirtualMachine, ByVal sharedFolder As VmSharedFolderInfo)
  1109.  
  1110.             Me.SharedFolderRemove(vm, sharedFolder.Name)
  1111.  
  1112.         End Sub
  1113.  
  1114.         ''' ----------------------------------------------------------------------------------------------------
  1115.         ''' <summary>
  1116.         ''' Sets the writable/readonly access of a shared folder in the guest operating system of the specified virtual machine.
  1117.         ''' </summary>
  1118.         ''' ----------------------------------------------------------------------------------------------------
  1119.         ''' <param name="vm">
  1120.         ''' The VMWare virtual machine.
  1121.         ''' </param>
  1122.         '''
  1123.         ''' <param name="shareName">
  1124.         ''' The name of the shared folder.
  1125.         ''' </param>
  1126.         '''
  1127.         ''' <param name="hostDirectoryPath">
  1128.         ''' The full path to the directory that points the shared folder on the host operating system.
  1129.         ''' </param>
  1130.         '''
  1131.         ''' <param name="allowWriteAccess">
  1132.         ''' If <see langword="True"/>, allow write access to the shared folder; otherwise, make the shared folder read-only.
  1133.         ''' </param>
  1134.         ''' ----------------------------------------------------------------------------------------------------
  1135.         <DebuggerStepThrough>
  1136.         Public Sub SharedFolderSetAccess(ByVal vm As VMWareVirtualMachine, ByVal shareName As String, ByVal hostDirectoryPath As String, ByVal allowWriteAccess As Boolean)
  1137.  
  1138.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" setSharedFolderState ""{2}"" ""{3}"" ""{4}"" ""{5}""",
  1139.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  1140.                                                                vm.VmxFile.FullName, shareName, hostDirectoryPath,
  1141.                                                                If(allowWriteAccess, "writable", "readonly")))
  1142.  
  1143.             vm.Refresh()
  1144.  
  1145.         End Sub
  1146.  
  1147.         ''' ----------------------------------------------------------------------------------------------------
  1148.         ''' <summary>
  1149.         ''' Sets the writable/readonly access of a shared folder in the guest operating system of the specified virtual machine.
  1150.         ''' </summary>
  1151.         ''' ----------------------------------------------------------------------------------------------------
  1152.         ''' <param name="vm">
  1153.         ''' The VMWare virtual machine.
  1154.         ''' </param>
  1155.         '''
  1156.         ''' <param name="sharedFolder">
  1157.         ''' The shared folder.
  1158.         ''' </param>
  1159.         '''
  1160.         ''' <param name="allowWriteAccess">
  1161.         ''' If <see langword="True"/>, allow write access to the shared folder; otherwise, make the shared folder read-only.
  1162.         ''' </param>
  1163.         ''' ----------------------------------------------------------------------------------------------------
  1164.         <DebuggerStepThrough>
  1165.         Public Sub SharedFolderSetAccess(ByVal vm As VMWareVirtualMachine, ByVal sharedFolder As VmSharedFolderInfo, ByVal allowWriteAccess As Boolean)
  1166.  
  1167.             Me.SharedFolderSetAccess(vm, sharedFolder.Name, sharedFolder.HostDirectory.FullName, allowWriteAccess)
  1168.  
  1169.         End Sub
  1170.  
  1171.         ''' ----------------------------------------------------------------------------------------------------
  1172.         ''' <summary>
  1173.         ''' Kills a running process from the guest operating system of the specified virtual machine.
  1174.         ''' </summary>
  1175.         ''' ----------------------------------------------------------------------------------------------------
  1176.         ''' <param name="vm">
  1177.         ''' The VMWare virtual machine.
  1178.         ''' </param>
  1179.         '''
  1180.         ''' <param name="pid">
  1181.         ''' The process identifier.
  1182.         ''' </param>
  1183.         ''' ----------------------------------------------------------------------------------------------------
  1184.         <DebuggerStepThrough>
  1185.         Public Sub ProcessKill(ByVal vm As VMWareVirtualMachine, ByVal pid As Integer)
  1186.  
  1187.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" killProcessInGuest ""{2}"" ""{3}""",
  1188.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  1189.                                                                vm.VmxFile.FullName, pid))
  1190.  
  1191.         End Sub
  1192.  
  1193.         ''' ----------------------------------------------------------------------------------------------------
  1194.         ''' <summary>
  1195.         ''' Runs a new process in the guest operating system of the specified virtual machine.
  1196.         ''' </summary>
  1197.         ''' ----------------------------------------------------------------------------------------------------
  1198.         ''' <param name="vm">
  1199.         ''' The VMWare virtual machine.
  1200.         ''' </param>
  1201.         '''
  1202.         ''' <param name="executablePath">
  1203.         ''' The full path to the executable file in the guest operating system.
  1204.         ''' </param>
  1205.         '''
  1206.         ''' <param name="flags">
  1207.         ''' Flags that determine the runtime behavior. You can combine the flags.
  1208.         ''' </param>
  1209.         '''
  1210.         ''' <param name="arguments">
  1211.         ''' The program arguments.
  1212.         ''' </param>
  1213.         ''' ----------------------------------------------------------------------------------------------------
  1214.         <DebuggerStepThrough>
  1215.         Public Sub ProcessRun(ByVal vm As VMWareVirtualMachine, ByVal executablePath As String, ByVal flags As VmRunProgramFlags, Optional ByVal arguments As String = "")
  1216.  
  1217.             Dim flagsFormatted As String = String.Empty
  1218.             If Not flags = VmRunProgramFlags.None Then
  1219.                 flags = (flags And Not VmRunProgramFlags.None)
  1220.                 flagsFormatted = flags.ToString().Replace(", ", " -").Insert(0, "-"c)
  1221.             End If
  1222.  
  1223.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" runProgramInGuest ""{2}"" {3} ""{4}"" ""{5}""",
  1224.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  1225.                                                                vm.VmxFile.FullName,
  1226.                                                                flagsFormatted, executablePath, arguments))
  1227.  
  1228.         End Sub
  1229.  
  1230.         ''' ----------------------------------------------------------------------------------------------------
  1231.         ''' <summary>
  1232.         ''' Runs a script in the guest operating system of the specified virtual machine.
  1233.         ''' </summary>
  1234.         ''' ----------------------------------------------------------------------------------------------------
  1235.         ''' <param name="vm">
  1236.         ''' The VMWare virtual machine.
  1237.         ''' </param>
  1238.         '''
  1239.         ''' <param name="interpreterPath">
  1240.         ''' The full path to the interpreter executable file in the guest operating system. (eg. "C:\Windows\System32\cscript.exe")
  1241.         ''' </param>
  1242.         '''
  1243.         ''' <param name="flags">
  1244.         ''' Flags that determine the runtime behavior. You can combine the flags.
  1245.         ''' </param>
  1246.         '''
  1247.         ''' <param name="scriptContent">
  1248.         ''' The text content of the script to run.
  1249.         ''' </param>
  1250.         ''' ----------------------------------------------------------------------------------------------------
  1251.         <DebuggerStepThrough>
  1252.         Public Function ProcessRunScript(ByVal vm As VMWareVirtualMachine, ByVal interpreterPath As String, ByVal flags As VmRunProgramFlags, ByVal scriptContent As String) As String
  1253.  
  1254.             Dim flagsFormatted As String = String.Empty
  1255.             If Not flags = VmRunProgramFlags.None Then
  1256.                 flags = (flags And Not VmRunProgramFlags.None)
  1257.                 flagsFormatted = flags.ToString().Replace(", ", " -").Insert(0, "-"c)
  1258.             End If
  1259.  
  1260.             Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" runScriptInGuest ""{2}"" {3} ""{4}"" ""{5}""",
  1261.                                                                vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  1262.                                                                vm.VmxFile.FullName,
  1263.                                                                flagsFormatted, interpreterPath, scriptContent))
  1264.  
  1265.             Return stdOut
  1266.  
  1267.         End Function
  1268.  
  1269.         ''' ----------------------------------------------------------------------------------------------------
  1270.         ''' <summary>
  1271.         ''' Asynchronously runs a script in the guest operating system of the specified virtual machine.
  1272.         ''' </summary>
  1273.         ''' ----------------------------------------------------------------------------------------------------
  1274.         ''' <param name="vm">
  1275.         ''' The VMWare virtual machine.
  1276.         ''' </param>
  1277.         '''
  1278.         ''' <param name="interpreterPath">
  1279.         ''' The full path to the interpreter executable file in the guest operating system. (eg. "C:\Windows\System32\cscript.exe")
  1280.         ''' </param>
  1281.         '''
  1282.         ''' <param name="flags">
  1283.         ''' Flags that determine the runtime behavior. You can combine the flags.
  1284.         ''' </param>
  1285.         '''
  1286.         ''' <param name="scriptContent">
  1287.         ''' The text content of the script to run.
  1288.         ''' </param>
  1289.         ''' ----------------------------------------------------------------------------------------------------
  1290.         <DebuggerStepThrough>
  1291.         Public Async Function ProcessRunScriptAsync(ByVal vm As VMWareVirtualMachine, ByVal interpreterPath As String, ByVal flags As VmRunProgramFlags, ByVal scriptContent As String) As Task(Of String)
  1292.  
  1293.             Dim t As New Task(Of String)(Function() Me.ProcessRunScript(vm, interpreterPath, flags, scriptContent))
  1294.             t.Start()
  1295.             Await t
  1296.             Return t.Result
  1297.  
  1298.         End Function
  1299.  
  1300. #End Region
  1301.  
  1302. #Region " Private Methods "
  1303.  
  1304.         ''' ----------------------------------------------------------------------------------------------------
  1305.         ''' <summary>
  1306.         ''' Runs vmrun.exe process with the specified arguments and returns the standard output.
  1307.         ''' </summary>
  1308.         ''' ----------------------------------------------------------------------------------------------------
  1309.         ''' <param name="args">
  1310.         ''' The vmrun.exe process arguments.
  1311.         ''' </param>
  1312.         ''' ----------------------------------------------------------------------------------------------------
  1313.         ''' <returns>
  1314.         ''' The standard output of vmrun.exe process.
  1315.         ''' </returns>
  1316.         ''' ----------------------------------------------------------------------------------------------------
  1317.         <DebuggerStepThrough>
  1318.         Private Function RunProcess(ByVal args As String) As String
  1319.             Dim stdOut As String
  1320.             Dim exitCode As Integer
  1321.  
  1322.             SyncLock Me.Process
  1323.                 Me.Process.StartInfo.Arguments = args
  1324.                 Me.Process.Start()
  1325.                 Me.Process.WaitForExit(Timeout.Infinite)
  1326.  
  1327.                 stdOut = Me.Process.StandardOutput.ReadToEnd().TrimEnd()
  1328.                 exitCode = Me.Process.ExitCode
  1329.             End SyncLock
  1330.  
  1331.             If stdOut.StartsWith("Error:", StringComparison.OrdinalIgnoreCase) Then
  1332.                 Throw New VmRunException(stdOut, exitCode)
  1333.             End If
  1334.  
  1335.             Return stdOut
  1336.         End Function
  1337.  
  1338. #End Region
  1339.  
  1340. #Region " IDisposable Implementation "
  1341.  
  1342.         ''' ----------------------------------------------------------------------------------------------------
  1343.         ''' <summary>
  1344.         ''' Flag to detect redundant calls when disposing.
  1345.         ''' </summary>
  1346.         ''' ----------------------------------------------------------------------------------------------------
  1347.         Private isDisposed As Boolean = False
  1348.  
  1349.         ''' ----------------------------------------------------------------------------------------------------
  1350.         ''' <summary>
  1351.         ''' Releases all the resources used by this instance.
  1352.         ''' </summary>
  1353.         ''' ----------------------------------------------------------------------------------------------------
  1354.         <DebuggerStepThrough>
  1355.         Public Sub Dispose() Implements IDisposable.Dispose
  1356.             Me.Dispose(isDisposing:=True)
  1357.             GC.SuppressFinalize(obj:=Me)
  1358.         End Sub
  1359.  
  1360.         ''' ----------------------------------------------------------------------------------------------------
  1361.         ''' <summary>
  1362.         ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  1363.         ''' </summary>
  1364.         ''' ----------------------------------------------------------------------------------------------------
  1365.         ''' <param name="isDisposing">
  1366.         ''' <see langword="True"/>  to release both managed and unmanaged resources;
  1367.         ''' <see langword="False"/> to release only unmanaged resources.
  1368.         ''' </param>
  1369.         ''' ----------------------------------------------------------------------------------------------------
  1370.         <DebuggerStepThrough>
  1371.         Private Sub Dispose(ByVal isDisposing As Boolean)
  1372.  
  1373.             If (Not Me.isDisposed) AndAlso (isDisposing) Then
  1374.  
  1375.                 If (Me.Process IsNot Nothing) Then
  1376.  
  1377.                     'Try
  1378.                     '    Me.Process.Kill()
  1379.                     '
  1380.                     'Catch ex As Exception
  1381.                     '
  1382.                     'End Try
  1383.  
  1384.                     Me.Process.Dispose()
  1385.                 End If
  1386.  
  1387.             End If
  1388.  
  1389.             Me.isDisposed = True
  1390.  
  1391.         End Sub
  1392.  
  1393. #End Region
  1394.  
  1395.     End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement