Advertisement
Guest User

Untitled

a guest
May 8th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 35.40 KB | None | 0 0
  1. ''' <summary>
  2. ''' A wrapper of VmWare's vmrun.exe application.
  3. ''' </summary>
  4. <ImmutableObject(True)>
  5. Public NotInheritable Class VmRunWrapper : Implements IDisposable
  6.  
  7. #Region " Properties "
  8.  
  9.     ''' <summary>
  10.     ''' Gets the <c>VmRun.exe</c> file path.
  11.     ''' </summary>
  12.     Public ReadOnly Property FilePath As String
  13.  
  14.     ''' <summary>
  15.     ''' Gets a value indicating whether the <c>VmRun.exe</c> file Exists.
  16.     ''' </summary>
  17.     Public ReadOnly Property Exists As Boolean
  18.         Get
  19.             Return File.Exists(Me.FilePath)
  20.         End Get
  21.     End Property
  22.  
  23.     ''' <summary>
  24.     ''' Gets the <c>VmRun.exe</c> <see cref="Diagnostics.Process"/> instance.
  25.     ''' </summary>
  26.     Public ReadOnly Property Process As Process
  27.  
  28. #End Region
  29.  
  30. #Region " Constructors "
  31.  
  32.     ''' <summary>
  33.     ''' Prevents a default instance of the <see cref="VmRunWrapper"/> class from being created.
  34.     ''' </summary>
  35.     Private Sub New()
  36.     End Sub
  37.  
  38.     ''' <summary>
  39.     ''' Initializes a new instance of the <see cref="VmRunWrapper"/> class.
  40.     ''' </summary>
  41.     ''' <param name="filepath">
  42.     ''' The <c>VmRun.exe</c> filepath.
  43.     ''' </param>
  44.     <DebuggerStepThrough>
  45.     Public Sub New(ByVal filepath As String)
  46.  
  47.         Me.FilePath = filepath
  48.  
  49.         Me.Process =
  50.             New Process With {.StartInfo =
  51.                 New ProcessStartInfo With {
  52.                     .FileName = filepath,
  53.                     .CreateNoWindow = True,
  54.                     .UseShellExecute = False,
  55.                     .RedirectStandardError = False,
  56.                     .RedirectStandardOutput = True
  57.                 }
  58.             }
  59.  
  60.     End Sub
  61.  
  62. #End Region
  63.  
  64. #Region " Public Methods "
  65.  
  66.     ''' <summary>
  67.     ''' Gets the amount of virtual machines that are running on the host operating system.
  68.     ''' </summary>
  69.     ''' <returns>
  70.     ''' The amount of virtual machines that are running on the host operating system.
  71.     ''' </returns>
  72.     <DebuggerStepThrough>
  73.     Public Function GetRunningVmCount() As Integer
  74.         Dim stdOut As String = Me.RunProcess("-T ws list")
  75.         Dim line As String = stdOut.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).First()
  76.         Dim count As Integer = CInt(line.Replace("Total running VMs: ", ""))
  77.  
  78.         Return count
  79.     End Function
  80.  
  81.     ''' <summary>
  82.     ''' Gets the file paths of the virtual machines that are running on the host operating system.
  83.     ''' </summary>
  84.     ''' <returns>
  85.     ''' The file paths of the virtual machines that are running on the host operating system.
  86.     ''' </returns>
  87.     <DebuggerStepThrough>
  88.     Public Function GetRunningVms() As ReadOnlyCollection(Of VMWareVirtualMachine)
  89.  
  90.         Dim vms As New Collection(Of VMWareVirtualMachine)
  91.  
  92.         Dim stdOut As String = Me.RunProcess("-T ws list")
  93.  
  94.         For Each line As String In stdOut.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
  95.             If Not line.StartsWith("Total running VMs:", StringComparison.OrdinalIgnoreCase) Then
  96.                 Dim filepath As String = line
  97.                 Dim vm As New VMWareVirtualMachine(filepath, isSharedVm:=False)
  98.                 vms.Add(vm)
  99.             End If
  100.         Next line
  101.  
  102.         Return New ReadOnlyCollection(Of VMWareVirtualMachine)(vms)
  103.  
  104.     End Function
  105.  
  106.     ''' <summary>
  107.     ''' Gets the amount of snapshots created in the specified virtual machine.
  108.     ''' </summary>
  109.     ''' <param name="vm">
  110.     ''' The VMWare virtual machine.
  111.     ''' </param>
  112.     ''' <returns>
  113.     ''' The amount of snapshots created.
  114.     ''' </returns>
  115.     <DebuggerStepThrough>
  116.     Public Function GetSnapshotCount(ByVal vm As VMWareVirtualMachine) As Integer
  117.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws listSnapshots ""{0}""", vm.VmxFile.FullName))
  118.         Dim line As String = stdOut.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).First()
  119.         Dim count As Integer = CInt(line.Replace("Total snapshots: ", ""))
  120.  
  121.         Return count
  122.     End Function
  123.  
  124.     ''' <summary>
  125.     ''' Gets the names of the snapshots created in the specified virtual machine.
  126.     ''' </summary>
  127.     ''' <param name="vm">
  128.     ''' The VMWare virtual machine.
  129.     ''' </param>
  130.     ''' <returns>
  131.     ''' The snapshot names.
  132.     ''' </returns>
  133.     <DebuggerStepThrough>
  134.     Public Function GetSnapshotNames(ByVal vm As VMWareVirtualMachine) As ReadOnlyCollection(Of String)
  135.  
  136.         Dim names As New Collection(Of String)
  137.  
  138.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws listSnapshots ""{0}""", vm.VmxFile.FullName))
  139.  
  140.         For Each line As String In stdOut.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
  141.             If Not line.StartsWith("Total snapshots:", StringComparison.OrdinalIgnoreCase) Then
  142.                 Dim name As String = line
  143.                 names.add(name)
  144.             End If
  145.         Next line
  146.  
  147.         Return New ReadOnlyCollection(Of String)(names)
  148.  
  149.     End Function
  150.  
  151.     ''' <summary>
  152.     ''' Gets a tree-view of the snapshots created in the specified virtual machine.
  153.     ''' </summary>
  154.     ''' <param name="vm">
  155.     ''' The VMWare virtual machine.
  156.     ''' </param>
  157.     ''' <returns>
  158.     ''' A tree-view of the snapshots.
  159.     ''' </returns>
  160.     <DebuggerStepThrough>
  161.     Public Function GetSnapshotsTreeview(ByVal vm As VMWareVirtualMachine) As String
  162.  
  163.         Dim sb As New StringBuilder()
  164.  
  165.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws listSnapshots ""{0}"" showTree", vm.VmxFile.FullName))
  166.  
  167.         For Each line As String In stdOut.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
  168.             If Not line.StartsWith("Total snapshots:", StringComparison.OrdinalIgnoreCase) Then
  169.                 sb.AppendLine(line)
  170.             End If
  171.         Next line
  172.  
  173.         Return sb.ToString()
  174.  
  175.     End Function
  176.  
  177.     ''' <summary>
  178.     ''' Gets a value that determine whether VmWare Tools is installed in the specified virtual machine.
  179.     ''' </summary>
  180.     ''' <param name="vm">
  181.     ''' The VMWare virtual machine.
  182.     ''' </param>
  183.     ''' <returns>
  184.     ''' <see langword="True"/> if VmWare Tools is installed in the specified virtual machine; otherwise, <see langword="False"/>.
  185.     ''' </returns>
  186.     <DebuggerStepThrough>
  187.     Public Function IsVmWareToolsInstalled(ByVal vm As VMWareVirtualMachine) As Boolean
  188.  
  189.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws checkToolsState ""{0}""", vm.VmxFile.FullName))
  190.  
  191.         Return stdOut.Equals("installed", StringComparison.OrdinalIgnoreCase) OrElse
  192.                stdOut.Equals("running", StringComparison.OrdinalIgnoreCase)
  193.  
  194.     End Function
  195.  
  196.     ''' <summary>
  197.     ''' Start the specified virtual machine.
  198.     ''' </summary>
  199.     ''' <param name="vm">
  200.     ''' The VMWare virtual machine.
  201.     ''' </param>
  202.     '''
  203.     ''' <param name="noGui">
  204.     ''' A value indicating whether to start the virtual machine with VmWare GUI or not.
  205.     ''' </param>
  206.     <DebuggerStepThrough>
  207.     Public Sub VmStart(ByVal vm As VMWareVirtualMachine, ByVal noGui As Boolean)
  208.  
  209.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws start ""{0}"" {1}", vm.VmxFile.FullName, If(noGui, "nogui", "gui")))
  210.  
  211.         vm.Refresh()
  212.  
  213.     End Sub
  214.  
  215.     ''' <summary>
  216.     ''' Stop the specified virtual machine.
  217.     ''' </summary>
  218.     ''' <param name="vm">
  219.     ''' The VMWare virtual machine.
  220.     ''' </param>
  221.     '''
  222.     ''' <param name="hardStop">
  223.     ''' A value indicating whether to perform a hard or soft stop.
  224.     ''' </param>
  225.     <DebuggerStepThrough>
  226.     Public Sub VmStop(ByVal vm As VMWareVirtualMachine, ByVal hardStop As Boolean)
  227.  
  228.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws stop ""{0}"" {1}", vm.VmxFile.FullName, If(hardStop, "hard", "soft")))
  229.  
  230.         vm.Refresh()
  231.  
  232.     End Sub
  233.  
  234.     ''' <summary>
  235.     ''' Reset the specified virtual machine.
  236.     ''' </summary>
  237.     ''' <param name="vm">
  238.     ''' The VMWare virtual machine.
  239.     ''' </param>
  240.     '''
  241.     ''' <param name="hardReset">
  242.     ''' A value indicating whether to perform a hard or soft reset.
  243.     ''' </param>
  244.     <DebuggerStepThrough>
  245.     Public Sub VmReset(ByVal vm As VMWareVirtualMachine, ByVal hardReset As Boolean)
  246.  
  247.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws reset ""{0}"" {1}", vm.VmxFile.FullName, If(hardReset, "hard", "soft")))
  248.  
  249.         vm.Refresh()
  250.  
  251.     End Sub
  252.  
  253.     ''' <summary>
  254.     ''' Suspend the specified virtual machine.
  255.     ''' </summary>
  256.     ''' <param name="vm">
  257.     ''' The VMWare virtual machine.
  258.     ''' </param>
  259.     '''
  260.     ''' <param name="hardSuspend">
  261.     ''' A value indicating whether to perform a hard or soft suspend.
  262.     ''' </param>
  263.     <DebuggerStepThrough>
  264.     Public Sub VmSuspend(ByVal vm As VMWareVirtualMachine, ByVal hardSuspend As Boolean)
  265.  
  266.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws suspend ""{0}"" {1}", vm.VmxFile.FullName, If(hardSuspend, "hard", "soft")))
  267.  
  268.         vm.Refresh()
  269.  
  270.     End Sub
  271.  
  272.     ''' <summary>
  273.     ''' Pause the specified virtual machine.
  274.     ''' </summary>
  275.     ''' <param name="vm">
  276.     ''' The VMWare virtual machine.
  277.     ''' </param>
  278.     <DebuggerStepThrough>
  279.     Public Sub VmPause(ByVal vm As VMWareVirtualMachine)
  280.  
  281.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws pause ""{0}""", vm.VmxFile.FullName))
  282.  
  283.         vm.Refresh()
  284.  
  285.     End Sub
  286.  
  287.     ''' <summary>
  288.     ''' Resume (unpause) the specified virtual machine.
  289.     ''' </summary>
  290.     ''' <param name="vm">
  291.     ''' The VMWare virtual machine.
  292.     ''' </param>
  293.     <DebuggerStepThrough>
  294.     Public Sub VmResume(ByVal vm As VMWareVirtualMachine)
  295.  
  296.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws unpause ""{0}""", vm.VmxFile.FullName))
  297.  
  298.         vm.Refresh()
  299.  
  300.     End Sub
  301.  
  302.     ''' <summary>
  303.     ''' Deletes the specified virtual machine.
  304.     ''' </summary>
  305.     ''' <param name="vm">
  306.     ''' The VMWare virtual machine.
  307.     ''' </param>
  308.     <DebuggerStepThrough>
  309.     Public Sub VmDelete(ByVal vm As VMWareVirtualMachine)
  310.  
  311.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws deleteVm ""{0}""", vm.VmxFile.FullName))
  312.  
  313.     End Sub
  314.  
  315.     ''' <summary>
  316.     ''' Create a snapshot of the specified virtual machine.
  317.     ''' </summary>
  318.     ''' <param name="vm">
  319.     ''' The VMWare virtual machine.
  320.     ''' </param>
  321.     '''
  322.     ''' <param name="name">
  323.     ''' The snapshot name.
  324.     ''' </param>
  325.     <DebuggerStepThrough>
  326.     Public Sub SnapshotCreate(ByVal vm As VMWareVirtualMachine, ByVal name As String)
  327.  
  328.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws snapshot ""{0}"" ""{1}""", vm.VmxFile.FullName, name))
  329.  
  330.         vm.Refresh()
  331.  
  332.     End Sub
  333.  
  334.     ''' <summary>
  335.     ''' Delete a snapshot from the specified virtual machine.
  336.     ''' </summary>
  337.     ''' <param name="vm">
  338.     ''' The VMWare virtual machine.
  339.     ''' </param>
  340.     '''
  341.     ''' <param name="name">
  342.     ''' The snapshot name.
  343.     ''' </param>
  344.     <DebuggerStepThrough>
  345.     Public Sub SnapshotDelete(ByVal vm As VMWareVirtualMachine, ByVal name As String)
  346.  
  347.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws deleteSnapshot ""{0}"" ""{1}""", vm.VmxFile.FullName, name))
  348.  
  349.         vm.Refresh()
  350.  
  351.     End Sub
  352.  
  353.     ''' <summary>
  354.     ''' Revert the state of the specified virtual machine to the state of the specified snapshot.
  355.     ''' </summary>
  356.     ''' <param name="vm">
  357.     ''' The VMWare virtual machine.
  358.     ''' </param>
  359.     '''
  360.     ''' <param name="name">
  361.     ''' The snapshot name.
  362.     ''' </param>
  363.     <DebuggerStepThrough>
  364.     Public Sub SnapshotRestore(ByVal vm As VMWareVirtualMachine, ByVal name As String)
  365.  
  366.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws revertToSnapshot ""{0}"" ""{1}""", vm.VmxFile.FullName, name))
  367.  
  368.         vm.Refresh()
  369.  
  370.     End Sub
  371.  
  372.     ''' <summary>
  373.     ''' Determine whether a file exists in the guest operating system of the specified virtual machine.
  374.     ''' </summary>
  375.     ''' <param name="vm">
  376.     ''' The VMWare virtual machine.
  377.     ''' </param>
  378.     '''
  379.     ''' <param name="filepath">
  380.     ''' The full path to a existing file in the guest operating system.
  381.     ''' </param>
  382.     ''' <returns>
  383.     ''' <see langword="True"/> if the file exists in the guest operating system; otherwise, <see langword="False"/>.
  384.     ''' </returns>
  385.     <DebuggerStepThrough>
  386.     Public Function FileExists(ByVal vm As VMWareVirtualMachine, ByVal filepath As String) As Boolean
  387.  
  388.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" fileExistsInGuest ""{2}"" ""{3}""",
  389.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  390.                                                            vm.VmxFile.FullName, filepath))
  391.  
  392.         Return stdOut.Equals("The file exists.", StringComparison.OrdinalIgnoreCase)
  393.  
  394.     End Function
  395.  
  396.     ''' <summary>
  397.     ''' Determine whether a directory exists in the guest operating system of the specified virtual machine.
  398.     ''' </summary>
  399.     ''' <param name="vm">
  400.     ''' The VMWare virtual machine.
  401.     ''' </param>
  402.     '''
  403.     ''' <param name="directoryPath">
  404.     ''' The full path to a existing file in the guest operating system.
  405.     ''' </param>
  406.     ''' <returns>
  407.     ''' <see langword="True"/> if the directory exists in the guest operating system; otherwise, <see langword="False"/>.
  408.     ''' </returns>
  409.     <DebuggerStepThrough>
  410.     Public Function DirectoryExists(ByVal vm As VMWareVirtualMachine, ByVal directoryPath As String) As Boolean
  411.  
  412.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" directoryExistsInGuest ""{2}"" ""{3}""",
  413.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  414.                                                            vm.VmxFile.FullName, directoryPath))
  415.  
  416.         Return stdOut.Equals("The directory exists.", StringComparison.OrdinalIgnoreCase)
  417.  
  418.     End Function
  419.  
  420.     ''' <summary>
  421.     ''' Delete a file from the guest operating system of the specified virtual machine.
  422.     ''' </summary>
  423.     ''' <param name="vm">
  424.     ''' The VMWare virtual machine.
  425.     ''' </param>
  426.     '''
  427.     ''' <param name="filepath">
  428.     ''' The full path of the file to be deleted in the guest operating system.
  429.     ''' </param>
  430.     <DebuggerStepThrough>
  431.     Public Sub FileDelete(ByVal vm As VMWareVirtualMachine, ByVal filepath As String)
  432.  
  433.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" deleteFileInGuest ""{2}"" ""{3}""",
  434.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  435.                                                            vm.VmxFile.FullName, filepath))
  436.  
  437.     End Sub
  438.  
  439.     ''' <summary>
  440.     ''' Delete a directory from the guest operating system of the specified virtual machine.
  441.     ''' </summary>
  442.     ''' <param name="vm">
  443.     ''' The VMWare virtual machine.
  444.     ''' </param>
  445.     '''
  446.     ''' <param name="directoryPath">
  447.     ''' The full path of the directory to be deleted in the guest operating system.
  448.     ''' </param>
  449.     <DebuggerStepThrough>
  450.     Public Sub DirectoryDelete(ByVal vm As VMWareVirtualMachine, ByVal directoryPath As String)
  451.  
  452.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" deleteDirectoryInGuest ""{2}"" ""{3}""",
  453.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  454.                                                            vm.VmxFile.FullName, directoryPath))
  455.  
  456.     End Sub
  457.  
  458.     ''' <summary>
  459.     ''' Create a directory in the guest operating system of the specified virtual machine.
  460.     ''' </summary>
  461.     ''' <param name="vm">
  462.     ''' The VMWare virtual machine.
  463.     ''' </param>
  464.     '''
  465.     ''' <param name="directoryPath">
  466.     ''' The full path of the directory to be created in the guest operating system.
  467.     ''' </param>
  468.     <DebuggerStepThrough>
  469.     Public Sub DirectoryCreate(ByVal vm As VMWareVirtualMachine, ByVal directoryPath As String)
  470.  
  471.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" createDirectoryInGuest ""{2}"" ""{3}""",
  472.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  473.                                                            vm.VmxFile.FullName, directoryPath))
  474.  
  475.     End Sub
  476.  
  477.     ''' <summary>
  478.     ''' Installs VmWare Tools on the guest operating system of the specified virtual machine.
  479.     ''' </summary>
  480.     ''' <param name="vm">
  481.     ''' The VMWare virtual machine.
  482.     ''' </param>
  483.     <DebuggerStepThrough>
  484.     Public Sub InstallVmWareTools(ByVal vm As VMWareVirtualMachine)
  485.  
  486.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" installTools ""{0}""",
  487.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  488.                                                            vm.VmxFile.FullName))
  489.  
  490.         vm.Refresh()
  491.  
  492.     End Sub
  493.  
  494.     ''' <summary>
  495.     ''' Rename a file in the guest operating system of the specified virtual machine.
  496.     ''' </summary>
  497.     ''' <param name="vm">
  498.     ''' The VMWare virtual machine.
  499.     ''' </param>
  500.     '''
  501.     ''' <param name="srcFilepath">
  502.     ''' The full path to a existing file in the guest operating system.
  503.     ''' </param>
  504.     '''
  505.     ''' <param name="dstFilepath">
  506.     ''' The new destination path for the file set in the <paramref name="srcFilepath"/> parameter.
  507.     ''' </param>
  508.     <DebuggerStepThrough>
  509.     Public Sub FileRename(ByVal vm As VMWareVirtualMachine, ByVal srcFilepath As String, ByVal dstFilepath As String)
  510.  
  511.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" renameFileInGuest ""{2}"" ""{3}"" ""{4}""",
  512.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  513.                                                            vm.VmxFile.FullName, srcFilepath, dstFilepath))
  514.  
  515.     End Sub
  516.  
  517.     ''' <summary>
  518.     ''' Copies a file from the host operating system to the guest operating system of the specified virtual machine.
  519.     ''' </summary>
  520.     ''' <param name="vm">
  521.     ''' The VMWare virtual machine.
  522.     ''' </param>
  523.     '''
  524.     ''' <param name="hostFilePath">
  525.     ''' The source file path; the file to be copied from the host operating system.
  526.     ''' </param>
  527.     '''
  528.     ''' <param name="guestFilePath">
  529.     ''' The destination file path; the file being copied into the guest operating system.
  530.     ''' </param>
  531.     <DebuggerStepThrough>
  532.     Public Sub FileCopyFromHostToGuest(ByVal vm As VMWareVirtualMachine, ByVal hostFilePath As String, ByVal guestFilePath As String)
  533.  
  534.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" CopyFileFromHostToGuest ""{2}"" ""{3}"" ""{4}""",
  535.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  536.                                                            vm.VmxFile.FullName, hostFilePath, guestFilePath))
  537.  
  538.     End Sub
  539.  
  540.     ''' <summary>
  541.     ''' Copies a file from the guest operating system of the specified virtual machine to the host operating system.
  542.     ''' </summary>
  543.     ''' <param name="vm">
  544.     ''' The VMWare virtual machine.
  545.     ''' </param>
  546.     '''
  547.     ''' <param name="guestFilePath">
  548.     ''' The source file path; the file to be copied from the guest operating system.
  549.     ''' </param>
  550.     '''
  551.     ''' <param name="hostFilePath">
  552.     ''' The destination file path; the file being copied into the host operating system.
  553.     ''' </param>
  554.     <DebuggerStepThrough>
  555.     Public Sub FileCopyFromGuestToHost(ByVal vm As VMWareVirtualMachine, ByVal guestFilePath As String, ByVal hostFilePath As String)
  556.  
  557.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" CopyFileFromGuestToHost ""{2}"" ""{3}"" ""{4}""",
  558.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  559.                                                            vm.VmxFile.FullName, guestFilePath, hostFilePath))
  560.  
  561.     End Sub
  562.  
  563.     ''' <summary>
  564.     ''' Capture the screen of the guest operating system of the specified virtual machine
  565.     ''' and save it to a image file (in .PNG format) in the host operating system.
  566.     ''' </summary>
  567.     ''' <param name="vm">
  568.     ''' The VMWare virtual machine.
  569.     ''' </param>
  570.     '''
  571.     ''' <param name="hostFilePath">
  572.     ''' The full path to the image file that will be created in the host operating system.
  573.     ''' </param>
  574.     <DebuggerStepThrough>
  575.     Public Sub CaptureScreen(ByVal vm As VMWareVirtualMachine, ByVal hostFilePath As String)
  576.  
  577.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" captureScreen ""{2}"" ""{3}""",
  578.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  579.                                                            vm.VmxFile.FullName, hostFilePath))
  580.  
  581.     End Sub
  582.  
  583.     ''' <summary>
  584.     ''' Gets the IP address of the guest operating system on the specified virtual machine.
  585.     ''' </summary>
  586.     ''' <param name="vm">
  587.     ''' The VMWare virtual machine.
  588.     ''' </param>
  589.     ''' <returns>
  590.     ''' The resulting <see cref="IPAddress"/>.
  591.     ''' </returns>
  592.     <DebuggerStepThrough>
  593.     Public Function GetIpAddress(ByVal vm As VMWareVirtualMachine) As IPAddress
  594.  
  595.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" getGuestIPAddress ""{2}"" -wait",
  596.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  597.                                                            vm.VmxFile.FullName))
  598.  
  599.         Return IPAddress.Parse(stdOut)
  600.  
  601.     End Function
  602.  
  603.     ''' <summary>
  604.     ''' Send keystrokes to the guest operating system of the specified virtual machine.
  605.     ''' </summary>
  606.     ''' <param name="vm">
  607.     ''' The VMWare virtual machine.
  608.     ''' </param>
  609.     '''
  610.     ''' <param name="keystrokes">
  611.     ''' The keystrokes to send.
  612.     ''' </param>
  613.     <DebuggerStepThrough>
  614.     Public Sub SendKeystrokes(ByVal vm As VMWareVirtualMachine, ByVal keystrokes As String)
  615.  
  616.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" typeKeystrokesInGuest ""{2}"" ""{3}""",
  617.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  618.                                                            vm.VmxFile.FullName, keystrokes))
  619.  
  620.     End Sub
  621.  
  622.     ''' <summary>
  623.     ''' Enable shared folders features in the guest operating system of the specified virtual machine.
  624.     ''' </summary>
  625.     ''' <param name="vm">
  626.     ''' The VMWare virtual machine.
  627.     ''' </param>
  628.     <DebuggerStepThrough>
  629.     Public Sub SharedFoldersEnable(ByVal vm As VMWareVirtualMachine)
  630.  
  631.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" enableSharedFolders ""{2}""",
  632.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  633.                                                            vm.VmxFile.FullName))
  634.  
  635.         vm.Refresh()
  636.  
  637.     End Sub
  638.  
  639.     ''' <summary>
  640.     ''' Enable shared folders features in the guest operating system of the specified virtual machine.
  641.     ''' </summary>
  642.     ''' <param name="vm">
  643.     ''' The VMWare virtual machine.
  644.     ''' </param>
  645.     <DebuggerStepThrough>
  646.     Public Sub SharedFoldersDisable(ByVal vm As VMWareVirtualMachine)
  647.  
  648.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" disableSharedFolders ""{2}""",
  649.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  650.                                                            vm.VmxFile.FullName))
  651.  
  652.         vm.Refresh()
  653.  
  654.     End Sub
  655.  
  656.     ''' <summary>
  657.     ''' Adds a new shared folder in the guest operating system of the specified virtual machine.
  658.     ''' </summary>
  659.     ''' <param name="vm">
  660.     ''' The VMWare virtual machine.
  661.     ''' </param>
  662.     '''
  663.     ''' <param name="shareName">
  664.     ''' The name to assign to the shared folder.
  665.     ''' </param>
  666.     '''
  667.     ''' <param name="hostDirectoryPath">
  668.     ''' The full path to the directory on the host operating system being shared.
  669.     ''' </param>
  670.     <DebuggerStepThrough>
  671.     Public Sub SharedFolderAdd(ByVal vm As VMWareVirtualMachine, ByVal shareName As String, ByVal hostDirectoryPath As String)
  672.  
  673.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" addSharedFolder ""{2}"" ""{3}"" ""{4}""",
  674.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  675.                                                            vm.VmxFile.FullName, shareName, hostDirectoryPath))
  676.  
  677.         vm.Refresh()
  678.  
  679.     End Sub
  680.  
  681.     ''' <summary>
  682.     ''' Adds a new shared folder in the guest operating system of the specified virtual machine.
  683.     ''' </summary>
  684.     ''' <param name="vm">
  685.     ''' The VMWare virtual machine.
  686.     ''' </param>
  687.     '''
  688.     ''' <param name="sharedFolder">
  689.     ''' The sharedFolder being added.
  690.     ''' </param>
  691.     <DebuggerStepThrough>
  692.     Public Sub SharedFolderAdd(ByVal vm As VMWareVirtualMachine, ByVal sharedFolder As VmSharedFolderInfo)
  693.  
  694.         Me.SharedFolderAdd(vm, sharedFolder.Name, sharedFolder.HostDirectory.FullName)
  695.  
  696.     End Sub
  697.  
  698.     ''' <summary>
  699.     ''' Adds a new shared folder in the guest operating system of the specified virtual machine.
  700.     ''' </summary>
  701.     ''' <param name="vm">
  702.     ''' The VMWare virtual machine.
  703.     ''' </param>
  704.     '''
  705.     ''' <param name="shareName">
  706.     ''' The name of the shared folder being removed.
  707.     ''' </param>
  708.     <DebuggerStepThrough>
  709.     Public Sub SharedFolderRemove(ByVal vm As VMWareVirtualMachine, ByVal shareName As String)
  710.  
  711.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" removeSharedFolder ""{2}"" ""{3}""",
  712.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  713.                                                            vm.VmxFile.FullName, shareName))
  714.  
  715.         vm.Refresh()
  716.  
  717.     End Sub
  718.  
  719.     ''' <summary>
  720.     ''' Adds a new shared folder in the guest operating system of the specified virtual machine.
  721.     ''' </summary>
  722.     ''' <param name="vm">
  723.     ''' The VMWare virtual machine.
  724.     ''' </param>
  725.     '''
  726.     ''' <param name="sharedFolder">
  727.     ''' The sharedFolder being removed.
  728.     ''' </param>
  729.     <DebuggerStepThrough>
  730.     Public Sub SharedFolderRemove(ByVal vm As VMWareVirtualMachine, ByVal sharedFolder As VmSharedFolderInfo)
  731.  
  732.         Me.SharedFolderRemove(vm, sharedFolder.Name)
  733.  
  734.     End Sub
  735.  
  736.     ''' <summary>
  737.     ''' Sets the writable/readonly access of a shared folder in the guest operating system of the specified virtual machine.
  738.     ''' </summary>
  739.     ''' <param name="vm">
  740.     ''' The VMWare virtual machine.
  741.     ''' </param>
  742.     '''
  743.     ''' <param name="shareName">
  744.     ''' The name of the shared folder.
  745.     ''' </param>
  746.     '''
  747.     ''' <param name="hostDirectoryPath">
  748.     ''' The full path to the directory that points the shared folder on the host operating system.
  749.     ''' </param>
  750.     '''
  751.     ''' <param name="allowWriteAccess">
  752.     ''' If <see langword="True"/>, allow write access to the shared folder; otherwise, make the shared folder read-only.
  753.     ''' </param>
  754.     <DebuggerStepThrough>
  755.     Public Sub SharedFolderSetAccess(ByVal vm As VMWareVirtualMachine, ByVal shareName As String, ByVal hostDirectoryPath As String, ByVal allowWriteAccess As Boolean)
  756.  
  757.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" setSharedFolderState ""{2}"" ""{3}"" ""{4}"" ""{5}""",
  758.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  759.                                                            vm.VmxFile.FullName, shareName, hostDirectoryPath,
  760.                                                            If(allowWriteAccess, "writable", "readonly")))
  761.  
  762.         vm.Refresh()
  763.  
  764.     End Sub
  765.  
  766.     ''' <summary>
  767.     ''' Sets the writable/readonly access of a shared folder in the guest operating system of the specified virtual machine.
  768.     ''' </summary>
  769.     ''' <param name="vm">
  770.     ''' The VMWare virtual machine.
  771.     ''' </param>
  772.     '''
  773.     ''' <param name="sharedFolder">
  774.     ''' The shared folder.
  775.     ''' </param>
  776.     '''
  777.     ''' <param name="allowWriteAccess">
  778.     ''' If <see langword="True"/>, allow write access to the shared folder; otherwise, make the shared folder read-only.
  779.     ''' </param>
  780.     <DebuggerStepThrough>
  781.     Public Sub SharedFolderSetAccess(ByVal vm As VMWareVirtualMachine, ByVal sharedFolder As VmSharedFolderInfo, ByVal allowWriteAccess As Boolean)
  782.  
  783.         Me.SharedFolderSetAccess(vm, sharedFolder.Name, sharedFolder.HostDirectory.FullName, allowWriteAccess)
  784.  
  785.     End Sub
  786.  
  787.     ''' <summary>
  788.     ''' Kills a running process from the guest operating system of the specified virtual machine.
  789.     ''' </summary>
  790.     ''' <param name="vm">
  791.     ''' The VMWare virtual machine.
  792.     ''' </param>
  793.     '''
  794.     ''' <param name="pid">
  795.     ''' The process identifier.
  796.     ''' </param>
  797.     <DebuggerStepThrough>
  798.     Public Sub ProcessKill(ByVal vm As VMWareVirtualMachine, ByVal pid As Integer)
  799.  
  800.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" killProcessInGuest ""{2}"" ""{3}""",
  801.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  802.                                                            vm.VmxFile.FullName, pid))
  803.  
  804.     End Sub
  805.  
  806.     ''' <summary>
  807.     ''' Runs a new process in the guest operating system of the specified virtual machine.
  808.     ''' </summary>
  809.     ''' <param name="vm">
  810.     ''' The VMWare virtual machine.
  811.     ''' </param>
  812.     '''
  813.     ''' <param name="executablePath">
  814.     ''' The full path to the executable file in the guest operating system.
  815.     ''' </param>
  816.     '''
  817.     ''' <param name="flags">
  818.     ''' Flags that determine the runtime behavior. You can combine the flags.
  819.     ''' </param>
  820.     '''
  821.     ''' <param name="arguments">
  822.     ''' The program arguments.
  823.     ''' </param>
  824.     <DebuggerStepThrough>
  825.     Public Sub ProcessRun(ByVal vm As VMWareVirtualMachine, ByVal executablePath As String, ByVal flags As VmRunProgramFlags, Optional ByVal arguments As String = "")
  826.  
  827.         Dim flagsFormatted As String = String.Empty
  828.         If Not flags = VmRunProgramFlags.None Then
  829.             flags = (flags And Not VmRunProgramFlags.None)
  830.             flagsFormatted = flags.ToString().Replace(", ", " -").Insert(0, "-"c)
  831.         End If
  832.  
  833.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" runProgramInGuest ""{2}"" {3} ""{4}"" ""{5}""",
  834.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  835.                                                            vm.VmxFile.FullName,
  836.                                                            flagsFormatted, executablePath, arguments))
  837.  
  838.     End Sub
  839.  
  840.     ''' <summary>
  841.     ''' Runs a script in the guest operating system of the specified virtual machine.
  842.     ''' </summary>
  843.     ''' <param name="vm">
  844.     ''' The VMWare virtual machine.
  845.     ''' </param>
  846.     '''
  847.     ''' <param name="interpreterPath">
  848.     ''' The full path to the interpreter executable file in the guest operating system. (eg. "C:\Windows\System32\cscript.exe")
  849.     ''' </param>
  850.     '''
  851.     ''' <param name="flags">
  852.     ''' Flags that determine the runtime behavior. You can combine the flags.
  853.     ''' </param>
  854.     '''
  855.     ''' <param name="scriptContent">
  856.     ''' The text content of the script to run.
  857.     ''' </param>
  858.     <DebuggerStepThrough>
  859.     Public Function ProcessRunScript(ByVal vm As VMWareVirtualMachine, ByVal interpreterPath As String, ByVal flags As VmRunProgramFlags, ByVal scriptContent As String) As String
  860.  
  861.         Dim flagsFormatted As String = String.Empty
  862.         If Not flags = VmRunProgramFlags.None Then
  863.             flags = (flags And Not VmRunProgramFlags.None)
  864.             flagsFormatted = flags.ToString().Replace(", ", " -").Insert(0, "-"c)
  865.         End If
  866.  
  867.         Dim stdOut As String = Me.RunProcess(String.Format("-T ws -gu ""{0}"" -gp ""{1}"" runScriptInGuest ""{2}"" {3} ""{4}"" ""{5}""",
  868.                                                            vm.GuestOsCredential.Username, vm.GuestOsCredential.Password,
  869.                                                            vm.VmxFile.FullName,
  870.                                                            flagsFormatted, interpreterPath, scriptContent))
  871.  
  872.         Return stdOut
  873.  
  874.     End Function
  875.  
  876. #End Region
  877.  
  878. #Region " Private Methods "
  879.  
  880.     ''' <summary>
  881.     ''' Runs vmrun.exe process with the specified arguments and returns the standard output.
  882.     ''' </summary>
  883.     ''' <param name="args">
  884.     ''' The vmrun.exe process arguments.
  885.     ''' </param>
  886.     ''' <returns>
  887.     ''' The standard output of vmrun.exe process.
  888.     ''' </returns>
  889.     <DebuggerStepThrough>
  890.     Private Function RunProcess(ByVal args As String) As String
  891.         Dim stdOut As String
  892.         Dim exitCode As Integer
  893.  
  894.         SyncLock Me.Process
  895.             Me.Process.StartInfo.Arguments = args
  896.             Me.Process.Start()
  897.             Me.Process.WaitForExit(Timeout.Infinite)
  898.  
  899.             stdOut = Me.Process.StandardOutput.ReadToEnd().TrimEnd()
  900.             exitCode = Me.Process.ExitCode
  901.         End SyncLock
  902.  
  903.         If stdOut.StartsWith("Error:", StringComparison.OrdinalIgnoreCase) Then
  904.             Throw New VmRunException(stdOut, exitCode)
  905.         End If
  906.  
  907.         Return stdOut
  908.     End Function
  909.  
  910. #End Region
  911.  
  912. #Region " IDisposable Implementation "
  913.  
  914.     ''' <summary>
  915.     ''' Flag to detect redundant calls when disposing.
  916.     ''' </summary>
  917.     Private isDisposed As Boolean = False
  918.  
  919.     ''' <summary>
  920.     ''' Releases all the resources used by this instance.
  921.     ''' </summary>
  922.     <DebuggerStepThrough>
  923.     Public Sub Dispose() Implements IDisposable.Dispose
  924.         Me.Dispose(isDisposing:=True)
  925.         GC.SuppressFinalize(obj:=Me)
  926.     End Sub
  927.  
  928.     ''' <summary>
  929.     ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  930.     ''' </summary>
  931.     ''' <param name="isDisposing">
  932.     ''' <see langword="True"/>  to release both managed and unmanaged resources;
  933.     ''' <see langword="False"/> to release only unmanaged resources.
  934.     ''' </param>
  935.     <DebuggerStepThrough>
  936.     Private Sub Dispose(ByVal isDisposing As Boolean)
  937.  
  938.         If (Not Me.isDisposed) AndAlso (isDisposing) Then
  939.  
  940.             If (Me.Process IsNot Nothing) Then
  941.  
  942.                 'Try
  943.                 '    Me.Process.Kill()
  944.                 '
  945.                 'Catch ex As Exception
  946.                 '
  947.                 'End Try
  948.  
  949.                 Me.Process.Dispose()
  950.             End If
  951.  
  952.         End If
  953.  
  954.         Me.isDisposed = True
  955.  
  956.     End Sub
  957.  
  958. #End Region
  959.  
  960. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement