VMware Cloud Community
Fang001
Contributor
Contributor

[PowerCLI 6.5] How to use GuestFileManager in C#?

Hi all,

     I would like to ask how to create a directory on the guest VM by using GuestFileManager?

     Could someone provide example code? Because I always get exception when I call MakeDirectoryInGuest()

     Many thanks.

    

    _service = _client.Connect(_hostName);

    NameValueCollection nvcFilter = new NameValueCollection();

    nvcFilter.Add("Name", "W81x64");

    _vm = (VirtualMachine)_client.FindEntityView(typeof(VirtualMachine), null, nvcFilter, null);


     string DirectoryPath = @"C:\TestFolder\";

     NamePasswordAuthentication auth = new NamePasswordAuthentication();

     auth.Password = "admin";

     auth.Username = "password";

     auth.InteractiveSession = false;

     GuestFileManager guestfilemgr = new GuestFileManager(VimClient, _service.FileManager);

     guestfilemgr.MakeDirectoryInGuest(_vm.Runtime.Host,auth, DirectoryPath, true);

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership

In PowerCLI it works like this

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

$si = Get-View ServiceInstance

$opMgr = Get-View -Id $si.Content.guestOperationsManager

$spec = New-Object VMware.Vim.NamePasswordAuthentication

$spec.UserName = 'domain\user'

$spec.Password = 'password'

$spec.interactiveSession = $false

$fileMgr = Get-View -Id $opMgr.FileManager

$fileMgr.MakeDirectoryInGuest($vm.ExtensionData.MoRef,$spec,'C:\Temp\Test',$false)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos