VMware Cloud Community
TimothyGaray
Contributor
Contributor
Jump to solution

Retriving MobRef.Value for VirtualMachine

I am working on a PowerCLI script to list locally-stored vs. SAN-stored VMs on a specific host. This script ultimately will create a batch file to kick-off vRanger. A value that vRanger uses for its CLI is found in the vm.mobref.value which basically returns the VM ID such as "vm-12345".

I'm new to PowerCLI scripting. This script does not give me the value at all for the mobrev.value and it takes 30 seconds to run.

$StartTime = get-date
$VCServer = "FA0150"
$VC = Connect-VIServer $VCServer
$FH0100 = @()
$VMStorage = @()
ForEach ($vm in (Get-VM -Location (Get-VMHost "fh0100")))
{
$HDs = Get-HardDisk -vm $vm
$StorageType = "SAN-Stored: "
$VMStorage = "" | Select SType, VM, VMID
ForEach ($hd in $HDs)
{
If ($hd.filename -like "
:storage")
{$StorageType = "Locally-Stored: "}
}
$VMStorage.SType = $StorageType
$VMStorage.VM = $vm.Name
#$vmMR = Get-VM $vm.name | get-view
$VMStorage.VMID = $vm.MoRef.Value
$FH0100 += $VMStorage
}
ForEach ($Output in ($FH0100 | Sort SType, VM))
{
If ($Output.SType -Like "Locally
")
{Write-Host $Output.SType, $Output.VM, $Output.VMID -ForegroundColor yellow}
Else
{Write-Host $Output.SType, $Output.VM, $Output.VMID -ForegroundColor white}
}
Disconnect-VIServer -Confirm:$False
Write-Host ""
Write-Host "Execution Time: ", (new-timespan -start $StartTime)

If I change the one line to:

ForEach ($vm in (Get-VM -Location (Get-VMHost "fh0100*") | Get-View))

I get the information I want but now the script takes 5mins + 30seconds to run.

I am open to suggestions.

Thanks!

-Tim

0 Kudos
1 Solution

Accepted Solutions
Zsoldier
Expert
Expert
Jump to solution

Instead of using the Get-Harddisk cmdlet, do an if statement against $vm.summary.config.vmpathname

K. Chris Nakagaki (Zsoldier)

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier

View solution in original post

0 Kudos
6 Replies
Zsoldier
Expert
Expert
Jump to solution

Try this:

$vms = Get-VM -location (get-vmhost "fh0100*") | get-view

ForEach ($vm in $vms)

The get-vm will run the longest, but after that should fly once you've populated the $vms variable.

K. Chris Nakagaki (Zsoldier)

http://tech.zsoldier.com

Message was edited by: Zsoldier

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
TimothyGaray
Contributor
Contributor
Jump to solution

I tried your suggestion.

Oddly enough, for locally-stored vms, it generates an error. For SAN-stored vms, it works just fine.

Get-HardDisk : Cannot bind parameter 'VM'. Cannot convert "VMware.Vim.VirtualMachine" to "VMware.VimAutomation.Types.Vi
rtualMachine".
At C:\Temp\RangerTest.ps1:11 char:29
+ $HDs = Get-HardDisk -vm <<<< $vm

If I change the one line to: HDs = Get-HardDisk -vm $vm.name then it runs fine but is back to taking 5+mins to run.

0 Kudos
TimothyGaray
Contributor
Contributor
Jump to solution

I take that back... it generates that error for all the vms on FH0100 which then labels them all as SAN-Stored.

0 Kudos
Zsoldier
Expert
Expert
Jump to solution

Instead of using the Get-Harddisk cmdlet, do an if statement against $vm.summary.config.vmpathname

K. Chris Nakagaki (Zsoldier)

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
TimothyGaray
Contributor
Contributor
Jump to solution

Thank you, that did it in 48 seconds.

-Tim

0 Kudos
Zsoldier
Expert
Expert
Jump to solution

Try this:

$StartTime = get-date

$VCServer = "FA0150"

$VC = Connect-VIServer $VCServer

$FH0100 = @()

$VMStorage = @()

$vms = Get-VM -location (get-vmhost "fh0100*") | get-view

ForEach ($vm in $vms)

{

$StorageType = "SAN-Stored: "

$VMStorage = "" | Select SType, VM, VMID

If ($vm.summary.config.vmpathname -like ":storage")

{$StorageType = "Locally-Stored: "}

$VMStorage.SType = $StorageType

$VMStorage.VM = $vm.Name

#$vmMR = Get-VM $vm.name | get-view

$VMStorage.VMID = $vm.MoRef.Value

$FH0100 += $VMStorage

}

ForEach ($Output in ($FH0100 | Sort SType, VM))

{

If ($Output.SType -Like "Locally*")

{Write-Host $Output.SType, $Output.VM, $Output.VMID -ForegroundColor yellow}

Else

{Write-Host $Output.SType, $Output.VM, $Output.VMID -ForegroundColor white}

}

Disconnect-VIServer -Confirm:$False

Write-Host ""

Write-Host "Execution Time: ", (new-timespan -start $StartTime)

K. Chris Nakagaki (Zsoldier)

http://tech.zsoldier.com

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos