Reputation: 1462
I am trying to iterate the VM names and storing them in PSCustomObject and assign a subnet in order wise in output like the following: VM1 Subnet1 VM2 Subnet2 VM3 Subnet3 VM4 Subnet1 VM5 Subnet2 How to achieve this.
$csv = import-csv C:\ps\sample.csv
$myobject = foreach ($line in $csv){
if ($line.count -gt 0) {
for($i = 0; $i -lt $line.count; $i++){
$count = $i + 1
[pscustomobject]@{
VMName = $line.vmname+$count
Subnet = $line.subnet -split ',' | Get-Random
Sku = $line.sku
os = $line.os
}
}
}
}
$myobject
Thanks!
Upvotes: 1
Views: 58
Reputation: 17007
this code does the job:
$csv = import-csv C:\ps\sample.csv -Delimiter ';'
$myobject = foreach ($line in $csv){
if ($line.count -gt 0) {
for($i = 0; $i -lt $line.count; $i++){
$ar_sub = $line.subnet -split ','
$l_sub = $ar_sub.count
[pscustomobject]@{
VMName = $line.vmname+$count
Subnet = $ar_sub[$i % $l_sub]
Sku = $line.sku
os = $line.os
}
}
}
}
$myobject
result:
VMName Subnet Sku os
------ ------ --- --
appvm1 subnet1 t2.mciro centos
appvm2 subnet2 t2.mciro centos
appvm3 subnet3 t2.mciro centos
appvm4 subnet1 t2.mciro centos
appvm5 subnet2 t2.mciro centos
dbvm1 subnet4 t3.micro centos
dbvm2 subnet5 t3.micro centos
dbvm3 subnet6 t3.micro centos
dbvm4 subnet4 t3.micro centos
Upvotes: 1