VMware Cloud Community
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Confirm snapshots have been scheduled

Hello,

I have a simple script which schedules several snapshots of VMs from a .csv file. I'm able to see that the tasks have been creating by getting a list of all scheduled tasks:

Get-VM $VM.Name | (Get-View ScheduledTaskManager).ScheduledTask | %{(Get-View $_).Info} | Select Name,NextRunTime

How do I only get scheduled tasks for the VMs in the csv file? I'm thinking something along these lines:

$VMs = Import-Csv E:\output\VMs.csv

foreach ($VM in $VMs){
     Get-VM $VM.Name | (Get-View ScheduledTaskManager).ScheduledTask | %{(Get-View $_).Info} | Select Name,NextRunTime
}

However this does not work. Any suggestions?
Thanks.

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$VMs = Import-Csv E:\output\VMs.csv

$vmObj = Get-VM -Name $VMs.Name

Get-View -Id (Get-View ScheduledTaskManager).ScheduledTask |
Where-Object { $_.Info.Entity -in $vmObj.Id } |
Select-Object @{N='Name';E={$_.Info.Name}}, @{N = 'NextRunTime'; E = { $_.Info.NextRunTime } }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$VMs = Import-Csv E:\output\VMs.csv

$vmObj = Get-VM -Name $VMs.Name

Get-View -Id (Get-View ScheduledTaskManager).ScheduledTask |
Where-Object { $_.Info.Entity -in $vmObj.Id } |
Select-Object @{N='Name';E={$_.Info.Name}}, @{N = 'NextRunTime'; E = { $_.Info.NextRunTime } }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
hpkishore
Contributor
Contributor
Jump to solution

I`m using the below script to get the scheduled tasks, Also can i have the output of this file "Import-Csv E:\output\VMs.csv" ,more over how to just get only the snapshot scheduled task? ,im running for multiple VM`s but i can get the status only for one of the VM ,but in VC i can see the scheduled tasks

$VMs = Import-Csv E:\output\VMs.csv

$vmObj = Get-VM -Name $VMs.Name

Get-View -Id (Get-View ScheduledTaskManager).ScheduledTask |
Where-Object { $_.Info.Entity -in $vmObj.Id } |
Select-Object @{N='Name';E={$_.Info.Name}}, @{N = 'NextRunTime'; E = { $_.Info.NextRunTime } }

 

0 Kudos
hpkishore
Contributor
Contributor
Jump to solution

 

Hi,

Im running the below script to get the snapshot scheduled tasks information for the VM`s, but im getting the result only for one of the VM and i could see the snapshot job has been scheduled for all the VM`s in the VC ,but  Script is not showing the result. Please advise me if im doing it wrong

Below is the script i use and the file name

$fileName = 'C:\Users\VM.csv'
$vcName = Get-Content "C:\Users\vcenter.txt"
Connect-VIServer $vcName -User $UserName -Password $UserPass
###############
Import-Csv -Path $fileName -UseCulture | %{
$vmObj = Get-VM -Name $_.VMName
Get-View -Id (Get-View ScheduledTaskManager).ScheduledTask |
Where-Object { $_.Info.Entity -in $vmObj.Id } |
Select-Object @{N='Name';E={$_.Info.Name}}, @{N = 'NextRunTime'; E = { $_.Info.NextRunTime } } |Export-Csv -Path $ReportFile -NoTypeInformation -UseCulture
}

Output of "C:\Users\VM.csv

VMName
VM1
VM2
VM3
VM4
VM5
VM6
VM7
VM8
VM9
VM10
VM11

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Your Export-Csv is inside the foreach loop, overwriting the file each time.
Which results in only the last VM in your CSV being in the CSV file

 

$fileName = 'C:\Users\VM.csv'
$vcName = Get-Content "C:\Users\vcenter.txt"
Connect-VIServer $vcName -User $UserName -Password $UserPass
###############
Import-Csv -Path $fileName -UseCulture | Foreach-Object -Process {
   $vmObj = Get-VM -Name $_.VMName
   Get-View -Id (Get-View ScheduledTaskManager).ScheduledTask |
   Where-Object { $_.Info.Entity -in $vmObj.Id } |
   Select-Object @{N='Name';E={$_.Info.Name}}, @{N = 'NextRunTime'; E = { 
      $_.Info.NextRunTime } }
} | Export-Csv -Path $ReportFile -NoTypeInformation -UseCulture

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos