Configuring the network of virtual machines

To configure the network of a virtual machine guest operating system, you can use the Invoke-VMScript cmdlet, to run scripts in the guest operating system of a virtual machine. You have already seen the Invoke-VMScript cmdlet in Chapter 5, Managing Virtual Machines with PowerCLI .

To configure the network of Microsoft Windows virtual machines with PowerShell V3 or later versions installed, you can use the cmdlets in the netadapter and NetTCPIP modules to modify network adapters and TCP/IP settings. For Microsoft Windows virtual machines with PowerShell V1 or V2 installed, you can use the Windows Management Instrumentation (WMIGet-WMIObject cmdlet with the Win32_NetworkAdapterConfiguration class. In this section, we will focus on using the cmdlets from the NetTCPIP module.

Setting the IP address

The PowerShell cmdlet we will use for setting the IP address, network mask, and default gateway is New-NetIPAddress. This cmdlet has the following syntax. The first parameter set is for creating and configuring an IP address by using an interface alias:

New-NetIPAddress [-IPAddress] <String> [-AddressFamily {IPv4 |
    IPv6}] [-CimSession <CimSession[]>] [-DefaultGateway <String>]
    [-PolicyStore <String>] [-PreferredLifetime <TimeSpan>]
    [-PrefixLength <Byte>] [-SkipAsSource <Boolean>] [-ThrottleLimit
    <Int32>] [-Type {Unicast | Anycast}] [-ValidLifetime <TimeSpan>]
    -InterfaceAlias <String> [-Confirm] [-WhatIf] [<CommonParameters>]

The second parameter set is for creating and configuring an IP address by using an interface index:

New-NetIPAddress [-IPAddress] <String> [-AddressFamily {IPv4 | IPv6}]
    [-CimSession <CimSession[]>] [-DefaultGateway <String>] [-PolicyStore
    <String>] [-PreferredLifetime <TimeSpan>] [-PrefixLength <Byte>]
    [-SkipAsSource <Boolean>] [-ThrottleLimit <Int32>] [-Type {Unicast |
    Anycast}] [-ValidLifetime <TimeSpan>] -InterfaceIndex <UInt32>
    [-Confirm] [-WhatIf] [<CommonParameters>]

The -InterfaceAlias, -InterfaceIndex, and -IPAddress parameters are required.

To use the Invoke-VMScript cmdlet, we have to specify credentials for the guest operating system. We will retrieve these credentials using the Get-Credential cmdlet and save them in the variable $GuestCredential, using the following command:

PowerCLI C:> $GuestCredential = Get-Credential

Next, we will create a string with the PowerShell command to set the IP address, network mask, and default gateway for network interface Ethernet. We will save the command in the variable $ScriptText. Notice that the -PrefixLength parameter specifies the number of bits of the network mask. A network mask of 255.255.255.0 is a 24-bit network mask. So we will use 24 as the value of the -PrefixLength parameter:

PowerCLI C:> $ScriptText = 'New-NetIPAddress -InterfaceAlias
    "Ethernet" -AddressFamily IPv4 -IPAddress 192.168.10.31
    -PrefixLength 24 -DefaultGateway 192.168.10.1'

Finally, we will call the Invoke-VMScript cmdlet, to run the command in the $ScriptText variable, in the guest operating system of virtual machine VM2:

PowerCLI C:> Invoke-VMScript -ScriptText $ScriptText -VM VM2
    -GuestCredential $GuestCredential

The preceding command gives the following output:

ScriptOutput
---------------------------------------------------------------------
|
---------------------------------------------------------------------

Setting the DNS server addresses

We have to use the Set-DnsClientServerAddress cmdlet to specify the DNS servers. This cmdlet has the following syntax. The first parameter set is for setting the DNS servers by using an interface alias:

Set-DnsClientServerAddress [-InterfaceAlias] <String[]> 
    [-CimSession <CimSession[]>] [-PassThru] [-ResetServerAddresses]
    [-ServerAddresses <String[]>] [-ThrottleLimit <Int32>] [-Validate]
    [-Confirm] [-WhatIf] [<CommonParameters>]

The second parameter set is for setting the DNS servers by name:

Set-DnsClientServerAddress [-CimSession <CimSession[]>]
    [-PassThru] [-ResetServerAddresses] [-ServerAddresses <String[]>]
    [-ThrottleLimit <Int32>] [-Validate] [-Confirm] [-WhatIf]
    [<CommonParameters>]

The third parameter set is for setting the DNS servers by using an input object:

Set-DnsClientServerAddress [-CimSession <CimSession[]>] [-PassThru]
    [-ResetServerAddresses] [-ServerAddresses <String[]>] [-ThrottleLimit
    <Int32>] [-Validate] -InterfaceIndex <UInt32[]> [-Confirm] [-WhatIf] 
    [<CommonParameters>]

The -InterfaceAlias or -InterfaceIndex parameter is required.

In the following command, we will create a string with the command to set the DNS server of the interface Ethernet to IP address 192.168.0.130 and save this string to a variable called $ScriptText:

PowerCLI C:> $ScriptText = 'Set-DnsClientServerAddress
    -InterfaceAlias "Ethernet0" -ServerAddresses 192.168.0.130'

Finally, we will call the Invoke-VMScript cmdlet to run the command in the $ScriptText variable, in the guest operating system of virtual machine VM2:

PowerCLI C:> Invoke-VMScript -ScriptText $ScriptText -VM VM2
    -GuestCredential $GuestCredential

The preceding command gives the following output:

ScriptOutput
---------------------------------------------------------------------
|
---------------------------------------------------------------------

Retrieving the network configurations

To retrieve the IP configuration of a Microsoft Windows virtual machine, we can either use the ipconfig /all command or the Get-NetIPConfiguration cmdlet. This cmdlet has the following syntax. The first parameter set is for retrieving the network configuration of a network adapter by interface alias:

Get-NetIPConfiguration [[-InterfaceAlias] <String>]
    [-AllCompartments] [-CimSession <CimSession>] [-CompartmentId <Int32>]
    [-Detailed] [<CommonParameters>]

The second parameter set is for retrieving the network configuration of all network adapters:

Get-NetIPConfiguration [-AllCompartments] [-CimSession <CimSession>]
    [-CompartmentId <Int32>] [-Detailed] -All [<CommonParameters>]

The third parameter set is for retrieving the network configuration of a network adapter by interface index:

Get-NetIPConfiguration [-AllCompartments] [-CimSession <CimSession>]
    [-CompartmentId <Int32>] [-Detailed] -InterfaceIndex <Int32>
    [<CommonParameters>]

The -All, or -InterfaceIndex parameter is required.

First, we will save the string with the command to retrieve the network configuration for the network interface Ethernet in the variable $ScriptText:

PowerCLI C:> $ScriptText = 'Get-NetIPConfiguration
    -InterfaceAlias "Ethernet0"'

Finally, we will call the Invoke-VMScript cmdlet, to run the command in the $ScriptText variable, in the guest operating system of virtual machine VM2:

PowerCLI C:> Invoke-VMScript -ScriptText $ScriptText -VM VM2
    -GuestCredential $GuestCredential

The output of the preceding command is as follows:

ScriptOutput
---------------------------------------------------------------------
|
|
|  InterfaceAlias       : Ethernet0
|  InterfaceIndex       : 12
|  InterfaceDescription : Intel(R) 82574L Gigabit Network Connection
|  IPv4Address          : 192.168.10.31
|  IPv6DefaultGateway   :
|  IPv4DefaultGateway   : 192.168.10.1
|  DNSServer            : 192.168.0.130
|
|
|
|
---------------------------------------------------------------------
..................Content has been hidden....................

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