Solved

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

Posted on 2014-10-08
4
719 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
Teach the user how to join ESXi hosts to Active Directory domains Open vSphere Client: Join ESXi host to AD domain: Verify ESXi computer account in AD: Configure permissions for domain user in ESXi: Test domain user login to ESXi host:
This Micro Tutorial walks you through using a remote console to access a server and install ESXi 5.1. This example is showing remote access and installation using a Dell server. The hypervisor is the very first component of your virtual infrastructu…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now