Hello people,
I am trying to learn powercli. I am using below commads to get some information for VMs. Looks like the results are not appending into csv file. csv file shows the result of last VM in import-csv file.
Q1 - Can you please help in indentifying what is wrong in below commands?
Q2 - How can i use one export-csv command to get output of both (get-vm and get-cluster) lines?
$vmname = Import-csv "C:\vmname.csv"
foreach ($_.name in $vmname){
get-vm $_.name | Select "Name","Host"|export-csv -UseCulture -NoTypeInformation C:\vmname-host-info.csv
get-cluster -vm $_.name | Select "Name" |export-csv -UseCulture -NoTypeInformation C:\vmcluster-info.csv}
Thanks!
You are overwritten the CSV file with each iteration of the loop.
Why do you want to produce 2 CSV files ? Isn't it easier to keep all the info in 1 file ?
The you could do something like this
$vmname = Import-csv "C:\vmname.csv"
&{foreach ($_.name in $vmname){
get-vm $_.name | Select "Name","Host",@{N="Cluster";E={Get-Cluster -VM $_ | Select -ExpandProperty Name}}}} | export-csv -UseCulture -NoTypeInformation C:\vm-info.csv
But better is to use the more PowerShell way
$vmname = Import-csv "C:\vmname.csv"
Get-VM -Name $vmname |
Select Name,Host,@{N="Cluster";E={Get-Cluster -VM $_ | Select -ExpandProperty Name}} |
export-csv -UseCulture -NoTypeInformation C:\vm-info.csv
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You are overwritten the CSV file with each iteration of the loop.
Why do you want to produce 2 CSV files ? Isn't it easier to keep all the info in 1 file ?
The you could do something like this
$vmname = Import-csv "C:\vmname.csv"
&{foreach ($_.name in $vmname){
get-vm $_.name | Select "Name","Host",@{N="Cluster";E={Get-Cluster -VM $_ | Select -ExpandProperty Name}}}} | export-csv -UseCulture -NoTypeInformation C:\vm-info.csv
But better is to use the more PowerShell way
$vmname = Import-csv "C:\vmname.csv"
Get-VM -Name $vmname |
Select Name,Host,@{N="Cluster";E={Get-Cluster -VM $_ | Select -ExpandProperty Name}} |
export-csv -UseCulture -NoTypeInformation C:\vm-info.csv
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you! LucD .
As i am learningpowercli, could you please help understanding what does ,@{N= and E= means? Does E means execute and N= means put a name in new column? Can it be a different letter?
Can you point me to a reference guide for this.
Thanks!
That construction is what is called a "calculated property".
You can use it on the Select-Object cmdlet as a value on the -Property parameter.
It allows you to not only select properties from the object that is passed to the Select-Object, but do calculations.
Perhaps an example will make it a bit easier to understand.
Get-VM | Select-Object -Property Name
This will get all the VM and display the Name property of each VM object.
Get-VM | Select-Object -Property Name,@{Name="Memory in GB";Expression={$_.MemoryMB/1KB}}
Now we added a calculated property, and yes N stands for Name (of the property) and E stands for expression.
This takes one of the properties (MemoryMB) and converts it to GB, by dividing the value by 1KB (which is a PowerShell constant and stands for 1024).
If you need some learning resources for PowerShell and PowerCLI, there are a number of pointers in My PS Library post.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you! sir.