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
}
I would do that like this
$Details | Add-Member -Name Networkstatus -Value ([string]::Join(',', (Get-VIObjectByVIView -MoRef $vm.MoRef | Get-NetworkAdapter | %{$_.ConnectionState.Connected}))) -Membertype NoteProperty
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear LucD,
Thank for your quick replay.
can you help me out to get VlanID syntax.
You better use the Get-VM instead of the Get-View in that case.
The script becomes something like this
$MyCollection = @()
$AllVMs = Get-VM | where {-not $_.Extensiondata.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.Extensiondata.Runtime.Host).Name -Membertype NoteProperty
$Details | Add-Member -Name Datastore -Value ([string]::Join(';',($vm.Extensiondata.Datastore | %{Get-View $_} | %{$_.Name}))) -Membertype NoteProperty
$Details | Add-Member -Name DSCapacity -Value ([string]::Join(';',($vm.Extensiondata.Datastore | %{Get-View $_} | %{"{0:f1}" -f ($_.Summary.Capacity/1MB)}))) -Membertype NoteProperty
$Details | Add-Member -Name Networkstatus -Value ([string]::Join(',', (Get-NetworkAdapter -VM $vm | %{$_.ConnectionState.Connected}))) -Membertype NoteProperty
$Details | Add-Member -Name VlanId -Value ([string]::Join(',', (Get-VirtualPortGroup -VM $vm | %{$_.VlanId}))) -Membertype NoteProperty
$MyCollection += $Details
}
$MyCollection
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear Lucd,
Where did you get the "$vmImpl" var from ?
Sorry, that was a leftover from a prevous version of the script.
I updated the code.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear Lucd,
Script Which posted i excuted on our setup, From that i able to get the of Name, VlanID and NetworkStatus, But not able to get the values of Hostname and DSCapacity.
################### ERROR ########################
Exception calling "Join" with "2" argument(s): "Value cannot be null.
Parameter name: value"
At C:\test.ps1:10 char:66
+ $Details | Add-Member -Name DSCapacity -Value ([string]::Join <<<< (';',($vm.Extensiondata.Datastore | %{Get-View $_} | %{"{0:f1}" -f ($_.Summary.Capacity/1MB)}))) -Membertype NoteProperty
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\test.ps1:9 char:54
+ $Details | Add-Member -Name Host -Value (Get-View <<<< $vm.Extensiondata.Runtime.Host).Name -Membertype NoteProperty $Details | Add-Member -Name Datastore -Value ([string]::Join(';',($
+ CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView
Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\test.ps1:10 char:112
+ $Details | Add-Member -Name DSCapacity -Value ([string]::Join(';',($vm.Extensiondata.Datastore | %{Get-View <<<< $_} | %{"{0:f1}" -f ($_.Summary.Capacity/1MB)}))) -Membertype NoteProperty
+ CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView
Exception calling "Join" with "2" argument(s): "Value cannot be null.
Parameter name: value"
At C:\test.ps1:10 char:66
#############################################
The out put
Name : Windows 2003 Template
Networkstatus : False
VlanId : 0
Looks like you don't have the Extensiondata property.
Which PowerCLI version are you using ?
Do a
Get-PowerCLIVersion
What does this say ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear LucD,
#######################################
VMware vSphere PowerCLI 4.0 U1 build 208462
#########################################
That explains it, you can install PowerCLI 4.1U1 and then the previous script will work.
Or you can use the following variation on the script (which will also work with PowerCLI 4.0U1).
$MyCollection = @() $AllVMs = Get-View -ViewType VirtualMachine | where {-not $_.Config.Template} foreach ($VM in $AllVMs) { $vmImpl = Get-VIObjectByVIView -MORef $vm.MoRef $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 $Details | Add-Member -Name Networkstatus -Value ([string]::Join(',', (Get-NetworkAdapter -VM $vmImpl | %{$_.ConnectionState.Connected}))) -Membertype NoteProperty $Details | Add-Member -Name VlanId -Value ([string]::Join(',', (Get-VirtualPortGroup -VM $vmImpl | %{$_.VlanId}))) -Membertype NoteProperty $MyCollection += $Details
} $MyCollection
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear LucD,
Very thankfull for your quick inputs.