fletch00
Enthusiast
Enthusiast

if GuestOS($vm) ~= linux mkdir $vm:/tmp/xyz

Jump to solution

Is it possible via Guest OS hooks to create the dir /tmp/xyz in all the linux OS VMs (RedHat, Centos etc) ?

What would the powercli script look like?

thanks

VCP5 VSP5 VTSP5 vExpert http://vmadmin.info
0 Kudos
1 Solution

Accepted Solutions
nielse
Expert
Expert

You can pass the parameter with "--guestpassword=mypass".

By the way, as Luc said, you should be able to do this by using Invoke-VMscript. This isn't really limited to CentOS and stuff. All you need is the root login and the password.

$vmname = "test"

$command = "mkdir /tmp/xyz"

$GuestCred = "root"

$GuestPass = "mycoolpass"

Invoke-VMScript -VM $vmname -ScriptText $command -GuestUser $GuestCred -GuestPassword $GuestPass -ScriptType Bash

I have been using this on Ubuntu, Debian and Red Hat without any problems.

@nielsengelen - http://foonet.be - VCP4/5

View solution in original post

0 Kudos
17 Replies
CRad14
Hot Shot
Hot Shot

To my knowledge...No.. atleast not through the standard powercli commands. Most of them don't have a way to affect the guest os in such a way, except maybe networkadapters..

Invoke-VMScript will get you into the guest os to run script blocks....so that might be useful, but it would still be linux commands just inside of the Invoke-VMScript -scriptblock

http://www.vmware.com/support/developer/PowerCLI/PowerCLI41/html/Invoke-VMScript.html

Someone else maybe have a different idea though

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos
LucD
Leadership
Leadership

Note that Invoke-VMScript is only supported on Redhat Enterprise 5, so no Centos or any other *nix flavours I'm afraid.


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

0 Kudos
fletch00
Enthusiast
Enthusiast

The thing that got me excited was Steve Jin's posts about new Guest OS hooks:

http://www.doublecloud.org/2012/03/run-kill-and-list-programs-in-guest-operating-system-on-vmware/

Maybe it can be done via Java but not PowerCLI?

thanks for the feedback

VCP5 VSP5 VTSP5 vExpert http://vmadmin.info
0 Kudos
LucD
Leadership
Leadership

In fact William already wrote a post on the GuestOperationsManager last year.

And he provided a Perl script to access the methods.

In PowerCLI the framework for all the GuestOperationsManager methods and properties is present as well.

And you can call the methods from a PowerCLI script.

And yes, you are right, provided you're on API 5, you could use this interface to create your folder.

As a matter of fact I'm preparing a post on using these methods from PowerCLI.

Should be posted soon (I hope).


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

0 Kudos
fletch00
Enthusiast
Enthusiast

Ok, thanks for reminding me of William's post

I can successfully do a mkdir via his perl wrapper:

./guestOpsManagement.pl --server vcenter50-1 --username root --vm vMA5  --operation mkdir --guestusername root --filepath_src  /tmp/virtuallyghetto

But what would the logic look like to iterate over all the linux VMs?

And are the vCenter and guestOS credentials able to be cached?

thanks

VCP5 VSP5 VTSP5 vExpert http://vmadmin.info
0 Kudos
LucD
Leadership
Leadership

You can use the VirtualMachineGuestOsIdentifier enumeration to find all the *nix guest identifiers.

And then check your VMs against these.

$linuxGuests = "rhel6Guest","rhel6_64Guest","centosGuest","centos64Guest" 
Get-VM
| where {$linuxGuests -contains $_.ExtensionData.Summary.Guest.GuestId} | %{     # Call the script for Linux guests
}

This just checks against the latest RHEL and CentOS, but you can add other identifiers to the array.


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

0 Kudos
fletch00
Enthusiast
Enthusiast

Ok - I have an issue with the credential handling:

I ran this:

PowerCLI C:\tmp> Get-VM | where {$linuxGuests -contains $_.ExtensionData.Summary
.Guest.GuestId} | %{ ./guestOpsManagement.pl --server vc-02 --username domainadm --vm $_.name --operation mkdir --guestusername root --filepath_src  /tmp/vmware-root }

and got a pop up for each VM (ideally I'd like to have it prompt once and cache for the rest, or alternatively let me specify the passwords on the commandline)

thanks

perl.jpg

VCP5 VSP5 VTSP5 vExpert http://vmadmin.info
0 Kudos
LucD
Leadership
Leadership

You should be able to pass the guest password as well via the guestpassword parameter (provided my Perl knowledge is good enough).


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

0 Kudos
nielse
Expert
Expert

You can pass the parameter with "--guestpassword=mypass".

By the way, as Luc said, you should be able to do this by using Invoke-VMscript. This isn't really limited to CentOS and stuff. All you need is the root login and the password.

$vmname = "test"

$command = "mkdir /tmp/xyz"

$GuestCred = "root"

$GuestPass = "mycoolpass"

Invoke-VMScript -VM $vmname -ScriptText $command -GuestUser $GuestCred -GuestPassword $GuestPass -ScriptType Bash

I have been using this on Ubuntu, Debian and Red Hat without any problems.

@nielsengelen - http://foonet.be - VCP4/5

View solution in original post

0 Kudos
LucD
Leadership
Leadership

Yes, there is obviously a difference between "working" and "supported" :smileygrin:


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

0 Kudos
nielse
Expert
Expert

I know but hey at the moment it still does the trick ;-).

@nielsengelen - http://foonet.be - VCP4/5
0 Kudos
fletch00
Enthusiast
Enthusiast

Niels thank you!

This the simple solution I was looking for!

thanks!

VCP5 VSP5 VTSP5 vExpert http://vmadmin.info
0 Kudos
fletch00
Enthusiast
Enthusiast

Why is the Invoke-VMScript solution not supported?

VCP5 VSP5 VTSP5 vExpert http://vmadmin.info
0 Kudos
fletch00
Enthusiast
Enthusiast

Spoke too soon - that method fails on VMs with out of date vmware tools - and ironically that is the issue I am trying to resolve by creating /tmp/vmware-root !

PowerCLI C:\tmp> Invoke-VMScript -VM healthy-bodies -ScriptText $command -GuestUser $GuestCred -GuestPassword $GuestPass -ScriptType Bash
WARNING: The version of VMware Tools on VM 'vm-01' is out of date and may cause Invoke-VMScript to work improperly.
Invoke-VMScript : 3/7/2012 10:44:06 AM    Invoke-VMScript        The guest operations agent is out of date.
At line:1 char:16
+ Invoke-VMScript <<<<  -VM vm1 -ScriptText $command -GuestUser $GuestCred -GuestPassword $GuestPass -ScriptType Bash
    + CategoryInfo          : NotSpecified: (:) [Invoke-VMScript], ViError
    + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_RunScriptInGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

VCP5 VSP5 VTSP5 vExpert http://vmadmin.info
0 Kudos
nielse
Expert
Expert

I am afraid that you will have to fall back towards the perl script to resolve this issue.

@nielsengelen - http://foonet.be - VCP4/5
0 Kudos
fletch00
Enthusiast
Enthusiast

Curious: when we are passing the guest OS credentials (like root in the examples above) - is this communication over 443 (encrypted)?

thanks

VCP5 VSP5 VTSP5 vExpert http://vmadmin.info
0 Kudos
nielse
Expert
Expert

If you connect with your vcenter with the HTTPS protocol this will be via port 443.

Connect-VIServer -Protocol HTTPS -server vcenter

For more information check out the Connect-VIServer cmdlet.

@nielsengelen - http://foonet.be - VCP4/5
0 Kudos