Solved

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

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

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.
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
Teach the user how to convert virtaul disk file formats and how to rename virtual machine files on datastores. Open vSphere Web Client: Review VM disk settings: Migrate VM to new datastore with a thick provisioned (lazy zeroed) disk format: Rename a…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

911 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

23 Experts available now in Live!

Get 1:1 Help Now