cjcurts
Contributor
Contributor

PowerCLI - Creating VMs - Grabbing MAC address

Jump to solution

I am trying to create a script that will create x number of VMs, grab the mac address of the e1000 NIC on the newly created VM and then append it to a CSV file.  When I execute my script, I receive an error "Property 'macaddress' cannot be found on this object; make sure it exists and is settable.

What am I doing wrong?

Here is what I have so far:

$newvms = import-csv $importfile

$objreport=@()

$Report = import-csv $importfile | select-object *,macaddress

$newvms | %{


$vname=$($_.Hostname)

New-VM -name $vname -VMhost $vmhost -ResourcePool $pool -DiskMB $disksize -MemoryMB $memsize -NumCpu $cpu -DiskStorageFormat thin -Datastore $datastore -GuestID $os -NetworkName $nic0 -Location $location

$macnic1= Get-NetworkAdapter -vm $vname | where {$_.type -match "e1000"} | select-object MacAddress

#$Report.hostname = $($_.Hostname)
$Report.macaddress = $macnic1

New-NetworkAdapter -VM $vname -NetworkName $nic1 -StartConnected -Type vmxnet3
New-NetworkAdapter -VM $vname -NetworkName $nic2 -StartConnected -Type vmxnet3
$objreport += $Report
}

$objreport | export-csv $outputfile -NoTypeInformation

It seems to work when I change

$Report = import-csv $importfile | select-object *,macaddress

to

$Report = "" | select-object hostname,macaddress

But I need the contents of the $importfile

Thanks in advance

CJ

0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert

Hello, cjcurts-

You can adjust Luc's code a bit so that it exports the original info along with the MAC address.  It would be something like:

## save the imported info
$colVMInfoFromCSV = Import-Csv $importfile
## add a NoteProperty to the info (later to be a "column" at Export-Csv time)
$colVMInfoFromCSV | Add-Member -MemberType NoteProperty macaddress -value $null
$colVMInfoFromCSV | %{
   
$vname = $($_.Hostname)
   
New-VM -name $vname -VMhost $vmhost -ResourcePool $pool -DiskMB $disksize -MemoryMB $memsize -NumCpu $cpu -DiskStorageFormat thin -Datastore $datastore -GuestID $os -NetworkName $nic0 -Location $location

   
$macnic1 = Get-NetworkAdapter -vm $vname | where {$_.type -match "e1000"} | select-object MacAddress

   
## put the MAC address in this "row" of the collection of info (this object in the array of objects)
    $_.macaddress = $macnic1

   
New-NetworkAdapter -VM $vname -NetworkName $nic1 -StartConnected -Type vmxnet3
   
New-NetworkAdapter -VM $vname -NetworkName $nic2 -StartConnected -Type vmxnet3
}

## export the info, which now contains the MAC address info as well as the original info
$colVMInfoFromCSV | Export-Csv $outputfile -NoTypeInformation

That uses the Add-Member cmdlet to add a NoteProperty to the original info, and then populates each "row" with the corresponding MAC address info.  This is actually setting a value for the "macaddress" property for the given custom object.  It then exports all the info to the "outputfile".

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership

Try it like this

$objreport = @()
import-csv $importfile | %{
    $vname = $($_.Hostname)
    New-VM -name $vname -VMhost $vmhost -ResourcePool $pool -DiskMB $disksize -MemoryMB $memsize -NumCpu $cpu -DiskStorageFormat thin -Datastore $datastore -GuestID $os -NetworkName $nic0 -Location $location

   
$macnic1 = Get-NetworkAdapter -vm $vname | where {$_.type -match "e1000"} | select-object MacAddress
   
$Report = "" | Select Hostname,MacAddres     $Report.hostname = $($_.Hostname)     $Report.macaddress = $macnic1
   
New-NetworkAdapter -VM $vname -NetworkName $nic1 -StartConnected -Type vmxnet3
    New-NetworkAdapter -VM $vname -NetworkName $nic2 -StartConnected -Type vmxnet3

   
$objreport += $Report
} $objreport | export-csv $outputfile -NoTypeInformation

The script loops through your input CSV and writes the name and the MAC to an array.

That array is exported to a CSV at the end


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

cjcurts
Contributor
Contributor

That will work if I just want a CSV file with hostname,macaddress. But, I also need all the other contents of my importfile.csv. If I could append the the macaddress to the csv file or if I could "copy" those contents then I would be set.

0 Kudos
mattboren
Expert
Expert

Hello, cjcurts-

You can adjust Luc's code a bit so that it exports the original info along with the MAC address.  It would be something like:

## save the imported info
$colVMInfoFromCSV = Import-Csv $importfile
## add a NoteProperty to the info (later to be a "column" at Export-Csv time)
$colVMInfoFromCSV | Add-Member -MemberType NoteProperty macaddress -value $null
$colVMInfoFromCSV | %{
   
$vname = $($_.Hostname)
   
New-VM -name $vname -VMhost $vmhost -ResourcePool $pool -DiskMB $disksize -MemoryMB $memsize -NumCpu $cpu -DiskStorageFormat thin -Datastore $datastore -GuestID $os -NetworkName $nic0 -Location $location

   
$macnic1 = Get-NetworkAdapter -vm $vname | where {$_.type -match "e1000"} | select-object MacAddress

   
## put the MAC address in this "row" of the collection of info (this object in the array of objects)
    $_.macaddress = $macnic1

   
New-NetworkAdapter -VM $vname -NetworkName $nic1 -StartConnected -Type vmxnet3
   
New-NetworkAdapter -VM $vname -NetworkName $nic2 -StartConnected -Type vmxnet3
}

## export the info, which now contains the MAC address info as well as the original info
$colVMInfoFromCSV | Export-Csv $outputfile -NoTypeInformation

That uses the Add-Member cmdlet to add a NoteProperty to the original info, and then populates each "row" with the corresponding MAC address info.  This is actually setting a value for the "macaddress" property for the given custom object.  It then exports all the info to the "outputfile".

View solution in original post

0 Kudos
cjcurts
Contributor
Contributor

Thanks, Matt. That was my issue. I have been working on this problem for two weeks!

I appreciate you and LucD's help and time.

0 Kudos
jasgrif11
Contributor
Contributor

Great post. I was looking for this.

I'm trying to get the Time sync and upgrade at power cycle options to work with this script but I'm unsure where to integrate it to the actual script.

I want the Vm's to create based on the csv info and then get them to write their Mac address back to the .csv file and finally enable "UpgradeAtPowerCycle" and "SynTimeWithHost". A bonus would be if the script emailed me when completed...

The following works on its own but need help to integrate it with Matt and Lucs script. (sorry new to Powercli since attending Alan and Lucs sessions atVMworld Vegas)

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$vmConfigspec.Tools.syncTimeWithHost = $true

Get-VM | %{
     $_.Extensiondata.ReconfigVM($vmConfigSpec)
}
0 Kudos
LucD
Leadership
Leadership

Try something like this

## Define the ConfigSpec object 
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec
.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$vmConfigspec.Tools.syncTimeWithHost = $true
#
# save the imported info
$colVMInfoFromCSV = Import-Csv $importfile
#
# add a NoteProperty to the info (later to be a "column" at Export-Csv time)
$colVMInfoFromCSV
| Add-Member -MemberType NoteProperty macaddress -value $null$colVMInfoFromCSV | %{     $vname = $($_.Hostname)     $vm = New-VM -name $vname -VMhost $vmhost -ResourcePool $pool -DiskMB $disksize -MemoryMB $memsize -NumCpu $cpu -DiskStorageFormat thin -Datastore $datastore -GuestID $os -NetworkName $nic0 -Location $location
   
$macnic1 = Get-NetworkAdapter -vm $vm | where {$_.type -match "e1000"} | select-object MacAddress
   
## put the MAC address in this "row" of the collection of info (this object in the array of objects)
    $_.macaddress = $macnic1     New-NetworkAdapter -VM $vm -NetworkName $nic1 -StartConnected -Type vmxnet3
    New-NetworkAdapter -VM $vm -NetworkName $nic2 -StartConnected -Type vmxnet3   
   
## Configure the Tools Upgrade Policy and the TimeSync
   
$vm.Extensiondata.ReconfigVM($vmConfigSpec) } ## export the info, which now contains the MAC address info as well as the original info
$colVMInfoFromCSV
| Export-Csv $outputfile -NoTypeInformation


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

0 Kudos
jasgrif11
Contributor
Contributor

Any help appreciated...

My script runs but it doesn't create an VM's. It just appends all the VM information in a new cell called macaddress in my sample.csv file

@{Hostname=testvm;   Destination=esx101; DiskMB=20000; NumCpu=1;   MemoryMB=1024; Datastore=store1; GUESTID=rhel5_64Guest}

Here is my script

$VI_SERVER = "ip of vcenter"
Connect-VIServer $VI_SERVER

$importfile = "C:\sample.csv"
$outputfile = "C:\sample.csv"


## Define the ConfigSpec object
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$vmConfigspec.Tools.syncTimeWithHost = $true

## save the imported info
$colVMInfoFromCSV = Import-Csv $importfile
## add a NoteProperty to the info (later to be a "column" at Export-Csv time)
$colVMInfoFromCSV | Add-Member -MemberType NoteProperty macaddress -value $null$colVMInfoFromCSV | %{
    $vname = $($_.Hostname)
    $vmhost = $($_.Destination)
    $disksize = $($_.DiskMB)
    $cpu = $($_.NumCpu)
    $memsize = $($_.MemoryMB)
    $datastore = $($_.Datastore)
    $os = $($_.GUESTID)


    $vm = New-VM -name $vname -VMhost $vmhost -DiskMB $disksize -MemoryMB $memsize -NumCpu $cpu -Datastore $datastore -GuestID $os
    $macnic1 = Get-NetworkAdapter -vm $vm | where {$_.type -match "e1000"} | select-object MacAddress

    ## put the MAC address in this "row" of the collection of info (this object in the array of objects)
    $_.macaddress = $macnic1
    New-NetworkAdapter -VM $vm -NetworkName $nic1 -StartConnected -Type vmxnet3
    New-NetworkAdapter -VM $vm -NetworkName $nic2 -StartConnected -Type vmxnet3   

    ## Configure the Tools Upgrade Policy and the TimeSync
    $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

## export the info, which now contains the MAC address info as well as the original info
$colVMInfoFromCSV | Export-Csv $outputfile -NoTypeInformation

Here is the contents of my sample.csv

HostnameDestinationDiskMBNumCpuMemoryMBDatastoreGUESTID
testvmesx1012000011024store1rhel5_64Guest
0 Kudos
a_p_
Leadership
Leadership

Maybe setting $outputfile will help.

André

0 Kudos
jasgrif11
Contributor
Contributor

Sorry yes I had that done I uploaded the wrong error and file. I have ammended the post. The script now runs but doesn't create any vms and just creates a new field in my sample.csv file called macaddress (as expected) but then it jsut dumps the VM info into this field.

0 Kudos