VMware Cloud Community
ctech930
Contributor
Contributor
Jump to solution

Using Get-View Help

All

Thanks for reading this.  I am, what I consider, still new to PowerCli.  I've been using powercli scripts in our enviroment for sometime now and reciently I ran across information on using Get-View to speed up scripts by leveraging the API.   I have been able to implement it with some success and it has speed up some scripts.  Now I have a problem that I do not understand and I hope someone can give me a hand.  I have a situation where I want to use Get-View to speed up the gathering of all powered on VM's and also only return some specific properties (to buy a little more optimization).   I've been able to get that part to work, however, next I need to feed the returned VM's into a Get-Stat command and pull statistics for example Memory COnsumed over the last month.   I do not know how to convert back the returned type so that I can feed it into the Get-Stat and it be accepted.  If I use get-vm and then a for each loop the type is correct and it works fine.    Below is an example of what I am trying to do.

$metric = "mem.usage.average"

$startdate = (Get-Date).AddDays(-1)

$rptint = (Get-StatInterval - Name "Past Day").SamplingPeriods

$vmfilter=@{"Runtime.PowerState"="poweredOn";"Config.Template"="false"}

$vmproperties = "Name","Config.Hardware","Runtime"

$vmsview = Get-View -ViewType “VirtualMachine” -Filter $vmfilter -Property $vmproperties

$stats = Get-Stat -Entity $vmsview -Stat $metric -Start $startdate

Thats just a quick example of the code snippits but I cannot get it to work.  From what I've read I should somehow be able to convert the Virtual Machine views stored in $vms back to a useful type that I can pass to the Get-Stat.   And then I can use the views as well as the stats to generate a report with specific items.

Thanks

Joe

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It looks as if there might be a problem with the ViView parameter in PowerCLI 5

With the MoRef parameter it seems to work.

Try this variation

$metric = "mem.usage.average" 
$startdate
= (Get-Date).AddDays(-1) $rptint = (Get-StatInterval - Name "Past Day").SamplingPeriods $vmfilter = @{"Runtime.PowerState"="poweredOn";"Config.Template"="false"} $vmproperties = "Name","Config.Hardware","Runtime"
$vmsview
= Get-View -ViewType VirtualMachine -Filter
$vmfilter -Property $vmproperties
$vms
= Get-VIObjectByVIView -MoRef ($vmsview | %{$_.MoRef}) $stats = Get-Stat -Entity $vms -Stat $metric -Start $startdate


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

You have to use the Get-ViObjectByVIView cmdlet for that.

Like this for example

$metric = "mem.usage.average" 
$startdate
= (Get-Date).AddDays(-1) $rptint = (Get-StatInterval - Name "Past Day").SamplingPeriods $vmfilter = @{"Runtime.PowerState"="poweredOn";"Config.Template"="false"} $vmproperties = "Name","Config.Hardware","Runtime"
$vmsview
= Get-View -ViewType VirtualMachine -Filter
$vmfilter -Property $vmproperties
$vms
= Get-VIObjectByVIView -VIView $vmsview
$stats
= Get-Stat -Entity $vms -Stat $metric -Start $startdate


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ctech930
Contributor
Contributor
Jump to solution

Thanks for the answer LucD.    So I took your example (hopefully I did it right), and I am still getting an error.   Here is the exact script I am testing with.   Minus the connect-viserver which is implied before the script is executed.

function LoadSnapin{
  param($PSSnapinName)
  if (!(Get-PSSnapin | where {$_.Name   -eq $PSSnapinName})){
    Add-pssnapin -name $PSSnapinName
  }
}

$WarningPreference="SilentlyContinue"
LoadSnapin -PSSnapinName   "VMware.VimAutomation.Core"


$metric = "mem.usage.average"
$startdate = (Get-Date).AddDays(-1)
$rptint = (Get-StatInterval -Name "Past Month").SamplingPeriodSecs
$vmfilter = @{"Runtime.PowerState"="poweredOn";"Config.Template"="false"}
$vmproperties = "Name","Config.Hardware","Runtime"

$vmsview = Get-View -ViewType “VirtualMachine” -Filter $vmfilter -Property $vmproperties

$vms = Get-VIObjectByVIView -VIView $vmsview

$stats = Get-Stat -Entity $vms -Stat $metric -Start $startdate -IntervalSecs $rptint

Errors:

Get-VIObjectByVIView : 11/16/2011 1:31:09 AM    Get-VIObjectByVIView        Object reference not set
to an instance of an object.   
At C:\Users\zzRouilJo\Desktop\test-perf1.ps1:18 char:28
+ $vms = Get-VIObjectByVIView <<<<  -VIView $vmsview
    + CategoryInfo          : NotSpecified: (:) [Get-VIObjectByVIView], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Comm
   ands.DotNetInterop.GetVIObjectByVIViewCommand

Get-Stat : 11/16/2011 1:31:09 AM    Get-Stat        Value cannot be found for the mandatory paramete
r Entity   
At C:\Users\zzRouilJo\Desktop\test-perf1.ps1:20 char:18
+ $stats = Get-Stat <<<<  -Entity $vms -Stat $metric -Start $startdate -IntervalSecs $rptint
    + CategoryInfo          : NotSpecified: (:) [Get-Stat], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Comm
   ands.GetViStats

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if there might be a problem with the ViView parameter in PowerCLI 5

With the MoRef parameter it seems to work.

Try this variation

$metric = "mem.usage.average" 
$startdate
= (Get-Date).AddDays(-1) $rptint = (Get-StatInterval - Name "Past Day").SamplingPeriods $vmfilter = @{"Runtime.PowerState"="poweredOn";"Config.Template"="false"} $vmproperties = "Name","Config.Hardware","Runtime"
$vmsview
= Get-View -ViewType VirtualMachine -Filter
$vmfilter -Property $vmproperties
$vms
= Get-VIObjectByVIView -MoRef ($vmsview | %{$_.MoRef}) $stats = Get-Stat -Entity $vms -Stat $metric -Start $startdate


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ctech930
Contributor
Contributor
Jump to solution

Thanks.

That got it!   Just a note, I'm not using PowerCLi 5 still using 4.1 and got that error.  SO possibly a problem in earlier version as well?

Joe

0 Kudos
jkb5054
Contributor
Contributor
Jump to solution

Hi all,

Having some trouble with Get-View myself. I want to only get-view for a particular Datacenter. Or even, attach the relative datacenter to the VM so we can sort by DC.

I also want to send the report in an attachment, but that is a secondary concern.

The last two @{N lines are fields added to the Vm properties, but I am unsure how to get those out properly.

$DC = Get-Datacenter "DataCenter1"

Get-View $DC -ViewType VirtualMachine | Select Name,

        @{N="Powerstate";E={$_.runtime.PowerState}},

        @{N="Tools version";E={$_.Guest.ToolsVersion}},       

        @{N="Tools version status";E={$_.Guest.ToolsVersionStatus}},

        @{N="Tools running status";E={$_.Guest.ToolsRunningStatus}},

        @{N="Operating System";E={$_.Summary.Config.GuestFullName}}     | `

        #@{N="Resp_Mgr";E={$_.Get-VMGuest | Resp_Mgr}},

        #@{N="Contact";E={$_.Get-VMGuest | Contact}}   

       Export-Csv "C:\ToolsReport.csv" -NoTypeInformation

#$msg = new-object Net.Mail.MailMessage

#$smtp = new-object Net.Mail.SmtpClient($smtpServer)

#$msg.From = $ReportFrom

#$msg.To.Add($ReportTo)

#$msg.Bcc.Add("bob")

#$msg.Subject = "VMware Tools upgrade complete on the following VM list "

#$msg.Body = ($report)

#$smtp.Send($msg)

#$msg.Dispose()

Thanks for the help in advance!

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can get all the virtual machines (and templates) for a given datacenter using the Get-View cmdlet as follows:

$Datacenter = Get-View -ViewType Datacenter -Filter @{"Name"="Datacenter1"}

Get-View -ViewType VirtualMachine -SearchRoot $Datacenter.MoRef


The Resp_Mgr and Contact fileds are probably annotations. You can use the Get-Annotation cmdlet to retrieve these. It will be easier if you use the Get-VM cmdlet instead of the Get-View cmdlet. Like this:

Get-Datacenter "DataCenter1" | Get-VM | Select-Object -Property Name,
  PowerState,
  @{N="Tools version";E={$_.ExtensionData.Guest.ToolsVersion}},        
  @{N="Tools version status";E={$_.ExtensionData.Guest.ToolsVersionStatus}},
  @{N="Tools running status";E={$_.ExtensionData.Guest.ToolsRunningStatus}},
  @{N="Operating System";E={$_.ExtensionData.Guest.GuestFullName}},
  @{N="Resp_Mgr";E={($_ | Get-Annotation |Where-Object {$_.Name -eq "Resp_Mgr"}).Value}},
  @{N="Contact";E={($_ | Get-Annotation |Where-Object {$_.Name -eq "Contact"}).Value}} | ` 
  Export-Csv -Path "C:\ToolsReport.csv" -NoTypeInformation -UseCulture

Send-MailMessage -From $ReportFrom -To $ReportTo -Bcc "bob" `
-Subject "VMware Tools upgrade complete on the following VM list " `
-Attachment C:\ToolsReport.csv -SmtpServer smtpserver.yourdomain.com

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos