Dear Friends,
I have Powercli script which get the information about all VM's, how to include where the VM presented ESX host name and datastore name.
$MyCollection = @()
$AllVMs = Get-View -ViewType VirtualMachine | Where {-not $_.Config.Template}
ForEach ($VM ) {
$Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
}
You can try it like this
$MyCollection = @()
$AllVMs = Get-View -ViewType VirtualMachine | where {-not $_.Config.Template}
foreach ($VM in $AllVMs) {
$Details = New-Object PSObject
$Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
$Details | Add-Member -Name Host -Value (Get-View $vm.Runtime.Host).Name -Membertype NoteProperty
$Details | Add-Member -Name Datastore -Value ([string]::Join(',',($vm.Datastore | %{Get-View $_} | %{$_.Name}))) -Membertype NoteProperty
$MyCollection += $Details
}
$MyCollection
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can try it like this
$MyCollection = @()
$AllVMs = Get-View -ViewType VirtualMachine | where {-not $_.Config.Template}
foreach ($VM in $AllVMs) {
$Details = New-Object PSObject
$Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
$Details | Add-Member -Name Host -Value (Get-View $vm.Runtime.Host).Name -Membertype NoteProperty
$Details | Add-Member -Name Datastore -Value ([string]::Join(',',($vm.Datastore | %{Get-View $_} | %{$_.Name}))) -Membertype NoteProperty
$MyCollection += $Details
}
$MyCollection
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear LucD,
Really Thank very much your quick Reply
LucD,
But it throws error like
[vSphere PowerCLI] C:\tmp> .\ESX-Hostname.ps1
Unexpected token 'in' in expression or statement.
At C:\tmp\ESX-Hostname.ps1:1 char:93
+ $AllVMs = Get-View -ViewType VirtualMachine | where {-not $_.Config.Template}
foreach ($VM in <<<< $AllVMs) { $Details = New-Object PSObject $Details
| Add-Member -Name Name -Value $VM.name -Membertype NoteProperty $Details |
Add-Member -Name Host -Value (Get-View $vm.Runtime.Host).Name -Membertype NoteP
roperty $Details | Add-Member -Name Datastore -Value ([string]::Join(',',($v
m.Datastore | %{Get-View $_} | %{$_.Name}))) -Membertype NoteProperty $MyCol
lection += $Details}$MyCollection
+ CategoryInfo : ParserError: (in:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
You seem to have lost the <CR><LF> while copying the script.
Attached a correct version
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Can we get the out put in CSV format as its shows the out put
Name Host Datastore
---- ---- ---------
USTPA3IFSWS85 ustpa3ifsvm057.nam.pwci... SYS01_DEV01_CL0811,DAT...
Dear LucD,
How can i get string value for datasrote capacity in MB.As you post using that i am getting Datastore name like how can get datastore useage space.
I am new to this how to write string.
Change the last line
$MyCollection
into this
$MyCollection | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try it like this
$MyCollection = @() $AllVMs = Get-View -ViewType VirtualMachine | where {-not $_.Config.Template} foreach ($VM in $AllVMs) { $Details = New-Object PSObject
$Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
$Details | Add-Member -Name Host -Value (Get-View $vm.Runtime.Host).Name -Membertype NoteProperty
$Details | Add-Member -Name Datastore -Value ([string]::Join(';',($vm.Datastore | %{Get-View $_} | %{$_.Name}))) -Membertype NoteProperty $Details | Add-Member -Name DSCapacity -Value ([string]::Join(';',($vm.Datastore | %{Get-View $_} | %{"{0:f1}" -f ($_.Summary.Capacity/1MB)}))) -Membertype NoteProperty $MyCollection += $Details
} $MyCollection
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You again seem to have <CR><LF> problems.
Use the attached version
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Thanks once Again its perfectly working.
Dear LucD,
Can you explain about this string where to use "math" and "join", What does mean "{0:f1} " and wat else can use to get same.
([string]::Join(';',($vm.Datastore | %{Get-View $_} | %{"{0:f1}" -f ($_.Summary.Capacity/1MB)})))
Since there can be more than 1 datastore and since the Export-Csv cmdlet can't handle arrays, I paste the capacity of the different datastores together in 1 string, seperated by a semi-colon.
The [string]::Join function is a .Net function that a PS script can call in this way.
Similarly you can call .Net mathematical functions.
For example the rounding function: [math]::Round($number, 2) which will round the number in $number to 2 decimals.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Works without a problem for me.
Can you attach the version of the script you are using ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The script is ok, runs fine in my environment and shows the hostnames.
The only explanation I now have is what is written in the SDK "The host that is responsible for running a virtual machine. This property is null if the virtual machine is not running and is not assigned to run on a particular host.".
Could it be that all these VMs are powered off or not assigned to a host ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I tested on other Vcenter2 it pulling the data for Host name too but not on the Vcenter1 which sent the output thoug this vcenter having 4000 VMs
Dear LucD,
To get Network Conection status i used below string but i am error.
"$Details | Add-Member -Name Networkstatus -Value ([string]::Join(',',($vm.Network | %{Get-View $_} | %($_.ConnectionState.Connected)))) -Membertype NoteProperty" is this syntax is correct.