Hi Guys,
Can someone help me and integrate an import csv script (which contains a list of VMs) and ran the below script against the imported VMs to create an output csv file? Thanks alot in advance.
Connect-VIServer xxxxxxxx
$date = Get-Date -format M-d-yyyy
. 'c:\Users\xxxx\test\test2\scripts\xxx scripts\Get-FolderPath.ps1'
$folders = Get-Folder -type vm | Get-FolderPath | Sort-Object Id
function Set-Path{
param($Object)
foreach ($folder IN $folders){
if( $folder.Id -eq $Object.Id){
$result = $folder.Path
break
}
}
$result
}
Get-VM vditest-1.xxx.internal | Select-Object -Property Name,@{N='Datacenter';E={$_|Get-Datacenter}},@{N='Cluster';E={$_.VMHost.Parent}},
NumCPU,MemoryGB,ProvisionedSpaceGB,@{N='Path';E={($_.Folder|Get-FolderPath).Path}},@{N='FolderID';E={$_.folder.Id}} | Export-Csv “C:\test\vm-xxx-$date.csv” -NoTypeInformation -UseCulture
Try something like below script
It assumes your CSV file looks like this
"Name"
"VM1"
"VM2"
The script
Import-Csv vm.csv -UseCulture | %{
Get-VM -Name $_.Name |
Select Name,
@{N='Datacenter';E={Get-Datacenter -VM $_ | Select -ExpandProperty Name}},
@{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},
NumCpu, MemoryGB,
ProvisionedSpaceGB,
@{N='Path';E={
$current = Get-View $_.ExtensionData.Parent
$path = $_.Name
do {
$parent = $current
if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
$current = Get-View $current.Parent
} while ($current.Parent -ne $null)
[string]::Join('\',($path.Split('\')[0..($path.Split('\').Count-2)]))
}},
FolderId
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Not sure what you are trying to accomplish here.
This looks like a collection of random copy/paste from other scripts.
Perhaps you could describe your input, and what you want to see in the report ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
I have a list of VMs from an excel/csv sheet. I'm trying to collect the some info + folderpath + folder Id's using the script i mentioned above.
The input list of VMs should come from the the excel sheet then it should collect something like this.
Name | Datacenter | Cluster | NumCpu | MemoryGB | ProvisionedSpaceGB | Path | FolderID |
vditest-1.xxx.internal | US | servers | 2 | 4 | 20.00145031 | US\servers | Folder-group-v905 |
vditest-2.xxx.internal
vditest-3.xxx.internal
hope this helps clarify what i want to achieve.
Try something like below script
It assumes your CSV file looks like this
"Name"
"VM1"
"VM2"
The script
Import-Csv vm.csv -UseCulture | %{
Get-VM -Name $_.Name |
Select Name,
@{N='Datacenter';E={Get-Datacenter -VM $_ | Select -ExpandProperty Name}},
@{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},
NumCpu, MemoryGB,
ProvisionedSpaceGB,
@{N='Path';E={
$current = Get-View $_.ExtensionData.Parent
$path = $_.Name
do {
$parent = $current
if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
$current = Get-View $current.Parent
} while ($current.Parent -ne $null)
[string]::Join('\',($path.Split('\')[0..($path.Split('\').Count-2)]))
}},
FolderId
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD works like a charm.