In this article we will take a look at how vcsim
can be used to test your PowerShell code. VCSIM
is a vCenter and ESXi API based simulator. As such you can connect a PowerCLI session to it, and run commands just as you would with a real vCenter instance or an ESXi host.
Now, as it’s a simulator, it’s not possible to do everything that you can with a real vCenter, but it can be useful as a target for quickly testing PowerCLI scripts, particularly ones that use the get
verb.
There is a vcsim docker image available, which I will be using for this article. You can launch the container using the following syntax, providing you have docker installed.
$ docker run -d -p 443:443 nimmis/vcsim
Unable to find image 'nimmis/vcsim:latest' locally
latest: Pulling from nimmis/vcsim
7b1a6ab2e44d: Already exists
40e5be356b1b: Pull complete
ae1d6b4b905f: Pull complete
4f4fb700ef54: Pull complete
2a0987ce81a8: Extracting [=====================================> ] 181MB/244MB
31f560ee0603: Download complete
fd78f5e332c8: Download complete
The first time you run this, the Docker image will be pulled down, before the vcsim container is created. Once done you should have a new container, which can be connected to on 0.0.0.0:443:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
499053dba2b0 nimmis/vcsim "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:443->443/tcp hopeful_lewin
With that running we can start to work with it using PowerCLI. On your system with PowerCLI installed, we first need to set the PowerCLI configuration to ignore invalid certificates (as vcsim isn’t using a trusted certificate). To so so, run the following:
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
We should now be able to connect to vcsim
successfully using the connect-viserver
cmdlet:
connect-viserver 172.17.0.2 -user u -password p
Name Port User
---- ---- ----
172.17.0.2 443 u
Once connected, you should be able to run familiar PowerCLI cmdlets, and the vcsim will respond. For example, we can run get-vmhost
to list the ‘hosts’ in the vcsim
inventory:
get-vmhost
Name ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz MemoryUsageGB MemoryTotalGB Version
---- --------------- ---------- ------ ----------- ----------- ------------- ------------- -------
DC0_H0 Connected PoweredOn 2 67 7182 1.371 4.000 6.5.0
DC0_C0_H0 Connected PoweredOn 2 67 7182 1.371 4.000 6.5.0
DC0_C0_H1 Connected PoweredOn 2 67 7182 1.371 4.000 6.5.0
DC0_C0_H2 Connected PoweredOn 2 67 7182 1.371 4.000 6.5.0
Or get-vm
:
get-vm
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
DC0_H0_VM0 PoweredOn 1 0.031
DC0_H0_VM1 PoweredOn 1 0.031
DC0_C0_RP0_VM0 PoweredOn 1 0.031
DC0_C0_RP0_VM1 PoweredOn 1 0.031
Summary
In this short article you have seen how you can use vcsim, running it a Docker container, as a target for running PowerCLI commands. Not quite a substitute for a real vCenter or ESXi host, but useful nonetheless. Check out the documentation for the project here.