PowerCLi script to check if 2 virtual machines are on the same host

Hello

I'm nubby on PowerCLI and I would like to help in the construction of a script.

The script should check if two virtual machines (named test1 and test2) are running on the same host in a vCenter and send a notification (email), if they do.

Something similar with affinity / anti-affinite DRS rules (but not through the DRS enabled on vCenter).  My focus is on the affinity. In case this happens, I need vMotion one of them on an another ESXi host(manually or automated).

What I have done until now is the following:

Get - VM | Select Name, @{N = 'Cluster'; {E = {Get-Cluster - VM $_}}, '

@{N = "ESX host"; {E = {Get-VMHost - VM $_}}, '

@{N = 'vCenter'; E={$_. "(ExtensionData.CLient.ServiceUrl.Split('/') [2]}}, '"

@{N = "Datastore"; {E = {Get-Datastore - VM $_}} | `

Export-Csv - NoTypeInformation C:\Scripts\file.csv

That becomes all the VMS and export a CSV file information:

NameClusterESX hostvCenterData store
test2cluster_testESXi_test1vCenter_test1:443datastore1
Test1cluster_testESXi_test1vCenter_test1:443datastore1
For1.localcluster_testESXi_test1vCenter_test1:443datastore1
VM1.localcluster_test2ESXi_test2vCenter_test1:443datastore1
VM2.localcluster_test3ESXi_test3vCenter_test1:443datastore1
VM31.localcluster_test3ESXi_test3vCenter_test1:443datastore1

Thank you.

Try like this.

$tgtVM = "test1", "test2".

Get-VM-name $tgtVM | Group-object - property VMHost | %{

if($_.) County - gt 1) {}

$vm = $_. Group | Get-Random

$esx = $vm. VMHost

$tgtEsx = get-Cluster - $vm VM | Get-VMHost | where {$_.} Name - not $esx. Name} | Get-Random

Move-VM - $vm - Destination $tgtEsx VM - confirm: $false

}

}

Tags: VMware

Similar Questions

  • vCenter Server and Manager on the same virtual machine and on the same host ESXi update that they manage

    Hi all

    I have a question to which I did not respond.

    I have an ESXi 5 on Proliant DL380 G7 server host. I want to install vCenter Server and Manager Update on one of the Server 2008 R2 VM on the same host. I know it's possible, but my question is how Update manager will provide updates to the same ESXi host it will run on? The update is applied every time host must spend in maintenance mode and power off the virtual computer running on this subject which means he'll kill VM where vCenter Server and Update Manager is running and the update will fail right?

    Is that possible at all or the VCS and UM such as a virtual computer operating point is updated to other guests if you have them or vMotion as VM to another host when you want to update the VCS and UM host is running?

    Thanks in advance.

    It will not work.  You will need at least two hosts.  This way you can vMotion the vCenter VM to another host.  From there, you can use VUM.  Otherwise, you will need to patch the host as a stand-alone host.

    See below for patching a standalone host

    http://communities.VMware.com/people/vmroyale/blog/2011/09/15/updating-ESXi-5--single-use-esxcli-how-to

  • What is the rate of transmission between the virtual machines connected to the same virtual switch?

    Hi guys,.

    I am trying to determine if the rate of data transmission between virtual machines connected to the same virtual switch is equivocal to 10 GB ethernet or faster.

    Thank you

    MK

    If it's the same vSwitch in the same ESX, should be the speed of the bus

  • Configure replication vSphere fails - there is a virtual machine that has the same UUID instance than the one you are trying to configure

    Hi all


    My colleague was trying to extend a hard disk of a server 2008R2 who (1) had a snapshot and (2) was being replicated. The operation has failed and they took the snapshot replication can be stopped. Long story short when I got to him, the server could not be configured for replication and could not extend the disk and a vMotion failed.

    Looking at the files of the VM on the data store and the VMs .vmx file we have all the characteristics of a virtual machine always configured for replication of vSphere. If you try to configure replication vSphere causes an instant error "vsphere replication does not support change the length of a replicated disk. After a few changes I can reconfigure vSphere new replication, however, when the wizard has finished, I get a new error message (see below):

    "Try to set up vSphere that replication has failed for the VM"name vm"during the creation of groups"vm". There is a virtual "vm-name" machine that has the same instance UUID ' UUID of the Virtual Machine "that you want to configure. The existing vSphere of replication is configured for site protection.

    A possible solution would be to reset the database embedded VRMS our DR as suggested here VR configure replication after losing a VC site server but I was wondering if there is another solution? I have not reset the database of VRM before and cannot find a document that tells exactly what to do. I guess I need to stop all the rehearsals before resetting the database?

    See you soon,.

    Dean

    Hello

    There is no need to reset the VRMS database.

    If you know the value GID... an orphan replication, you can use the CROWD VRMS (https://vrms_address:8043/mob/?moid=GID-...&vmodl=1) to call the destroy method - it is eliminating simple replication to the site of the source or the target.

    If you don't know the GID... value, it can be found of VRMS DB or log files. Please file an SR for assistance.

    This procedure is already documented in internal KB articles and help using VMware support:

    KB article internal 2056086, section "cannot replicate virtual machine as there is another virtual machine with the same UUID instance"

    and also internal KB article 2060751.

    The admin guide section will be updated as it is confusing right now by suggesting only to reset the DB.

    Kind regards

    Martin

  • How do I create standard vSwitches in a HA cluster where the virtual machine switches to the same network?

    I have a lab put in place with 2 hosts 5.5 ESXi and vCenter.  I want to create a situation where each virtual machine must have a dedicated network card.  If I create vSwitchABC on host 1, what should I do on host 2?  Create vSwitchABC on that host too and connect it to the same physical switch?  Which will allow the host 1 failure and the virtual machine to arrive on host 2 on the network because the same vSwitch names?

    Thank you!

    Welcome to the community-Yes, you need to create a vswitch on host 2 has a virtual machine port group with the same name on host 1 connection on the same subnet as connected to the host - 1

  • Two virtual machines running at the same time

    Fussion 1.0, I remember being able to use two virtual machines at the same time, and each me each own IP without worrying if I used the bridged network configuration or address NAT. With the Version 2.0.1 (128865), only one will get an IP address and the other, the two XP SP3, visit account disabled NETWORK card. Any recommendations?

    Yes, it's tru, but I updated the vmx file to have another MAC. NIC I get this error when I try to boot the secondary machine (which otherwise boots fine solo).

    As I mentioned in the linked post, there are several unique identifiers that MAC address. Fusion has a UUID, and Windows has a SID. All must be unique (my guess is that it's the SID).

    The network bridge on/dev/vmnet0 of the device does not work.

    Which will result in network bridge does not work. The only suggestion I have right now is to try to restart the host, but demand around other people seem to be seeing this as well.

  • Start the virtual machine based on the load of another virtual machine

    Hi all

    I am looking for a solution that will start the VM on request depending on the load on the other virtual machines, perhaps in the same resource pool. Essentially, the expected result would be something like this:

    -When the dv01 reaches 75% CPU usage for 15 minutes,

    -Boot VM02

    -Once dv01 was less 40% use for 30 minutes,

    -Close the VM02

    This could also be a new virtual machine that is upwards of a model and Sysprep, rather than one that just sits there, offshore and unused.

    I hope that the final objective is clear. Does anyone have recommendations on how to do it? I just did a quick search on certain clauses that I thought would be relevant and did not really turn anything upwards.

    Thanks for your comments!

    -James

    jamesgreen wrote:

    Hi all

    I am looking for a solution that will start the VM on request depending on the load on the other virtual machines, perhaps in the same resource pool. Essentially, the expected result would be something like this:

    -When the dv01 reaches 75% CPU usage for 15 minutes,

    -Boot VM02

    -Once dv01 was less 40% use for 30 minutes,

    -Close the VM02

    This could also be a new virtual machine that is upwards of a model and Sysprep, rather than one that just sits there, offshore and unused.

    I hope that the final objective is clear. Does anyone have recommendations on how to do it? I just did a quick search on certain clauses that I thought would be relevant and did not really turn anything upwards.

    Thanks for your comments!

    -James

    with raise vm02, you can set the alarm action to run a script that will start vm02.

    PowerCLI command would be

    Start-VM - vm VM02

    you could probably add an alarm if dv01 has a less than 40% cpu

    run another script

    the command is:

    stop-vmguest - vm-VM02

  • Check if a virtual machine has connected network and connect if it isn't?

    Thus, creating a big deployment VM automation workflow, I need a way to check and make sure that the deployed virtual computer has its connected network. However, I can't seem to find a way to generate scripts which correctly. Any ideas?

    Hello

    There are different strategies. Look at the 'vim3waitDNSNameInTools' action, it waits until the virtual machine arrives with a given host name. Which ensures that this customization is done completely.

    Alternatively you can ping the virtual computer by using the System.isHostReachable () method, find an example here: Ping on the workflow server

    (I remember there were some problems with this method in previous versions, be sure to properly test ;-))

    Another strategy might be to use the operations of comments and call a ping of in the guest operating system (with which you shall vro network connection to the virtual machine itself, you can for example leave the guest operating system ping the address of the gateway or so)

    Kind regards

    Joerg

  • I'm looking for a Script that can list all virtual machines with type of NIC E1000 via the output of the CSV file.

    Hi gurrus and LucD

    I'm looking for a Script that can list all virtual machines with type of NIC E1000 via the output of the CSV file.

    The script should search for information in a multiple Vcenter servers and multiple clusters and list all the VMs name, status (two powers on or off) with type card NETWORK Type E1000 only no other.

    Concerning

    Nauman

    Try like this

    $report = @)

    {foreach ($cluster Get-cluster)

    foreach ($rp in Get-ResourcePool-location $cluster) {}

    foreach ($vm in (Get-VM-location the $rp |)) Where {Get-NetworkAdapter - VM $_______ | where {$_.}} Type - eq "e1000"}})) {}

    $report += $vm. Select @{N = "VM"; E={$_. Name}},

    @{N = 'vCenter'; E={$_. Uid.Split('@') [1]. "Split(':') [0]}},"

    @{N = "Cluster"; E = {$cluster. Name}},

    @{N = "ResourcePool"; E = {$rp. Name}}

    }

    }

    }

    $report | Export Csv C:\temp\report.csv - NoTypeInformation - UseCulture

  • How to check if a virtual machine has active hotplug?

    Hello

    I am trying to create a workflow that is supposed to change the amount of vCPUs and memory on a virtual computer.

    (for those who arrive suspicious: I'm continuing / edition work of Munel, another user here)

    Since we have different types of VMS, I need to check if a virtual machine is capable of hotplugging processors and memory.

    Now the workflow is designed in a way, it first tries to change the values, and if it runs into an error, it checks if the error code says that the vhardware is not active hotplug then stop the virtual machine or throws an exception.

    This is not a very nice solution and must be changed.

    Is there a way to check the ability before hotplug actually trying to change the vhardware?

    Thanks in advance

    Andreas

    Hi Andreas,

    To check if a virtual machine has a hot connection activated, you have the following options:

    Check if memory hot add is enabled:

    virtual_machine_instance.config.memoryHotAddEnabled

    Check if the CPU hot add is enabled

    virtual_machine_instance.config.cpuHotAddEnabled

    Check if the hot removal of CPU is enabled

    virtual_machine_instance.config.cpuHotRemoveEnabled

    If you have the vSphere client, tree options above are located in the Configuration dialog box of the virtual machine on the tab Options-> memory/CPU hotplug.

    Hope this helps,

    Martin

  • the request to turn off this virtual machine failed because the corresponding vmware tools script didn't run successfully

    using vmware fusion 2.0.5

    the request to turn off this virtual machine failed because the corresponding vmware tools script didn't run successfully

    now vmware cant off power

    is vmware tools script to run when windows is running

    If I power off (mac - force quit) now... what happens

    VMware Fusion 2.x...

    VMware Fusion (menu bar) > help > VMware Fusion help > running VMware Fusion and Virtual Machines > Shutting Down a virtual machine operating system

    VMware Fusion (menu bar) > help > VMware Fusion help > running VMware Fusion and Virtual Machines > Options for VMware Fusion food orders

    VMware Fusion (menu bar) > help > VMware Fusion help > running VMware Fusion and Virtual Machines > orders of switching power supply from the default Options

    ==========

    Note: It's like pulling the plug of the wall on a physical computer. Whatever this is not saved will be lost and there is always the possibility of corrupting the file system where preforming a hard stop! (Even if you have no choice but to do.)

    VMware Fusion menu bar > VM > press the option key and select Power Off

    By pressing the keys alt/option "Shutdown called" changes to "Power Off".

    Note: It also changes orders of "Suspend comments" and "Restart Guest' too.

    Or if this does not work then...

    On the library Virtual Machine ctrl - click on the target of the Virtual Machine and select Show in Finder and delete the .vmss file, the ctrl + click the target package of Virtual Machine, and then after the merger closed. Note: All that was not justified for the suspended state will be lost by deleting the .vmss file. Note: However: you have no other choice if the Virtual Machine does not suspend/resume/stop/start correctly.

  • Clone a virtual machine directly from single ESXi host

    Hi, I want to clone a virtual machine with all the related feathers which the Associate ID (CPU ID, UUID, BIOS ID, etc.) and redeployed on another single ESXi host. It could help me how can I do?

    Hello

    without vCenter server you can accomplish this by check script PowerCLI this useful post below:

    Tools of HL - part 1 - clone a virtual machine without vCenter - notes LucD

    If you want to clone individual disks check this KB:

    VMware KB: Cloning of disks to the individual virtual machine through the ESX/ESXi terminal host

    _________________________________________________________________________________________

    If you have found this device or any other answer useful, please consider to award points. (use the useful or correct)

    Kind regards

    P.

  • rename several virtual machines based on the State of food & VM name

    I'm looking for a powershell script to rename the virtual machine based on the state name and vm power.  For example, I have several virtual machines with a - temp at the end of the name of the server.  Some are turned on and some are turned off.  I am wanting to rename only those who is off

    Thank you

    tjw82

    Assuming that - temp is part of the name

    Get - vm | where {$_.powerstate - eq "poweredoff"} | foreach {Set-machine virtual - VM $_ - name (($_.name).)} Replace ("temp", "delete"))}

  • Several virtual machines are running, but are not in stock

    Hi all

    I hope that someone has experienced this condition and can help.

    Here's what I have.

    I have 13 VMs (Windows-based) I did some tests with the help of EMC RP DR. All servers in a group to ensure consistency with the two LUNs. I was able to failover of the environment on the site of copy and then restore to the original location.

    However, today when I didn't return the case I lost 4 virtual machines.

    -J' have 9 virtual machines I see in vCenter Server

    -The virtual machines that I've "lost" are running, and I can connect using RDP, but they do not appear in vCenter Server

    -Virtual machines are in their records in their data warehouses. When I find the appropriate folder and try to add to the inventory, the option is grayed out. Which suggests that the VM is saved, but when I search I do not see the instance.

    PowerCLI command revealed that the two Mas I have 9 powered on virtual machines. Like I said all 13 virtual machines are running.

    I asked each his vmInventory.xml of the host in the cluster file servers registered on all hosts are 9 instead of 13.

    I have not disassembled the DS during operations of RecoverPoint failover, power off just the virtual machines.

    Fortunately, this environment is not in full production, but which is the right thing.

    Someone at - he encountered this rather odd situation.

    If relevant, Bay storage is EMC VNX and access is any block.

    Kind regards

    no problem you are welcome!

  • Two virtual machines running on a same PC network

    Hi all:

    I'm running both SUSE Linux Virtual Machines (VM) with VMPlayer on a PC with Windows server 2008. The two virtual machines are configured to the bridge at the same host physical ethernet port (bridge mode). Started the first virtual machine can access the network without problem, but the second VM started not. The order from VM has been changed in order to check if it was a configuration problem on one of the virtual machine, but according to the virtual machine that is launched in the first place seems if appropriate switch VMnet0 and block access to the second VM. Defined for each virtual computer LANs are different: 10.2.1.0/24 for a VM and 10.4.1.0/24 for the other.

    Any help will be really appreciated

    Case resolved, I realize that he had a message waiting in the toolbar of VMware by saying * adapter Ethernet0 do not have network connectivity. MAC 00 address: 0C: 29:58:61:19 adapter Ethernet0 is located between reserved address or is being used by another virtual card on your system. *. I just checked on google and found an identical case on the VMware communities with the solution: http://communities.vmware.com/thread/269465?start=0&tstart=0. To make the story short my VMs have been duplicated and used the same MAC address. So just stop a virtual machine and generate a new MAC for him in settings-> settings-> Advance-> ECS

Maybe you are looking for