VMSysAdmin

When you need another brain

Posts Tagged ‘govc

Getting started with govc on Windows

leave a comment »

govc is a vSphere CLI that provides an interface to vSphere API. I’ve found that most govc guides are using Linux or MacOS, so here are a few points on using govc on Windows 10.

The easiest way to install govc is to get it from Chocolatey PS repository, although it may be a version or two behind the latest. You might want to get it from GitHub instead if being on a current version is important to you. The following commands are run in Powershell.

Install Chocolatey repository:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Install govc and jq (jq is a json processor, which comes handy in parsing values returned by govc):

choco install govc
choco install jq

Optionally, install VMware PowerCLI and adjust PowerCLI variables:

install-module vmware.powercli
Set-ExecutionPolicy RemoteSigned
Set-PowerCLIConfiguration -ParticipateInCEIP $false
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

Create credential store for vCenter authentication:

$vcenter = "<your_vcenter_fqdn>"

$cred = get-credential
New-VICredentialStoreItem -Host $vcenter -User $cred.username -Password $cred.GetNetworkCredential().password

govc uses environment variables to set up the session:

$env:GOVC_URL="https://"+$vcenter
$env:GOVC_USERNAME=(Get-VICredentialStoreItem $vcenter).User
$env:GOVC_PASSWORD=(Get-VICredentialStoreItem $vcenter).Password
$env:GOVC_INSECURE="true"
$env:GOVC_DATASTORE="<datastore_name>"
$env:GOVC_NETWORK="<portgroup_name>"
$env:GOVC_RESOURCE_POOL='*/Resources'

To print your session GOVC variables, you can run the following (note that the password will be in plaintext, no way around it as far as I know):

ls env:GOVC*

Now you should be able to run govc to interface with vCenter API:

govc about
govc about -json | jq '.'

Command reference: https://github.com/vmware/govmomi/blob/master/govc/USAGE.md

Some functionality is not yet implemented. For example, the ability to manipulate vApp properties is still missing.

Some useful commands:

govc import.ova -name ubuntu1804template-orig .\ubuntu-18.04-server-cloudimg-amd64.ova
govc vm.change -vm ubuntu1804template-orig -c 1 -m 1024 -e="disk.enableUUID=1"
govc vm.clone -on=false -vm ubuntu1804template-orig ubuntu1804template-test
govc vm.power -on=true ubuntu1804template-test
govc vm.destroy ubuntu1804template-test

Note that govc is not expecting Windows text file format, and you may see the following error if you try to feed it with a json file edited in notepad on Windows. For example:

govc import.ova -options=C:\ubuntu.json .\Downloads\ubuntu-18.04-server-cloudimg-amd64.ova

C:\ProgramData\chocolatey\lib\govc\tools\x64\govc.exe: invalid character ‘ΓΏ’ looking for beginning of value

You will need to convert your json file to unix format using some tool like dos2unix

.\dos2unix.exe -n C:\ubuntu.json C:\ubuntu2.json

Written by vmsysadmin

September 14, 2019 at 12:19 pm

Posted in vSphere

Tagged with ,