VCPGuru
Contributor
Contributor

Disconnect Serial Port

Hi Folks

Disconnect CD-ROM and Floppy disks with the powershell is really easy, but I want to disconnect the Serial Ports in my VM.

Has anyone a script to do that (Or could anybody create a script)???

I dont wont to remove the serial port, because you can only remove the serial port if the VM is "powered off".

Any help would be appreciated!

Cheers

Simon

Best Regards Simon Ciglia
Reply
0 Kudos
LucD
Leadership
Leadership

You have to use the ReconfigVM_Task method from the SDK for that.

$VMname = <VMname>
$vmImpl = Get-VM $VMname
$vm = $vmImpl | Get-View

$spec = new-object VMware.Vim.VirtualMachineConfigSpec

foreach($dev in $vm.Config.Hardware.Device){
  if($dev.DeviceInfo.Label -like "Serial Port*"){
	$temp = New-Object VMware.Vim.VirtualDeviceConfigSpec
	$temp.device = [http://VMware.Vim.VirtualDevice|http://VMware.Vim.VirtualDevice]$dev
	$temp.device.connectable.connected = $false
	$temp.device.connectable.StartConnected = $false
	$temp.operation = "edit"
	$spec.deviceChange += $temp
  }
}
$vm.ReconfigVM_Task($spec)

This changes all serial ports to "disconnected" and will also unselect "Connect at power on".


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

VCPGuru
Contributor
Contributor

Many thanks for your answer!

But when I run this method I got the following error:

Method invocation failed because [http://System.Object[|http://System.Object[]] doesn't contain a method named 'ReconfigVM_Task'.

Any idea?

Thank you!!!

Best Regards Simon Ciglia
Reply
0 Kudos
LucD
Leadership
Leadership

The forum SW has problems with square brackets.

Try the attached script.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

VCPGuru
Contributor
Contributor

I tried the attached script I only modified the first line of your script "$VMname = Get-VM"

What are the preconditions that I can use the "ReconfigVM_Task"? Have I to do something or should that script run on each VI3 with VC?

The error i got told me, that the method "ReconfigVM_Task" is unkown?!

Thank you!

Best Regards Simon Ciglia
Reply
0 Kudos
LucD
Leadership
Leadership

The variable $VMname should be initialised with a string containing the name of a guest.

This is the name as the guest appears in the VI client.

For example:

$VMname = "PC999"

And you did add a Connect-VIServer to the beginning of the script I suspect ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
VCPGuru
Contributor
Contributor

How can I disconnect ALL VMs from the serial port?

I dont want to enter the VMs name

Any idea?

Many thanks

Best Regards Simon Ciglia
Reply
0 Kudos
LucD
Leadership
Leadership

By placing a loop around the previous code.

Something like this


Get-VM | Get-View | %{
  $spec = new-object VMware.Vim.VirtualMachineConfigSpec
  foreach($dev in $_.Config.Hardware.Device){
    if($dev.DeviceInfo.Label -like "Serial Port*"){
	  $temp = New-Object VMware.Vim.VirtualDeviceConfigSpec
	  $temp.device = [http://VMware.Vim.VirtualDevice|http://VMware.Vim.VirtualDevice]$dev
	  $temp.device.connectable.connected = $false
	  $temp.device.connectable.StartConnected = $false
	  $temp.operation = "edit"
	  $spec.deviceChange += $temp
    }
  }
  $_.ReconfigVM_Task($spec)
  Remove-Variable $spec
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
JDMils_Interact
Enthusiast
Enthusiast

Hi LucD,

Your script looks gr8, can you modify it so that the serial ports which output to a file on a datastore are modified so that they can be pointed to a new datastore? I need this code in order to migrate VMs off datastores which are to be decomed.

I've noticed that when I manually edit the VM settings, the new com files are created on the new datastores however the old ones are still present on the original datastore?

Thanks.

 

Reply
0 Kudos
LucD
Leadership
Leadership

You can indeed use a similar method to change the Datastore location to which serial port will write.
But changing the destination this way, will not automatically remove the old file.
You will need to do that separately.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos