I run my script and it creates a bunch of vm's and then writes their hostname and mac address to a .csv file.
However the output looks like this
Hostname macaddress
testvm1 @{MacAddress=00:50:56:bf:00:92}
How do I remove the @{MacAddress=} so that I just get the 00:50:56:bf:00:92 in the output?
Here is part of the script
$macnic1= Get-NetworkAdapter -vm $_.Hostname | where {$_.type -match "e1000"} | select-object MacAddress
$Report.hostname = $($_.Hostname)
$Report.macaddress = $macnic1
$objreport += $Report
}
$objreport | export-csv $outputfile -NoTypeInformation
Many Thanks
J
Ok, I see, you are trying to add a property to the row you read from the input CSV file.
You have to use an Add-Member cmdlet for that.
Something like this
$VI_SERVER = "VCENTER IP"
$importfile = "C:\powercli\sample.csv"
outputfile = "C:\powercli\sample2.csv"
Connect-VIServer $VI_SERVER
$objreport=@() import-csv $importfile | %{ $Report = $_ New-VM -Name $_.Hostname -VMHost $_.Destination -DiskMB $_.DiskMB -MemoryMB $_.MemoryMB -NumCpu $_.NumCpu -Datastore $_.Datastore -guestID rhel5_64Guest $macnic1= Get-NetworkAdapter -vm $_.Hostname | where {$_.type -match "e1000"} | %{$_.MacAddress} $Report.hostname = $($_.Hostname) Add-Member -InputObject $Report -Name MacAddress -Value $macnic1 -MemberType NoteProperty $objreport += $Report
} $objreport | export-csv $outputfile -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The Select-Object cmdlet always returns an object, not a single value.
To avoid this you can fetch the actual value of the property, something like this
$macnic1= Get-NetworkAdapter -vm $_.Hostname | where {$_.type -match "e1000"} | %{$_.MacAddress} $Report.hostname = $($_.Hostname) $Report.macaddress = $macnic1 $objreport += $Report} $objreport | export-csv $outputfile -NoTypeInformation
Note that if your VM has more than 1 NIC, this will result in an array of values and the export to a CSV file will not work.
You can convert all the values in the array to a single string, something like this
$macnic1= Get-NetworkAdapter -vm $_.Hostname | where {$_.type -match "e1000"} | %{$_.MacAddress} $Report.hostname = $($_.Hostname) $Report.macaddress = [string]::Join(',',$macnic1) $objreport += $Report} $objreport | export-csv $outputfile -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
Thanks for that it worked great....
However I noticed I have another problem. If I have one line entry in my .csv file it creates the VM. If I have more than one line entry I get this error.
Property 'hostname' cannot be found on this object; make sure it exists and is
settable.
At C:\powercli\sample.ps1:33 char:9
+ $Report. <<<< hostname = $($_.Hostname)
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Property 'macaddress' cannot be found on this object; make sure it exists and i
s settable.
At C:\powercli\sample.ps1:34 char:9
+ $Report. <<<< macaddress = $macnic1
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
How do i get it to cycle through each line?
Many Thanks
J
Here is my full script
$VI_SERVER = "VCENTER IP"
$importfile = "C:\powercli\sample.csv"
$outputfile = "C:\powercli\sample2.csv"Connect-VIServer $VI_SERVER
$newvms = import-csv $importfile
$objreport=@()$Report = import-csv $importfile | select-object *,macaddress
$newvms | %{
$vname=$($_.Hostname)
New-VM -Name $_.Hostname -VMHost $_.Destination -DiskMB $_.DiskMB -MemoryMB $_.MemoryMB -NumCpu $_.NumCpu -Datastore $_.Datastore -guestID rhel5_64Guest
$macnic1= Get-NetworkAdapter -vm $_.Hostname | where {$_.type -match "e1000"} | %{$_.MacAddress}
$Report.hostname = $($_.Hostname)
$Report.macaddress = $macnic1
$objreport += $Report
}$objreport | export-csv $outputfile -NoTypeInformation
Ok, I see, you are trying to add a property to the row you read from the input CSV file.
You have to use an Add-Member cmdlet for that.
Something like this
$VI_SERVER = "VCENTER IP"
$importfile = "C:\powercli\sample.csv"
outputfile = "C:\powercli\sample2.csv"
Connect-VIServer $VI_SERVER
$objreport=@() import-csv $importfile | %{ $Report = $_ New-VM -Name $_.Hostname -VMHost $_.Destination -DiskMB $_.DiskMB -MemoryMB $_.MemoryMB -NumCpu $_.NumCpu -Datastore $_.Datastore -guestID rhel5_64Guest $macnic1= Get-NetworkAdapter -vm $_.Hostname | where {$_.type -match "e1000"} | %{$_.MacAddress} $Report.hostname = $($_.Hostname) Add-Member -InputObject $Report -Name MacAddress -Value $macnic1 -MemberType NoteProperty $objreport += $Report
} $objreport | export-csv $outputfile -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc works perfect