I want to be able change from Bridged to NAT on a running VM and have it take effect immediately. I need to do this with a script. How can I do it?
My host is Vista. My guest is Server 2003. I am running VMware server 2x
I am thinking here that 'immediately' is a bit relative. You will drop packets and the VM will be unavailable for a brief period of time.
In order to complete the transition, you'll have to ipconfig /release and ipconfig /renew within the guest (or something equivalent) to drop the old IP and get the new one -- assuming you're using DHCP. If not, you will have issues anyway.
The simplest course of action may be to have two NICs configured in the VM -- one NAT and one Bridged. You could then selectively activate/deactivate them within the guest without touching the VMware Server environment. Would that work for you?
If so, take a look at netsh commands within Windows:
netsh interface set interface <interface name> [ENABLED|DISABLED]
For example, to disable an interface with its default name “Local Area Connection,” run:
netsh interface set interface “Local Area Connection” DISABLED
To enable the interface, you'd then run:
netsh interface set interface “Local Area Connection” ENABLED
This may be cleaner if you're trying to do everything within the guest OS.
That is a helpful suggestion. I had not thought of two NICs, yes that would do it. I am familiar with netsh and am already using that in what I am doing, as I am switching from DHCP to STATIC with that command, and yes, I did not mean "immediate" to be exactly literal.
I would llike to find a way to do it though on the same NIC. I wish there was a command to order the infrastructure to make the switch.
I am no VIX guru, but it looks to me like VIX is intended for manipulating the guest OS from an external source. Unfortunately, this does not include messing with the settings in the VMX file. I'm not that familiar with VMware Server and its APIs, but that is more along the lines of what you are asking.
You might look into using vmware-cmd -- it lives in C:\Program Files\VMware\VMware Server, I think:
vmware-cmd (path to VMX file) setconfig ethernet0.connectionType="nat"
I am guessing that setting the connectionType to "bridged" would revert to Bridged, but I am not certain since I don't have a VMware Server machine to play with.
(Update) A little more information. You may be able to run vmware-cmd from within a VM and have it talk to the host that is running the VM. Get vmware-cmd.exe and any required support files into the guest, and use the connection options described below (put them before the path to the VMX file)
Connection Options:
-H <host> specifies an alternative host (if set, -U and -P must also be set)
-O <port> specifies an alternative port
-U <username> specifies a user
-P <password> specifies a password
Message was edited by: DougBaer
Thanks for the helpful answer. Unfortunately that command is not available in Windows with Server 2.x. Also, I need this command to run from inside the VM. I think you answer is close to the right track, but I'm not quite there yet. Any more suggestions? It seems that no one out there knows how to make this happen, but I am still hoping.
I'm wondering if it's as simple as calling Set-NetworkAdapter cmdlet and setting the network name, VMNet0/1/2/etc... Can anyone confirm?
Looking at my Virtual Network Editor in Server2.0, VMnet0 is bridged, VMnet1 is host-only and VMnet8 is nat....
http://blogs.vmware.com/files/vi-toolkit-lab----vmworld-europe-2009.pdf
RE:
vmware-cmd (path to VMX file) setconfig ethernet0.connectionType="nat"
d/l and install http://communities.vmware.com/thread/191565?tstart=10. All you'll care about is the Console shortcut. See the pic for explanation. Do you have a specific test we could do?

Wow - I think you are on to the answer here. I downloaded and installed all of that and have the VIX Virtuozo Client available. But I don't know what to do next. A specific test? Well, here is what needs to happen. From a command line inside a VM, run a script that changes the NIC of that VM from Bridged to NAT, and another script that does the reverse, changes it from NAT back to Bridged.
Once you have the Virtuozo Suite installed, run the Console from Vix_Virtuozo_Suite folder. Connect to your Server2.0. Select the VM. Then use R and W to read/write RUNTIME variables. (http://www.vmware.com/support/developer/vix-api/vix16_reference/lang/com/functions/WriteVariable.html)
We need a network traffic test to see if the NIC is actually changing from NAT to BRIDGED. How will you know that this change is working correctly?
My worry is that changing the variable might not be enough. If you look at your virtual network/switch configuration using the 'Manage Virtual Networks' app that comes with Server2.0, you'll see on the 'NAT' tab that, by default, VMNet8 virtual switch is used by the NAT service (http://www.extremetech.com/article2/0,2845,1156371,00.asp). So, the thing to do in this situation is either keep searching for answers inside the VI Toolkit, or assign two Virtual NICs to the VM. One on VMNet0 and one on VMNet8. Then, as suggested, use netsh to switch between the two (http://sanbarrow.com/network/commandlineguide2vnets.html)(http://communities.vmware.com/thread/133297).
Any other ideas?
A good test is this: If it switched to NAT, then the VM can see the real network and map drives to machines on the physical network, but no machines on the physical network can see a VM that is on NAT. If it is bridged, then machines on the physical network can see and map drives to the VM. I think that is the ultimate test.
Have you tried this by just changing the connectionType variable?
Do you have Powershell and VI Toolkit installed... We can try something once you do.
I will install those right now. Do you know the URL link to download them? Tell me how to change the connectiion type varialble or any other command you want me to try and I'll try this right away.
Ok, I now have Windows Powershell 1.0 and VMware vSphere PowerCLI installed? Is this correct? Do I have the right things installed? Bear in mind I am not running ESX, but am running Windows Vista 64-bit as the host with VMware Server 2.x and am running 2003 Server in a VM as the guest. What do I do now?
With the VI toolkit there is a CMDLETs reference.
I would try playing with VMNET0/VMNET8 with Set-VirtualSwitch , Set-NetworkAdapter to begin with.
Here is how you connect to your server:
$VMCore = Get-PSSnapin VMware.VimAutomation.Core -EA 0
if ( -not $VMCore ) { Add-PSSnapin VMware.VimAutomation.Core };
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Core")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.Vim")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Types")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Common")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Client20")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.Security.CredentialStore")
$chost = "192.168.1.1";
$cuser = "me";
$cpassword = "mypass";
$cport = 8333;
$cprotocol = "https";
Connect-VIServer -Server $chost -User $cUser -Password $cPassword -Port $cport -Protocol $cprotocol;
if ( $DefaultVIServer -eq $null)
{ Write-Host "Connection Failed!"; return $false }
else
{ Write-Host "Connected : " $DefaultVIServer.IsConnected;
## Lets assign some views to variables
$global:moServiceInstance = (get-view ServiceInstance);
$global:moHostSystem = Get-View -VIObject (Get-VMHost);
$global:DefaultVIServer = $DefaultVIServer;
};
#do something
Disconnect-VIServer -Server $global:DefaultVIServer -WhatIf:$False -Confirm:$false -EA 0;
I wasn't sure what to do with that code. I put it in a batch file, but those don't run in powershell apparently. I then pasted it directly into the powershell windw. Is that right? This is what I get when I do that. I'm a bit lost here. I appreciate your help...
PS C:\Documents and Settings\Administrator> $VMCore = Get-PSSnapin VMware.VimAutomation.Core -EA 0
PS C:\Documents and Settings\Administrator> if ( -not $VMCore ) { Add-PSSnapin VMware.VimAutomation.Core };
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithParti
alName("VMware.VimAutomation.Core")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ [http://Reflection.Assembly|http://Reflection.Assembly]: <<<< :LoadWithPartialName("VMware.VimAutomation.Core")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithParti
alName("VMware.Vim")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ [http://Reflection.Assembly|http://Reflection.Assembly]: <<<< :LoadWithPartialName("VMware.Vim")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithParti
alName("VMware.VimAutomation.Types")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ [http://Reflection.Assembly|http://Reflection.Assembly]: <<<< :LoadWithPartialName("VMware.VimAutomation.Types"
)
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithParti
alName("VMware.VimAutomation")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ [http://Reflection.Assembly|http://Reflection.Assembly]: <<<< :LoadWithPartialName("VMware.VimAutomation")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithParti
alName("VMware.VimAutomation.Common")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ [http://Reflection.Assembly|http://Reflection.Assembly]: <<<< :LoadWithPartialName("VMware.VimAutomation.Common
")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithParti
alName("VMware.VimAutomation.Client20")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ [http://Reflection.Assembly|http://Reflection.Assembly]: <<<< :LoadWithPartialName("VMware.VimAutomation.Client
20")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithParti
alName("VMware.Security.CredentialStore")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ [http://Reflection.Assembly|http://Reflection.Assembly]: <<<< :LoadWithPartialName("VMware.Security.CredentialS
tore")
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> $chost = "192.168.0.4";
PS C:\Documents and Settings\Administrator> $cuser = "mswarbrick";
PS C:\Documents and Settings\Administrator> $cpassword = "cnecne997";
PS C:\Documents and Settings\Administrator> $cport = 8333;
PS C:\Documents and Settings\Administrator> $cprotocol = "https";
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> Connect-VIServer -Server $chost -User $cUser -Password $cPassword -Port $cpo
rt -Protocol $cprotocol;
WARNING: There were one or more problems with the server certificate:
A certification chain processed correctly, but terminated in a root certificate which isn't trusted by the trust
provider.
The certificate's CN name does not match the passed value.
IsConnected : True
Name : 192.168.0.4
Port : 8333
SessionId : A4FA03DA-B4B1-4336-8FF4-15B864F79EA0
User : mswarbrick
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> if ( $DefaultVIServer -eq $null)
>> { Write-Host "Connection Failed!"; return $false }
>> else
>> { Write-Host "Connected : " $DefaultVIServer.IsConnected;
>> ## Lets assign some views to variables
>> $global:moServiceInstance = (get-view ServiceInstance);
>> $global:moHostSystem = Get-View -VIObject (Get-VMHost);
>> $global:DefaultVIServer = $DefaultVIServer;
>> };
>>
Connected : True
PS C:\Documents and Settings\Administrator> #do something
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> Disconnect-VIServer -Server $global:DefaultVIServer -WhatIf:$False -Confirm:
$false -EA 0;
PS C:\Documents and Settings\Administrator>
It looks like you're connecting and disconnecting successfully. The only problem I see is this thread editor changed my 'loadwithpartialassembly' lines. Take a look at the attachment of how they're supposed to look.
So between your connect and disconnect code you have to retrieve the VM you want and try those two networking CMDLETS. Look at vmwarescripting.com for Powershell examples. Or get Halr9000's book. http://halr9000.com/article/716
The attachment looks a bit different from the other one you sent. This does not have any place to put in my username and password and IP and such.
Here is the out put I get when I paste that into power shell...
PS C:\Documents and Settings\Administrator> #region Title Block
PS C:\Documents and Settings\Administrator> #Name:vim_0_firestarter
PS C:\Documents and Settings\Administrator> #File:vim_0_firestarter.ps1
PS C:\Documents and Settings\Administrator> #Author:fixitchris
PS C:\Documents and Settings\Administrator> #Date:April 2009
PS C:\Documents and Settings\Administrator> #Purpose:Prepares environment for VI/dotnet execution.
PS C:\Documents and Settings\Administrator> "module " + $myInvocation.myCommand.Name;
module
PS C:\Documents and Settings\Administrator> #endregion Title Block
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> #region Firestarter
PS C:\Documents and Settings\Administrator> $VMCore = Get-PSSnapin VMware.VimAutomation.Core -EA 0
PS C:\Documents and Settings\Administrator> if ( -not $VMCore ) { Add-PSSnapin VMware.VimAutomation.Core };
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> $scriptPath = split-path $myInvocation.myCommand.Path;
Split-Path : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:25
+ $scriptPath = split-path <<<< $myInvocation.myCommand.Path;
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> #[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadFile($($scriptPath + "\HIDLibrary.dll"));
PS C:\Documents and Settings\Administrator> #[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadFile($($scriptPath + "\USBlib.dll"));
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadFile($($scriptPath + "\devconLib.dll"));
Exception calling "LoadFile" with "1" argument(s): "Absolute path information is required."
At line:1 char:38
+ [http://Reflection.Assembly|http://Reflection.Assembly]::LoadFile( <<<< $($scriptPath + "\devconLib.dll"));
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadFile($($scriptPath + "\nhciClientNET.dll"))
;
Exception calling "LoadFile" with "1" argument(s): "Absolute path information is required."
At line:1 char:38
+ [http://Reflection.Assembly|http://Reflection.Assembly]::LoadFile( <<<< $($scriptPath + "\nhciClientNET.dll"));
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("System.Diagnostics")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("System.Runtime.InteropServ
ices")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("System.Net")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Core"
)
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.Vim")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Types
")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Commo
n")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Clien
t20")
PS C:\Documents and Settings\Administrator> [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.Security.Credential
Store")
PS C:\Documents and Settings\Administrator> #endregion Firestarter
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> #region TheEnd
PS C:\Documents and Settings\Administrator> "vim_0_firestarter finished."
vim_0_firestarter finished.
PS C:\Documents and Settings\Administrator> #endregion TheEnd
I was only referring to the LoadWithPartialName lines of code, since this editor hacked em up.
Let's try changing the variable from bridged to nat. Run the Virtuozo Console, connect to your box, https://192.168.0.4:8333/sdk.
Then select the VM you want and hit W to rewrite the ethernet0.connectionType to nat.
Then do your test to see if the variable change has any effect on the traffic pattern.
Then change the ethernet0.connectionType to bridged and re-do the test.
I connected, hit W, and I get...
1. VIX_VM_GUEST_VARIABLE
2. VIX_VM_CONFIG_RUNTIME_ONLY
3. VIX_GUEST_ENVIRONEMNT_VARIABLE
Where do I go from here?
Number 2.
