Hi
Is there an available PowerCli script to get the all folder containers in a datacenter and list all the VMs member on it.
VM list should get also VM information such as VMname, IPaddress, GuestOS, CPU, MEM, Version.
The report should also create a CSV file.
Thanks.
You can do this in a nested loop.
Something like this
$dcName = "MyDC"
&{Get-Datacenter -Name $dcName | %{ foreach($folder in (Get-Folder -Location $_)){ Get-VM -Location $fld |
Select @{N="Folder";E={$folder.Name}},Name,
@{N="OS";E={$_.Guest.OSFullName}},
@{N="IP";E={[string]::Join(',',($_.Guest.IPAddress))}},NumCPU,MemoryGB,Version }}} |
Export-Csv C:\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oops, wrong variable.
I updated the code above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It's a bit more than what you need. Try it
connect-viserver -server "xxx" -user "xxx" -password "xxx"
$report = @()
$vms = Get-View -ViewType "virtualmachine"
foreach($vm in $vms){
foreach($dev in $vm.Config.Hardware.Device){
if(($dev.gettype()).Name -eq "VirtualDisk"){
$row = "" | select VMName, FolderPath, PowerState, HostName, ClusterName, OSVersion, GuestIPAddress, ToolsStatus, NumCPU, MemoryMB, DeviceInfo, CapacityInGB, Datastore
$row.VMName = $vm.Name
$current = Get-View $vm.Parent
$path = $vm.Name
do {
$parent = $current
if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
$current = Get-View $current.Parent
} while ($current.Parent -ne $null)
$row.FolderPath = $path
$row.PowerState = $vm.runtime.powerstate
$row.HostName = (get-view -id $vm.runtime.host).name
$clu = (get-view -id $vm.Runtime.Host).parent
$row.ClusterName = (Get-View -id $clu).name
$row.OSVersion = $vm.summary.guest.guestfullname
$row.GuestIPAddress = $vm.guest.ipaddress
$row.ToolsStatus = $vm.guest.toolsstatus
$row.NumCPU = $vm.config.hardware.numcpu
$row.MemoryMB = $vm.config.hardware.MemoryMB
$row.DeviceInfo = $dev.deviceinfo.label
$row.CapacityInGB = [system.math]::Round($dev.CapacityInKB / 1048576)
$row.Datastore = $dev.backing.filename.split("]")[0].trim("[")
$report += $row
}
}
}
$report | Export-Csv C:\exportvms.csv -NoTypeInformation -UseCulture
The folder called "vm" is a hidden folder.
It contains all the VMs that are stored in the root of your vCenter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Have error:
Get-View : 11/20/2012 10:09:24 AM Get-View Exception of type 'System.OutOfMemoryException' was thrown.
At C:\mywork\VSPHERE SCRIPTING\scripts\get-export.ps1:3 char:16
+ $vms = Get-View <<<< -ViewType VirtualMachine
+ CategoryInfo : NotSpecified: (:) [Get-View], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
You cannot call a method on a null-valued expression.
At C:\mywork\VSPHERE SCRIPTING\scripts\get-export.ps1:6 char:27
+ if(($dev.gettype <<<< ()).Name -eq "VirtualDisk"){
+ CategoryInfo : InvalidOperation: (gettype:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
That looks like a PowerShell error.
Could be that the PC where you run this doesn't have a lot of free memory, or you're running this against a huge environment.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hey
This one works for me now. The scripts can export full VM folder path from root inventory with some VM properties.
Thanks.
filter Get-FolderPath {
$_ | Get-View | % {
$row = "" | select Name, Path
$row.Name = $_.Name
$current = Get-View $_.Parent
# $path = $_.Name # Uncomment out this line if you do want the VM Name to appear at the end of the path
$path = ""
do {
$parent = $current
if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
$current = Get-View $current.Parent
} while ($current.Parent -ne $null)
$row.Path = $path
$row
}
}
$VCServerName = "vcenter.domain.com"
$VC = Connect-VIServer $VCServerName
$VMFolder = "DATACENTER"
$ExportFilePath = "C:\Output\Export-Folder-Member.csv"
$Report = @()
$VMs = Get-Folder -Location $VMFolder | Get-VM
$Datastores = Get-Datastore | select Name, Id
$VMHosts = Get-VMHost | select Name, Parent
ForEach ($VM in $VMs) {
$VMView = $VM | Get-View
$VMInfo = {} | Select VMName,Powerstate,OS,Folder,IPAddress,ToolsStatus,Host,Cluster,Datastore,NumCPU,MemMb
$VMInfo.VMName = $vm.name
$VMInfo.Powerstate = $vm.Powerstate
$VMInfo.OS = $vm.Guest.OSFullName
$VMInfo.Folder = ($vm | Get-Folderpath).Path
$VMInfo.IPAddress = $vm.Guest.IPAddress[0]
$VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
$VMInfo.Host = $vm.VMhost.name
$VMInfo.Cluster = $vm.VMhost.Parent.Name
$VMInfo.Datastore = ($Datastores | where {$_.ID -match (($vmview.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name
$VMInfo.NumCPU = $vm.NumCPU
$VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)
$Report += $VMInfo
}
$Report = $Report | Sort-Object VMName
IF ($Report -ne "") {
$report | Export-Csv $ExportFilePath -NoTypeInformation
}
$VC = Disconnect-VIServer -Confirm:$False