public void FileTransferFromGuest(string guestFilePath, string localFilePath) { //throw new NotImplementedException(); FileTransferInformation fti = _vimService.InitiateFileTransferFromGuest( _morFileManager, //VimLib.VimServiceReference.ManagedObjectReference _this, _morVM, //VimLib.VimServiceReference.ManagedObjectReference vm, _NamePasswordAuthentication, //VimLib.VimServiceReference.GuestAuthentication auth, guestFilePath ); WebClient webClient = new WebClient(); webClient.DownloadFile(fti.url, localFilePath); }
//WIP for SSO auth /*private static byte[] GetSSPIToken(string packageName) * { * ClientCurrentCredential clientCred = null; * ClientContext client = null; * * ServerCurrentCredential serverCred = null; * ServerContext server = null; * * byte[] clientToken; * byte[] serverToken; * * SecurityStatus clientStatus; * * try * { * clientCred = new ClientCurrentCredential(packageName); * serverCred = new ServerCurrentCredential(packageName); * * Console.Out.WriteLine(clientCred.PrincipleName); * * client = new ClientContext( * clientCred, * serverCred.PrincipleName, ContextAttrib.Zero * ); * * server = new ServerContext( * serverCred, ContextAttrib.Zero * ); * * clientToken = null; * serverToken = null; * * clientStatus = client.Init(serverToken, out clientToken); * * * } * finally * { * if (server != null) * { * server.Dispose(); * } * * if (client != null) * { * client.Dispose(); * } * * if (clientCred != null) * { * clientCred.Dispose(); * } * * if (serverCred != null) * { * serverCred.Dispose(); * } * } * return clientToken; * }*/ static void ExecuteCommand(GuestAuthentication creds, string arguments, string programPath, string workingDirectory, bool output) { try { ManagedObjectReference processManager = GetProperty <ManagedObjectReference>(serviceContent.guestOperationsManager, "processManager"); var guestProgramSpec = new GuestProgramSpec() { arguments = arguments, programPath = programPath, workingDirectory = workingDirectory, }; if (output) { //Set file to receive output var outfile = Path.GetRandomFileName(); guestProgramSpec.arguments += @" > C:\Users\Public\" + outfile + @" 2>&1"; //Start the program and receive the PID back Log("[x] Attempting to run cmd with the following arguments: " + guestProgramSpec.arguments); Log(@"[x] Temporarily saving out to C:\Users\Public\" + outfile); long pid = vim.StartProgramInGuest(processManager, vm, creds, guestProgramSpec); //Display PID Log("[x] Process started with PID " + pid + " waiting for execution to finish"); bool finished = false; while (!finished) { //Get status of our process long[] pids = { pid }; GuestProcessInfo[] guestProcessInfo = vim.ListProcessesInGuest(processManager, vm, creds, pids); if (guestProcessInfo.Length == 0) { Log("Error retrieving status of the process, check for the existance of the output file manually"); } if (guestProcessInfo[0].exitCodeSpecified) { Log("[x] Execution finished, attempting to retrieve the results"); //Get the results var fileTransferInformation = vim.InitiateFileTransferFromGuest(guestFileManager, vm, creds, @"C:\Users\Public\" + outfile); using (var client = new System.Net.WebClient()) { client.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); var results = client.DownloadString(fileTransferInformation.url); Log("[x] Output: "); Log(results); } //Delete the file vim.DeleteFileInGuest(guestFileManager, vm, creds, @"C:\Users\Public\" + outfile); Log("[x] Output file deleted"); finished = true; } } } else { //Start the program and receive the PID back Log("[x] Attempting to execute with cmd /c the following command: " + guestProgramSpec.arguments); long pid = vim.StartProgramInGuest(processManager, vm, creds, guestProgramSpec); //Display PID Log("[x] Process started with PID" + pid); } } catch (Exception fault) { Error(fault); } }