Invoke-VMScript allows you to run PowerShell, batch, and Bash scripts on remote VMware virtual machines from PowerShell. Not only that but since you are actually authenticating to vCenter or ESXi, it does not require network connectivity to the server you want to run the script on.

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:

  1. Detects guest info data for the VM
  2. Determines guest operating system and ensures VM tools is running
  3. Runs PowerShell or Bash based on the operating system
  4. Waits for VM tools to confirm the VM is powered on
  5. Authenticates with the VM with the credentials provided
  6. Creates a temporary file on the VM to store the script's output
  7. Selects a script interpreter (cmd or Bash)
  8. Runs the script interpreter as a process on the VM
  9. Copies the temporary file to the local computer
  10. Stores the contents of the temporary file in a variable
  11. Deletes the temporary file locally and on the VM
  12. 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:

Pinging from local host

Pinging from local host

Now I will disconnect the NIC on the VM via PowerCLI:

After disconnecting my NIC, I can no longer ping it from my local host:

Ping after disconnect from local host

Ping after disconnect from 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.

Running Invoke VMScript

Running Invoke VMScript

Conclusion ^

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.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

2+

Users who have LIKED this post:

  • avatar
Share
3 Comments
  1. Excellent!
    That's the equivalent of Microsoft's Invoke-Command -VMName

    Thanks Dan!

    1+

  2. Prabhu G 2 years ago

    Hi Dan,

    Im trying to execute a command on a remote VM's Powershell using invoke-vmscript cmdlet. By default this cmdlet will use cmd.exe but I'm trying to execute powershell command. How can we do thihs. Here is the command set I'm using.

    $dns_name = "Test-DNSVM"
    function dnsFeatureInstall {
    $scriptText = @"
    Install-WindowsFeature DNS -IncludeManagementTools
    "@
    $user = 'administrator'
    $password = 'password'
    write-host "installing DNS feature on $dns_name"
    Invoke-VMScript -Scripttype Powershell -ScriptText $scriptText -VM $dns_name -GuestUser "$user" -GuestPassword $password
    }
    dnsFeatureInstall

     

    3+

  3. Prabhu G 2 years ago

    Hi DAN,

    Im trying to run a command in remote VM's powershell using Invoke-vmscript. but it is not working as expected. By default this cmdlet uses cmd.exe sp I changed the scripttype to powershell. Still no changes. Here is the command block I'm using.

    $dns_name = "Test-DNSVM"
    function dnsFeatureInstall {
    $scriptText = @"
    Install-WindowsFeature DNS -IncludeManagementTools
    "@
    $user = 'administrator'
    $password = 'password'
    write-host "installing DNS feature on $dns_name"
    Invoke-VMScript -Scripttype Powershell -ScriptText $scriptText -VM $dns_name -GuestUser "$user" -GuestPassword $password
    }
    dnsFeatureInstall

    1+

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account