Running commands in the guest OS

To run a command on the guest operating system of a virtual machine, you can use the Invoke-VMScript cmdlet. To use this cmdlet, the virtual machines must be powered on and must have VMware Tools installed. You also need network connectivity to the ESXi server hosting the virtual machine on port 902.

The Invoke-VMScript cmdlet has the following syntax:

Invoke-VMScript [-ScriptText] <String> [-VM] <VirtualMachine[]>
    [-HostCredential <PSCredential>] [-HostUser <String>] 
    [-HostPassword <SecureString>] [-GuestCredential <PSCredential>]
    [-GuestUser <String>] [-GuestPassword <SecureString>] 
    [-ToolsWaitSecs <Int32>] [-ScriptType <ScriptType>] [-RunAsync]
    [-Server <VIServer[]>] [-WhatIf] [-Confirm] [<CommonParameters>] 

The -ScriptText and -VM parameters are required. The value of the -ScriptText parameter is the text of the script you want to run or the path to the script. The -ScriptType parameter specifies the type of script. Valid values are PowerShell, Bat, and Bash. For Windows virtual machines, the default value is PowerShell, while for Linux virtual machines it is Bash.

The following example runs 'ipconfig /all' on the guest operating system of the VM2 virtual machine:

PowerCLI C:> $GuestCredential = Get-Credential
PowerCLI C:> Invoke-VMScript -VM VM2 -ScriptText 'ipconfig /all' `
>> -GuestCredential $GuestCredential

The output of the preceding command is as follows:

ScriptOutput
------------------------------------------------------------------
|  Windows IP Configuration
|
|     Host Name . . . . . . . . . . . . : VM2
|     Primary Dns Suffix. . . . . . . . : blackmilktea.com
|     Node Type . . . . . . . . . . . . : Hybrid
|     IP Routing Enabled. . . . . . . . : No
|     WINS Proxy Enabled. . . . . . . . : No
|     DNS Suffix Search List. . . . . . : blackmilktea.com
|
|  Ethernet adapter Local Area Connection:
|
|     Connection-specific DNS Suffix. . :
|     Description . . . . . . . . . . . : vmxnet3 Ethernet Adapter
|     Physical Address. . . . . . . . . : 00:50:56:81:1c:0c
|     DHCP Enabled. . . . . . . . . . . : No
|     Autoconfiguration Enabled . . . . : Yes
|     IPv4 Address. . . . . . . . . . . : 192.168.0.202(Preferred)
|     Subnet Mask . . . . . . . . . . . : 255.255.255.0
|     Default Gateway . . . . . . . . . : 192.168.0.1
|     DNS Servers . . . . . . . . . . . : 192.168.0.2
|                                         192.168.0.3
|     NetBIOS over Tcpip. . . . . . . . : Enabled
|
------------------------------------------------------------------
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.186.201