VMware Cloud Community
Zsoldier
Expert
Expert
Jump to solution

Upgrade VM Guest NIC and other things using PowerCLI

Anyone figure out how to do this?  I'm trying to script the following process:

  • Script Upgrade VM Tools to current version.  <-- Done, grabbed from somewhere.  Using Update-Tools cmdlet after check
Function Check-ToolsStatus($vm){
$vmview = get-VM $vm | Get-View
$status = $vmview.Guest.ToolsStatus
if ($status -match "toolsOld"){
$vmTools = "Old"}
elseif($status -match "toolsNotRunning"){
$vmTools = "Not Running"}
else{
$vmTools = "Ok"}
return $vmTools
}
  • Script Upgrade VM Hardware from 4 to 7.  <-- Done, grabbed from somewhere.

Function Check-VMHardwareVersion($vm){      $vmView = get-VM $vm | Get-View      $vmVersion = $vmView.Config.Version      $v4 = "vmx-04"      $v7 = "vmx-07"                if ($vmVersion -eq $v4){                $vmHardware = "Old"}           elseif($vmVersion -eq $v7){                $vmHardware = "Ok"}                    else{Write-Host "Error!!" -ForegroundColor Red                $vmHardware = "Error"}                     return $vmHardware } Function Upgrade-VMHardware($vm){      $vmview = Get-VM $vm | Get-View      $vmVersion = $vmView.Config.Version      $v4 = "vmx-04"      $v7 = "vmx-07"           if ($vmVersion -eq $v4){                Write-Host "Version 4 detected" -ForegroundColor Red                               # Update Hardware                Write-Host "Upgrading Hardware on" $vm -ForegroundColor Yellow                Get-View ($vmView.UpgradeVM_Task($v7)) | Out-Null      }     }

  • Script Copy current NIC configurations. <-- Not sure how to do this.  vbScript? netsh? using invoke-vmscript?
    • Needs to get full IP address config.
    • Needs to get current nic adapter attached network.

????

  • Script Add VMXNET 2 | 3 nic. <-- See below for idea.

#My Thought

New-NetworkAdapter -Type EnhancedVmxnet -NetworkName <from previous script> -vm <targetvm> -confirm:$false

    • Should remove old nic once settings have been transferred.

#Probably a where statement would be better since I only want to remove non enhancedvmxnet and vmxnet3

$nic = Get-NetworkAdapter -VM | Remove-NetworkAdapter -networkadapter $nic[0]

  • Script Apply copied NIC configs to new nic (VMXNET2 | 3) <-- Not sure how to do this. vbScript? netsh? using invoke-vmscript?

????

  • Script Add vmdisk, change controller to pvscsi, wait 30 sec for OS to detect.

#My Thought

$tempdisk = $vm | New-Harddisk -CapacityKB 2048 -StorageFormat Thin

Sleep 30

$vm | Shutdown-VMGuest -confirm:$false

Sleep 90

If ($vm.powerstate -eq "PoweredOn"){Stop-VM $vm -Confirm:$false}

$tempdisk | New-ScsiController -Type Paravirtual

Start-VM $vm -confirm:$false

Sleep 90

  • Script to shutdown, remove added disk, and change current disks to pvscsi

#My Thought

$vm | Shutdown-VMGuest -confirm:$false

Sleep 90

If ($vm.powerstate -eq "PoweredOn"){Stop-VM $vm -Confirm:$false}

$tempdisk = $vm | get-harddisk | where {$_.CapacityKB -eq "2048"}

$tempdisk | Remove-HardDisk

$vm | get-harddisk | set-scsicontroller -type paravirtual -confirm:$false

Start-VM $vm -confirm:$false

Message was edited by: Zsoldier

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

Hi,

It will work certenly with the root user, but it is possible to enable changes with user from root group.Behind the two commandlets there are two perl scripts that you can find in C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\ folder. The scripts are GetVmGuestNetworkInterface_LinuxGuest and SetVMGuestNetworkInterface_LinuxGuest.

The Get script reads configuration from /etc/sysconfig/network-script/ifcfg* files and also executes ifconfig command. A little observation on the permissions of pointed files shows that the get commandlet will work with user from root group.

The Set script writes into configuration files in /etc/sysconfig/network-scripts/, so you should give write permission on root group if you want to use another user.

The linux machine in our test environement is RedHat Enterprise 5

Vitali

PowerCLI Team

View solution in original post

Reply
0 Kudos
11 Replies
DBofTLP
Contributor
Contributor
Jump to solution

ah, must watch this thread.  will look for ideas to throw in and get an answer.  i need this!

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi,

* About changing the virtual nic type you can use the Set-NetworkAdapter commandlet.

  Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -Type EnhancedVmxnet

* About saving network configuration.

   ** You can use  Get-VMGuestNetworkInterface and Set-VMGuestNetworkInterface to obtain/set infromation from Guest OS about the network adapter configuration like does the machine has statically assigned IP address or it takes one from DHCP, gateway, DNS, etc.

   ** Get-NetworkAdapter can give you information about the connected virtual network.

* Upgrading the version of virtual hardware could be made in single line:

Get-VM | where { $_.Version -eq "v4" } | Set-VM -Version v7 -confirm:$false

Vitali

PowerCLI Team

DBofTLP
Contributor
Contributor
Jump to solution

Get-VMGuestNetworkInterface is kicking back errors after requesting credentials.

'Connect to host service 'https://myvcenter/sdk' at port 902' the following error occured: 'Failed to resolve host'


+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_VixWaitForJob_VixErr
or,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVmGuestNetworkInterface

So, I'm stuck at retrieving the network settings from the existing network adapters to be reapplied to the new adapters after they're added...

Reply
0 Kudos
Zsoldier
Expert
Expert
Jump to solution

@vitalibaruh I'll give these a try, thanks.

For Linux (RHEL4/5/6) VM's what level of permissions on the guest OS are needed to get it's guest network information? 

Can it be an account w/ sub-root permissions?

Message was edited by: Zsoldier

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
Zsoldier
Expert
Expert
Jump to solution

@DBofTLP Seems you need to connect to the vCenter server using it's FQDN.  In your example:

'https://myvcenter/sdk

You should connect in this method:

connect-viserver myvcenter.mydomain.com

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
bulletprooffool
Champion
Champion
Jump to solution

Seems a fair bit that you would like to do.

Have you had a look at the VMware labs - they have a tool called Onyx that will help you in gathering code requirements for what you are trying to do.

See the following links:

http://www.get-virtual.info/2011/02/16/script-of-the-day-quick-and-easy-vmware-powershell-scripts/

http://download3.vmware.com/software/vmw-tools/onyx/Onyx_2.0.3910.32223.zip

the actual Labs page appears to be down at the moment . . but I am sure will be back soon.

One day I will virtualise myself . . .
Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi,

It will work certenly with the root user, but it is possible to enable changes with user from root group.Behind the two commandlets there are two perl scripts that you can find in C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\ folder. The scripts are GetVmGuestNetworkInterface_LinuxGuest and SetVMGuestNetworkInterface_LinuxGuest.

The Get script reads configuration from /etc/sysconfig/network-script/ifcfg* files and also executes ifconfig command. A little observation on the permissions of pointed files shows that the get commandlet will work with user from root group.

The Set script writes into configuration files in /etc/sysconfig/network-scripts/, so you should give write permission on root group if you want to use another user.

The linux machine in our test environement is RedHat Enterprise 5

Vitali

PowerCLI Team

Reply
0 Kudos
Zsoldier
Expert
Expert
Jump to solution

Thanks, think I have enough info to sew up the pieces.  Thanks.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
csa
Contributor
Contributor
Jump to solution

Hi Zsoldier,

I am facing a similar task. Any chance you could share the final script once it's done?

Reply
0 Kudos
Zsoldier
Expert
Expert
Jump to solution

Yeah, sure thing.Smiley Happy

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
csa
Contributor
Contributor
Jump to solution

Great ... looking forward to it 😉

Reply
0 Kudos