Hi All,
Getting error for VM Datastore Name, below is the script
$report = @()
$vms = Get-VM rpt01 | Get-View
foreach ($vm in $vms)
{
foreach($dev in $vm.Config.Hardware.Device){
if(($dev.gettype()).Name -eq "VirtualDisk"){
if(($dev.Backing.CompatibilityMode -eq "physicalMode") -or ($dev.Backing.CompatibilityMode -eq "virtualMode")){
$row = "" | select Cluster, Folder, VMHost, VMName, IP, Powerstate, HDMode, HDName, HDsize, LUN, HDDisplayName, VMDataS
#$row = "" | select VMHost, VMName, HDName, LUN, HDsize, HDMode, HDDisplayName, IP, Cluster,Folder
$row.VMName = $vm.Name
$esx = Get-View $vm.Runtime.Host
$row.VMHost = ($esx).Name
$row.Powerstate = $vm.Runtime.PowerState
$row.VMDataS = (Get-Datastore -VM $vm | Select -Last 1).Name
$row.HDName = $dev.DeviceInfo.Label
$row.HDMode = $dev.Backing.CompatibilityMode
$row.HDSize = [system.math]::Round($dev.CapacityInKB / 1048576)
$row.HDDisplayName = ($esx.Config.StorageDevice.ScsiLun | where {$_.Uuid -eq $dev.Backing.LunUuid}).CanonicalName
$lun = Get-ScsiLun -VmHost $row.VMHost -CanonicalName $row.HDDisplayName
$row.LUN = $lun.RuntimeName.SubString($lun.RuntimeName.LastIndexof("L")+1)
$row.IP = $vm.guest.IpAddress
$row.Cluster = (get-view -ViewType clustercomputeresource | ?{$_.moref -match $esx.parent.value}).name
$row.Folder = (get-view -ViewType Folder | ?{$_.moref -match $vm.parent.value}).name
$report += $row
}
}
}
}
getting below error
Get-Datastore : Cannot bind parameter 'RelatedObject'. Cannot convert the "VMware.Vim.VirtualMachine" value of type "VMware.Vim.VirtualMachine" to type
"VMware.VimAutomation.ViCore.Types.V1.RelatedObject.DatastoreRelatedObjectBase".
At C:\VM_RDM_Info.ps1:16 char:39
+ $row.VMDataS = (Get-Datastore -VM $vm | Select -Last 1).Name
+ ~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Datastore], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatastore
Get-Datastore : Cannot bind parameter 'RelatedObject'. Cannot convert the "VMware.Vim.VirtualMachine" value of type "VMware.Vim.VirtualMachine" to type
"VMware.VimAutomation.ViCore.Types.V1.RelatedObject.DatastoreRelatedObjectBase".
At C:\VM_RDM_Info.ps1:16 char:39
+ $row.VMDataS = (Get-Datastore -VM $vm | Select -Last 1).Name
+ ~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Datastore], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatastore
Try replacing this:
$row.VMDataS = (Get-Datastore -VM $vm | Select -Last 1).Name
with this:
$row.VMDataS = (Get-Datastore -Id $vm.Datastore | select -last 1).name
The problem is that Get-Datastore -vm expects the -VM parameter to be a VM type rather than the VMs' View type that is being referenced because of the $vms = get-vm rpt01 | get-view.
Try replacing this:
$row.VMDataS = (Get-Datastore -VM $vm | Select -Last 1).Name
with this:
$row.VMDataS = (Get-Datastore -Id $vm.Datastore | select -last 1).name
The problem is that Get-Datastore -vm expects the -VM parameter to be a VM type rather than the VMs' View type that is being referenced because of the $vms = get-vm rpt01 | get-view.