Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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 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…

730 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