Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-10-08
4
Medium Priority
?
927 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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Teach the user how to configure vSphere Replication and how to protect and recover VMs Open vSphere Web Client: Verify vsphere Replication is enabled: Enable vSphere Replication for a virtual machine: Verify replicated VM is created: Recover replica…
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…

824 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