Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

powercli - get-snapshot, get-vmname get-hostname

Posted on 2014-10-08
4
779 Views
Last Modified: 2014-10-15
Hi guys,

I have this powercli script:

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-14)} | Select-Object VM, Name, Created, SizeMB

I also need the host name, normally this is achieved by get-vm | get-hostname

I also need the datacentre name it is ok,

how can I combine these codes.

thank you in advance.
0
Comment
Question by:Kay
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:asavener
ID: 40369202
Are you saying you want an array variable to return the hostname, snapshot, and datacenter?
0
 

Author Comment

by:Kay
ID: 40370260
yes
0
 

Author Comment

by:Kay
ID: 40370264
I d this code to give me the hostname and the datacentre also, I tried this but it don't work:

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-14)} | Select VM,Name,@{N="SizeGB";E={@([math]::Round($_.SizeGB))}},Created, @{N="VMHost";E={Get-vm $_ | get-vmhost | select name}}
0
 
LVL 28

Accepted Solution

by:
asavener earned 500 total points
ID: 40370647
Well, the easy way would be to use variables, and then populate an array variable.


$report = @()

$vms = Get-VM

ForEach ($vm in $vms) {
     $snapshots = Get-SnapShot -VM $vm
     ForEach ($snapshot in $snapshots) {
          if ($snapshot.created -lt (Get-Date).AddDays(-14)) {
               $row = "" | Select-Object VM, SnapshotName, Created, SizeMB, HostName, DataCenter
               $row.VM = $vm.name
               $row.SnapshotName = $snapshot.name
               $row.created = $snapshot.created
               $row.SizeMB = $snapshot.sizemb
               $row.HostName = get-vmhost -vm $vm
               $row.DataCenter = get-datacenter -vm $vm
               $report += $row
          }
     }
}


Then you can do what you want with the $report array - display on the screen, write to CSV, or whatever.  The Get-VMHost line is a bit slow, I'll grant you.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Teach the user how to use configure the vCenter Server storage filters Open vSphere Web Client:  Navigate to vCenter Server Advanced Settings: Add the four vCenter Server storage filters: Review the advanced settings: Modify the values of the four v…
Teach the user how to install and configure the vCenter Orchestrator virtual appliance Open vSphere Web Client: Deploy vCenter Orchestrator virtual appliance OVA file: Verify vCenter Orchestrator virtual appliance boots successfully: Connect to the …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question