VMware Cloud Community
MarcSmid
Contributor
Contributor

Export VM's created today in specified cluster

Hello,

I am new to powershell and been experimenting with it. To automate a common task i need  csv file with:

Name (vmname)

Mac (vm mac address).

The vm's listed need to be in a specified cluster and the creation date needs to be de day the script is run.

Listing vm's and mac's is do-able for my level, but combining that with the cluster and date requirements is too much for me.

Can anybody help me ?

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership

Sorry, I'm not sure I get the request 100%.

What I understan:

  • you have a CSV or text file that contains VM names and the MAC address of the VMs
  • you want to check if these VMs are running in a specific cluster
  • if not, the VM shall be moved to that cluster

What I don't get is what you want to do with the creation date.

Is that a Custom Attribute you want to set ?


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

Reply
0 Kudos
MarcSmid
Contributor
Contributor

I would like to scan a specific cluster for vm's created "today".

And export their vmname and mac-adress to a csv

Reply
0 Kudos
VinithMenon
VMware Employee
VMware Employee

You  can use this to extract the info.

Get-Cluster -Name <ClusterName> | Get-VM | % {

Get-VIEvent $_.name | ?{($_.createdtime).ToShortDateString() -eq (get-date).ToShortDateString()}  | select -First 1 | select @{l='VMName';e={ $_.vm| select -ExpandProperty name}},createdtime,

@{l='MACAddress';e={ Get-VM ($_.vm| select -ExpandProperty name) | Get-NetworkAdapter | select -ExpandProperty macaddress}}

}

1.png

MarcSmid
Contributor
Contributor

Vinithmenon,

Thank you for your effort, it is almost what i want.

Your code shaows all vm's and creatd date. But i need a list of vm created on the day this script will run, exported to csv with name.macaddress.

So:

Query cluster for vm's created today

Export name.macaddress

Can you help with that ?

Reply
0 Kudos
VinithMenon
VMware Employee
VMware Employee

Can you try this, this should do it Smiley Happy

Get-Cluster -Name <ClusterName> | Get-VM | % {

Get-VIEvent $_.name | Sort-Object createdtime | select -First 1 | ?{($_.createdtime).ToShortDateString() -eq (get-date).ToShortDateString()}  | select -First 1 | select @{l='VMName';e={ $_.vm| select -ExpandProperty name}},createdtime,

@{l='MACAddress';e={ Get-VM ($_.vm| select -ExpandProperty name) | Get-NetworkAdapter | select -ExpandProperty macaddress}}

}

1.png

MarcSmid
Contributor
Contributor

Not quite there yet.

Can i get it with this?

$CDT = Get-Date

Get-Cluster -Name MyCluster | Get-VM

Get-VIEvent -Types Info -Start $CDT.AddDays(-1) -Finish $CDT |

(this way i should get all vm's in a cluster, created today)

And then list vmname, macaddress ?

I don't need the creation dat jsut and export of vm's created today, with name and mac address

Reply
0 Kudos
LucD
Leadership
Leadership

Try something like this, it will list all VMs in the cluster that were created since midnight today.

$clusterName = 'MyCluster'

$start = Get-Date -Hour 0 -Minute 0 -Second 0

$vms = Get-Cluster -Name $clusterName | Get-VM

Get-VIEvent -Entity $vms -Start $start -MaxSamples ([int]::MaxValue) |

where{$_ -is [VMware.Vim.VmCreatedEvent]} |

Select CreatedTime,

  @{N='Name';E={$_.VM.Name}},

  @{N='MAC';E={(Get-View -Id $_.VM.VM).Config.Hardware.Device |

                where{$_ -is [VMware.Vim.VirtualEthernetCard]} |

                Select -ExpandProperty MacAddress}}


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

Reply
0 Kudos
MarcSmid
Contributor
Contributor

Very cool ! I had to change the event, as the vm's are rolled out from a template.

This is what is working for me:

Get-Module -ListAvailable | Import-Module

$clusterName = "mycluster"

$start = Get-Date -Hour 0 -Minute 0 -Second 0

$vms = Get-Cluster -Name $clusterName | Get-VM

Get-VIEvent -Entity $vms -Start $start -MaxSamples ([int]::MaxValue) |

where{$_ -is [VMware.Vim.VmBeingDeployedEvent]} |

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

  @{N="ClientId" ;E={(Get-View -Id $_.VM.VM).Config.Hardware.Device |

                where{$_ -is [VMware.Vim.VirtualEthernetCard]} |

                Select -ExpandProperty MacAddress}} | select Name,ClientID | Export-Csv servers.csv -notype

How do i format the output for CliendtId, so that the : signs are removed and the mac is formatted as 0011223344E1F6 ? Instead of 00:11:22:33:44:E1:F6

I use the output of the script to create Reservations in DHCP and set device options.

Reply
0 Kudos
LucD
Leadership
Leadership

Something like this

$mac = '00:11:22:33:44:E1:F6'

$mac.Split(':') -join ''


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

Reply
0 Kudos
MarcSmid
Contributor
Contributor

right now the export csv is filled with: Name, ClientId.

But the client id is formatted as a mac-address, it needs to be formatted as text without the colons.

Reply
0 Kudos
LucD
Leadership
Leadership

Replace this

select Name,ClientID

by this

select Name,@{N='ClientID';E={$_.ClientID.Split(':') -join ''}}


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

Reply
0 Kudos