vmware / govmomi

Go library for the VMware vSphere API
Apache License 2.0
2.32k stars 913 forks source link

Executing file on windows vm using govc #2446

Closed rahul14sep closed 3 years ago

rahul14sep commented 3 years ago

I want to execute powershell script on window vm using govc, I tried using govc guest.run but it looks to execute file in /bin/bash shell, Is there any way i could tell govc to use windows command prompt?

Command I tried govc guest.run -vm VC-ATM-ICLONE -l='username:password' 'powershell C:\windows_network_refresh.ps1'

and got below error govc: ServerFaultCode: File /bin/bash was not found

Is there any other way I could execute file on windows vm.

github-actions[bot] commented 3 years ago

Howdy 🖐   rahul14sep ! Thank you for your interest in this project. We value your feedback and will respond soon.

If you want to contribute to this project, please make yourself familiar with the CONTRIBUTION guidelines.

dougm commented 3 years ago

What does the following report for your vm?

% govc object.collect vm/VC-ATM-ICLONE guest.guestFamily

The govc code uses cmd.exe when windowsGuest, otherwise bash: https://github.com/vmware/govmomi/blob/13851af3d26288ec5760970b5b3ae3fd298d014d/guest/toolbox/client.go#L120-L133

rahul14sep commented 3 years ago

govc object.collect vm/VC-ATM-ICLONE guest Output: guest.toolsStatus types.VirtualMachineToolsStatus toolsOk guest.toolsVersionStatus string guestToolsCurrent guest.toolsVersionStatus2 string guestToolsSupportedNew guest.toolsRunningStatus string guestToolsRunning guest.toolsVersion string 11269 guest.toolsInstallType string guestToolsTypeMSI guest.guestId string
guest.guestFamily string otherGuestFamily guest.guestFullName string Windows Server 2019, 64-bit (Build 17763)

Its not populating correct information for guest family because we are using ESX 6.7 which does not support win2k19 OS.

For such cases can we have optional parameter in guest.run to override shell path , where we could specify which shell to use (shell="c:\Windows\System32\cmd.exe" or shell="/bin/bash") something like that .

Like we have option in pyvmomi ps = vim.vm.guest.ProcessManager.ProgramSpec(programPath=r'C:\Windows\System32\cmd.exe', arguments='/c powershell C:\windows_network_refresh.ps1')

dougm commented 3 years ago

Ah ok. With #2451 you can use the same ProgramSpec in your example or the new guest.run -w flag to set guestFamily to Windows, which makes cmd.exe /c the default way to run the program

rahul14sep commented 3 years ago

This will help. Thanks @dougm for adding this support.

embano1 commented 3 years ago

@rahul14sep just note that Doug created an even better fix for this issue using auto-detection, that is not requiring any flags :)