VMware Cloud Community
Tutti21
Contributor
Contributor
Jump to solution

select template via portgroup and datastore

Hello,

I have a Lists of VMs in a CSVfile that I want to create from templates

I have a templates:

"vlan1DS0101"

"vlan1DS0102"

"vlan2DS0101"

"vlan2DS010"

so I want to check in whitch vlan and datastore the VM in the CSVfile is

 

like

 

Name = $row.Name

Datastore = $row.Datastore

$NameTemplate1 = "vlan1DS0101"

$NameTemplate2 = "vlan1DS0102"

$NameTemplate3 = "vlan2DS0101"

$NameTemplate4 = "vlan2DS0102"

 

If{(

      $row.Name = ($row.Portgroup = vlan1) & ($row.Datastore = DS0101) then Template1 = "vlan1DS0101")

       else (

                 If $row.Name = ($row.Portgroup = vlan1) & ($row.Datastore = DS0102) then Template2 = "vlan1DS0102")

     else(

                If $row.Name = ($row.Portgroup = vlan2) & ($row.Datastore = DS0101) then Template3 = "vlan2DS0101")

    else(

            If $row.Name = ($row.Portgroup = vlan2) & ($row.Datastore = DS0102) then Template4 = "vlan2DS0102")

}

somethin like this

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You mean something like this?

I assume that the column header in the CSV is VMName.

Import-Csv -Path .\vmnames.csv -UseCulture |

   ForEach-Object -Process {

   Get-VM -Name $_.VMName |

  Select Name,

   @{N = 'Datastore'; E = {(Get-Datastore -RelatedObject $_).Name}},

   @{N = 'VLAN'; E = {(Get-VirtualPortGroup -VM $_).Name}} |

   ForEach-Object -Process {

   $_ | Add-Member -MemberType NoteProperty -Name 'Template' -Value "$($_.VLAN)$($_.Datastore)" -PassThru

   }

}


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

View solution in original post

12 Replies
LucD
Leadership
Leadership
Jump to solution

You mean something like this?

I assume that the column header in the CSV is VMName.

Import-Csv -Path .\vmnames.csv -UseCulture |

   ForEach-Object -Process {

   Get-VM -Name $_.VMName |

  Select Name,

   @{N = 'Datastore'; E = {(Get-Datastore -RelatedObject $_).Name}},

   @{N = 'VLAN'; E = {(Get-VirtualPortGroup -VM $_).Name}} |

   ForEach-Object -Process {

   $_ | Add-Member -MemberType NoteProperty -Name 'Template' -Value "$($_.VLAN)$($_.Datastore)" -PassThru

   }

}


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

Tutti21
Contributor
Contributor
Jump to solution

is it possilbe to hardcode the templates?

because the template names are somthing like

template-target-vlan1-DS-01-01

and the portgroup is called DBvlan1

and the Datastore DB-01-01

and with these 2 "portgroup" and "datastore" are unique and you know what template to use and then create the VM with the data in thew CSV

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could use a hash table for lookup.

$templateTab = @{

   'DBvlan1-DB-01-01' = 'template-target-vlan1-DS-01-01'

}

Import-Csv -Path .\vmnames.csv -UseCulture |

   ForEach-Object -Process {

   Get-VM -Name $_.VMName |

  Select Name,

   @{N = 'Datastore'; E = {(Get-Datastore -RelatedObject $_).Name}},

   @{N = 'VLAN'; E = {(Get-VirtualPortGroup -VM $_).Name}} |

   ForEach-Object -Process {

   $_ | Add-Member -MemberType NoteProperty -Name 'Template' -Value $templateTab["$($_.VLAN)-$($_.Datastore)"] -PassThru

   }

}


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

0 Kudos
Tutti21
Contributor
Contributor
Jump to solution

thx my the script for all is, but the script is breaking

in my csv there are

pastedImage_0.png

"$contact = "WTSserver"

$Funktion = "WTSserver"

foreach ($row in Import-csv "U:\vmwarescript\WTS.csv" -UseCulture) {

   $sVM = @{

   Template = $templateTab

   Name = $row.Name

   Datastore = $row.Datastore

   VMHost = $row.VMHost

   Location = $row.Folder

   }

    $templateTab = @{

      'VDI-768-VDI-ENTW-00' = 'template1'
      'WTS-354-VDI-ENTW-01' = 'template2'

   }

#   Import-Csv -Path .\vmnames.csv -UseCulture |

#   ForEach-Object -Process {

#   Get-VM -Name $_.Name |

#  Select Name,

   @{N = 'Datastore'; E = {(Get-Datastore -RelatedObject $_).Name}},

   @{N = 'Portgroup'; E = {(Get-VirtualPortGroup -VM $_).Name}} |

   ForEach-Object -Process {

   $_ | Add-Member -MemberType NoteProperty -Name 'Template' -Value $templateTab["$($_.Portgroup)-$($_.Datastore)"] -PassThru

   }

}

   Stop-VM $row.Name -Confirm:$false
  
   remove-VM $row.Name -DeletePermanently -Confirm:$true


   $vm = New-VM @sVM

   Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -MacAddress $row.MAC -NetworkName $row.Portgroup -Confirm:$false

   Set-Annotation -Entity $vm -CustomAttribute Ansprechpartner -Value $Ansprechpartner

   Set-Annotation -Entity $vm -CustomAttribute Funktion  -Value $Funktion

   Set-Annotation -Entity $vm -CustomAttribute Inventar -Value $row.Inventar

   Start-VM $row.Name}"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Breaking in what sense?

Any error messages?


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

0 Kudos
Tutti21
Contributor
Contributor
Jump to solution

PS U:\vmwarescript> [Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'; & 'U:\vmwarescript\Create from CSV.ps1'
New-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.Template' required by parameter 'Template'. Specified method is not supported.
At U:\vmwarescript\Create from CSV.ps1:63 char:17
+    $vm = New-VM @sVM
+                 ~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-VM], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM
 
Get-NetworkAdapter : Cannot validate argument on parameter 'VM'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At U:\vmwarescript\Create from CSV.ps1:65 char:27
+    Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -MacAddress $row.M ...
+                           ~~~
    + CategoryInfo          : InvalidData: (:) [Get-NetworkAdapter], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetNetworkAdapter
 
Set-Annotation : Cannot validate argument on parameter 'Entity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At U:\vmwarescript\Create from CSV.ps1:67 char:27
+    Set-Annotation -Entity $vm -CustomAttribute Ansprechpartner -Value ...
+                           ~~~
    + CategoryInfo          : InvalidData: (:) [Set-Annotation], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAnnotation

 

 

and some more

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are assigning the complete hash table to the Template parameter.

It is the idea that you pick one based on the key, that is composed from the datastore and the portgroup.


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

0 Kudos
Tutti21
Contributor
Contributor
Jump to solution

Get-Template : A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'.

At U:\vmwarescript\Create from CSV.ps1:29 char:5

+     Template = $templateTab

+     ~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-Template], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetTemplate

Only one error now and have the table now there is it possible to give out what template it used just for testing not need for the real thing

   $sVM = @{

  

   Name = $row.Name

   Datastore = $row.Datastore

   VMHost = $row.VMHost

   Location = $row.Folder

   }

    Template = $templateTab

    $templateTab = @{

      'VDI-768-VDI-ENTW-00' = 'template1'

      'WTS-354-VDI-ENTW-01' = 'template2'

   }

#   Import-Csv -Path "U:\vmwarescript\WTS.csv" -UseCulture |

#  ForEach-Object -Process {

#   Get-VM -Name $_.Name |

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are still assigning the hash table to the Template parameter.


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

0 Kudos
Tutti21
Contributor
Contributor
Jump to solution

so I just did command out the

#  Template = $templateTab

I dont know what the clone from the template is doning but the cloned VMs have 1 CPU 0.25 GB RAM and the Templates both have 2 or 4 CPUs and 4 or 8 GB RAM

at least no error but the wrong configurations seems like it cant find my templates and then just do a default one.

but all the parameters from the CSV are set.

so what is the right way to call the right template

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'll give you an updated version of your script.

Hold on.


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

0 Kudos
Tutti21
Contributor
Contributor
Jump to solution

  Template = $row.Template

 

I got it now thanks

0 Kudos