I am trying to deploy 20 vms in vcenter using vsphere powerCLI, instead of prompting for vmname/hostname again and again or passing params for 20 times I am looking for passing the 20 vm names from a file.
Asked
Active
Viewed 415 times
-1
-
`Get-Content`, `Import-Csv`? There are plenty of ways to read in from a text file. These are basic commands so it's hard to say more than that without understanding where you might have trouble integrating them. – Chris Dent Aug 18 '16 at 10:40
-
using Get-Content i can read the line in a CSV file, but i want to use each line as a input/variable – Docgyan Aug 18 '16 at 11:26
-
Create a loop based on the file content. `Get-Content | ForEach-Object { New-VM $_ }`, or take it a step further and make `New-VM` accept pipeline input. However, you've no code example which makes it very hard to provide specific advice. – Chris Dent Aug 18 '16 at 11:32
2 Answers
1
You can do this in a quick one-liner, create a csv file with the headings and values you want to use like so:
VMName,Hostname
VM001,Server01
VM002,Server02
VM003,Server03
Then use Import-CSV and Foreach to loop through the file and run your command (New-VM used as an example) with the fields from each row.
Import-Csv C:\folder\file.csv | Foreach { New-VM -VMName $_.VMName -Hostname $_.Hostname }

henrycarteruk
- 12,708
- 2
- 36
- 40
-
Thank you guys.. am new to powershell and above comments helped me a lot. – Docgyan Aug 22 '16 at 06:05
0
$1 = Get-content PATH
$1[ROWNUMBER]
That would be how to import the file, and then select a row number in it.
Seeing you wanted to use each line you could do something like this:
$1 = Get-content PATH
Foreach($Row in $1){
New-VM $row
}

Jakodns
- 314
- 1
- 9