Replace the first 2 lines
$cluster=Get-View-ViewType ClusterComputeResource -Property Name -Filter @{Name='cluster'}
Get-View-ViewType VirtualMachine -SearchRoot $cluster.MoRef-PipelineVariable vm |
by
Get-View-ViewType VirtualMachine -Filter @{Name='vmname'} -PipelineVariable vm |