2017-09-21 3 views
0

Ich versuche eine Datei von einem externen Client an eine VM auf einem ESXi Server zu senden. Ich verwende das VMware SDK VMWare.Vim.dll. Wenn ich versuche, die von der FileTransfer-Anfrage angegebene URL zu verwenden, schlägt sie fehl. Code:C# Datei an VM Directory mit VMware SDK senden InitiateFileTransferToGuest Protokollfehler -> ResponseStream Fehler 22 <Der Dateiname ist nicht gültig>

public static string ServerIP 
{ 
    get { return ConfigurationManager.AppSettings["ServerIP"].ToString(); } 
} 

private string ServiceUrl = "https://" + ServerIP + "/sdk"; 
private string UserName = "ESXIUser"; 
ServiceContent sc = new ServiceContent(); 
VimClient client = new VimClientImpl(); 

public VmOps() 
{ 
} 

public void Connect(string password) 
{ 
    // connect to vSphere web service 
    sc = client.Connect(ServiceUrl); 
    // Login using username/password credentials 
    client.Login(UserName, password); 
} 

public void copyDatastoreFile(/*string VMName, string Folder, string sourceFilePath*/) 
{ 
    string hostpass = "ESXIUserPAssword"; 
    string VMName = "VMname"; 
    string Folder = "Test"; 
    string FileName = "test.zip"; 
    //File destination info 
    string BasePath = "D:"; 
    String targetDir = BasePath + "/" + Folder; 
    string srcPath = BasePath + "/" + Folder + "/" + FileName; 

    //Connect to the ESXi 
    Connect(hostpass); 
    NameValueCollection nvcFilter = new NameValueCollection(); 
    nvcFilter.Add("Name", VMName); 
    var vm = (VirtualMachine)client.FindEntityView(typeof(VirtualMachine), null, nvcFilter, null); 

    GuestOperationsManager gom = new GuestOperationsManager(client, sc.GuestOperationsManager); 
    gom.UpdateViewData(); 
    if (gom.FileManager == null) 
    { 
     return; 
    } 
    GuestFileManager gfm = new GuestFileManager(client, gom.FileManager); 

    var Auth = new NamePasswordAuthentication { Password = "VMPassword", Username = "VMUSER", InteractiveSession = false }; 

    bool exists = false; 

    GuestListFileInfo fInfo = gfm.ListFilesInGuest(vm.MoRef, Auth, BasePath, null,null, Folder); 
    exists = fInfo.Files != null; 
    if (!exists) 
    { 
     // Create directory where the files will be copied to 
     gfm.MakeDirectoryInGuest(vm.MoRef, Auth, targetDir, true); 
    } 

    // Copy Virtual Machine file To 
    string URL = gfm.InitiateFileTransferToGuest(vm.MoRef, 
     DSSAuth, 
     targetDir + "/" + FileName, 
     new GuestFileAttributes(), 
     new FileInfo(srcPath).Length, 
     true); 
    UploadFile(srcPath, URL.Replace("*", ServerIP));    
}   

private void UploadFile(string from, string to) 
{ 
    using (WebClient wc = new WebClient()) 
    { 
     string cookie = ((VimApi_60.VimService)client.VimService).CookieContainer.GetCookies(new Uri(((VimApi_60.VimService)client.VimService).Url))[0].ToString(); 
     wc.Credentials = new NetworkCredential("ESXiUSER", "ESXIUserPAssword"); 
     wc.Headers.Add(HttpRequestHeader.Cookie, cookie); 

     try 
     { 
      wc.UploadFile(to, "PUT", from); 
     } 
     catch (WebException wex) 
     { 
      if (wex.Response != null) 
      { 
       string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();       
      } 
     } 
     wc.Dispose(); 
    } 
    } 

URL wie folgt aussieht: "https://199.199.0.1:443/guestFile?id=22&token=23e2deef-7979-b1a8-75e5-413440d8c1377" Plätzchen Sieht aus wie: "vmware_soap_session =" 5e24eaa312s1245de2gg213456m23b4bd87c8e1ca“

WebException: Der Remote-Server hat einen Fehler zurückgegeben: (500) Internal Server Error

Response: \ n 22 \ n Der Dateiname ist nicht gültig

Jeder, der Erfahrung in der Dateiübertragung mit VMware SDK hat? Danke für Ihre Hilfe!

Antwort

0

btw. es war ein einfacher Fehler, alle Schrägstriche müssen mit einem doppelten Backslash geschaltet werden. Der VMWare SDK kann den Schrägstrich verarbeiten, der Webclient put jedoch nicht.

Beispiel:

string targetDir = BasePath + "\\" + Folder; 

string srcPath = BasePath + *"\\" + Folder + "\\" + FileName; 
Verwandte Themen