Latest posts by Dan Franciscus (see all)
- SolarWinds Server Performance and Configuration Bundle - Tue, Jun 18 2019
- SolarWinds Patch Manager: Updating Windows and third-party software - Tue, Apr 30 2019
- Monitor file changes in Windows with PowerShell and pswatch - Fri, Feb 1 2019
VMware has long supported the use of PowerShell for managing vSphere via PowerCLI. As a VMware Certified Professional (VCP) and PowerShell enthusiast, I have found this to be a great method for managing my own vSphere environments as well. In terms of remoting into my VMware virtual machines (VMs), I typically log in to them the same way I log in to other physical servers via PowerShell remoting.
With that said, there have been instances when I lose network connectivity to a VM. For those cases, Invoke-VMScript, a cmdlet from the PowerCLI module, is a great tool to use. As long as port 902 on the ESXi host is open and the VM has VMware tools installed, you can access it.
How does it work? ^
According to a VMware technical note, here is the general process of how Invoke-VMScript works:
- Detects guest info data for the VM
- Determines guest operating system and ensures VM tools is running
- Runs PowerShell or Bash based on the operating system
- Waits for VM tools to confirm the VM is powered on
- Authenticates with the VM with the credentials provided
- Creates a temporary file on the VM to store the script's output
- Selects a script interpreter (cmd or Bash)
- Runs the script interpreter as a process on the VM
- Copies the temporary file to the local computer
- Stores the contents of the temporary file in a variable
- Deletes the temporary file locally and on the VM
- Returns output to your local shell
Testing it out ^
So to illustrate the lack of dependency on network connectivity to the VM, I will do an experiment. I will disconnect the network interface controller (NIC) from the VM and then attempt to run a remote script. Here, I have a VM called "Test-VM." As you can see, I can ping it from my local host:
Now I will disconnect the NIC on the VM via PowerCLI:
C:\> Get-VM -Name Test-VM | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$False -Confirm:$False
Name Type NetworkName MacAddress WakeOnLan
---- ---- ----------- ---------- ---------
Network adapter 2 e1000 Server-Te... 00:50:56:8d:1e:43 True
After disconnecting my NIC, I can no longer ping it from my local host:
Here, I will use Invoke-VMScript to connect to the VM and run a simple command such as Get-Process. Note that first I will use Get-Credential to store my credentials used to authenticate to the guest VM that I then pass to Invoke-VMScript. I have also already authenticated to my vCenter server.
Our experiment worked, proving we do not need VM network access to execute PowerShell remotely on our VMware virtual machine. There are many use cases for using Invoke-VMScript, such as configuring a new server via PowerCLI. In addition, the ability to run both PowerShell and Bash on remote VMs is really convenient and can make an admin's life much simpler.