Tuesday, March 13, 2012

PowerCLI GuestFileManager Methods

With the latest version of vSphere 5 and the vmware-tools, we have access to a whole new suite of guest methods including GuestFileManager methods including: ChangeFileAttributesInGuest, CreateTemporaryDirectoryInGuest, CreateTemporaryFileInGuest, DeleteDirectoryInGuest, DeleteFileInGuest, InitiateFileTransferFromGuest, InitiateFileTransferToGuest, ListFilesInGuest, MakeDirectoryInGuest, MoveDirectoryInGuest, MoveFileInGuest

I was having issues upgrading the vmware-tools in some linux VMs due to the missing /tmp/vmware-root dir in the guest OS. So with the following powerCLI code it is possible to call the MakeDirectoryInGuest method remotely, fixing the vmware-tools upgrade issue. (credit to the powerCLI guru's LucD and Neilse for their help):

$vmname = "vm-01"
$command = "mkdir /tmp/vmware-root"
$GuestCred = "root"
$GuestPass = "rootpass"
Invoke-VMScript -VM $vmname -ScriptText $command -GuestUser $GuestCred -GuestPassword $GuestPass -ScriptType Bash

And if you wanted to iterate over all *nix VMs:

$linuxGuests = "rhel6Guest","rhel6_64Guest","centosGuest",

Get-VM | where {$linuxGuests -contains
$_.ExtensionData.Summary.Guest.GuestId} | %{

Invoke-VMScript -VM $_.name -ScriptText $command -GuestUser
$GuestCred -GuestPassword $GuestPass -ScriptType Bash

Note: This assumes your GuestOS root passwords are all set consistently.

No comments: