jroeser637
Contributor
Contributor

Getting the path of the latest snapshot!

Jump to solution

Hey guys,

So, I'm working on automating a recompose. My current line is this:

Get-DesktopVM -pool_id Pool_Name | Send-LinkedCloneRecompose -schedule ((Get-Date).AddMinutes(2)) -parentVMPath "/pathforvm/vm" -parentSnapshotPath "I/NEED/HELP/HERE" -forceLogoff $false -stopOnError $false

So, here's the tricky part. I want to recompose with the most recent snapshot I took. However, I don't see any easy commands to get the path for the latest snapshot. Is there some way that I can get the path for the latest snapshot and then just store it in $snapshot and then run my command as -parentSnapshotPath $snapshotpath?

Thanks for the help!

~Justin

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

I think we might be talking about different things here.

What you call PowerCLI, is in fact PowerCLI View, the PSSnapin to work with your View environment.

Then there is PowerCLI itself, currently a set of PSSnapins and modules to work with your vSphere environment.

That last one contains the Get-VM and Get-Snapshot cmdlets I used in the script above.

To use the script, you need both.

There are several options here, as I documented in my blog post.

You can install PowerCLI (for vSphere) on your Connection Server, then you run the script on the Connection server.

The other option is to install PowerCLI (for vSphere) on your workstation.

For the PowerCLI View cmdlets you then use remote sessions to the Connection server.

I hope this clarifies.


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

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership

Try something like this

$poolId = 'MyPool'

$pool = Get-Pool -Pool_id $poolId

$vm = Get-VM -Name $pool.parentVmPath.Split('/')[-1]

$snap = Get-Snapshot -VM $vm | Sort-Object -Property Created -Descending | Select -First 1

if($snap.Name -notmatch $pool.parentVMSnapshotPath)

{

    $newSnap = $pool.parentVMSnapshotPath.Replace($pool.parentVMSnapshotPath.Split('/')[-1],$snap.Name)

    Get-DesktopVM -Pool_id $poolId |

    Send-LinkedCloneRecompose -schedule ((Get-Date).AddMinutes(2)) -parentVMPath "/pathforvm/vm" -parentSnapshotPath $newSnap -forceLogoff $false -stopOnError $false

}


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

0 Kudos
jroeser637
Contributor
Contributor

LucD,

Thank you for your response! I can't run the Get-VM and Get-Snapshot cmdlets on the connection server, though. How else could I go about accomplishing this without the use of these commands?

Thanks for your time!

~Justin

0 Kudos
LucD
Leadership
Leadership

Do you mean that you are not allowed to install PowerCLI on one of the Connection servers ?

If that is the case, you can run the View cmdlets remotely, and the PowerCLI ones on your desktop.

See my How to run View PowerCLI post.


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

0 Kudos
jroeser637
Contributor
Contributor

No, I have PowerCLI on my connection server. I can run the Get-Pool commands there and Get-VDIDesktop commands; do I just need to install or import a module?

0 Kudos
LucD
Leadership
Leadership

Just do the following before you run the script

Add-PSSnapin VMware*

And of course you need to do a Connect-ViServer


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

0 Kudos
jroeser637
Contributor
Contributor

Maybe I'm just not seeing this correctly; I'm running this script on my view connection server. If I try to do add-pssnapin VMware*, it says that VMware.View.Broker is already added. Doing a Connect-VIServer just returns an error that the cmdlet is not found. What am I not seeing here? Thanks!

Edit: Actually, I run PowerCLI from the get-go on my connection server. But even if I open powershell and do add-pssnapin VMware*, I am still limited to what commands I can access. That is, even with Add-PSSnapin VMware*, I still can access get-pool but not get-vm or get-snapshot.

Edit2: Okay, on the computer I am using where Get-VM works, I noticed that the PSSnapin VMware.VimAutomation.Core is added, but it is not on my connection server. This must be why I can't do my get-vm command. I will see if I can find a link to download this snap-in. Hopefully that will work.

0 Kudos
LucD
Leadership
Leadership

That seems to indicate that PowerCLI is not installed on the Connection Server.

You can run the script from your local station, where you have PowerCLI.

For the View cmdlets you do a remote session, and then with Invoke-Command and the Session parameter you run those on the Connection Server.


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

0 Kudos
jroeser637
Contributor
Contributor

LucD,

Please bear with me, as I am still new to some of this! PowerCLI *is* installed on the Connection Server, as I can open up PowerCLI and type in Get-Pools and/or Send-LinkedRecompose. I'm wondering if it's not so much about whether or not I have PowerCLI installed, but instead whether or not I have the latest version.

You were a great help to me with my last question here, and I bookmarked the link you provided as far as running PowerCLI. I tried working with that script to get a remote session, but for some reason can't seem to get it working. I have no trouble with Connect-VIServer at my local station, and I can do things like copy files over to the connection server, it's just that the I can't seem to have the run both sets of commands in a single session! I'll investigate more today and see if I can get closer to the heard of the problem.

0 Kudos
LucD
Leadership
Leadership

I think we might be talking about different things here.

What you call PowerCLI, is in fact PowerCLI View, the PSSnapin to work with your View environment.

Then there is PowerCLI itself, currently a set of PSSnapins and modules to work with your vSphere environment.

That last one contains the Get-VM and Get-Snapshot cmdlets I used in the script above.

To use the script, you need both.

There are several options here, as I documented in my blog post.

You can install PowerCLI (for vSphere) on your Connection Server, then you run the script on the Connection server.

The other option is to install PowerCLI (for vSphere) on your workstation.

For the PowerCLI View cmdlets you then use remote sessions to the Connection server.

I hope this clarifies.


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

0 Kudos
jroeser637
Contributor
Contributor

Thanks a bunch! Yes, it clarified a lot for me. I installed the latest version of PowerCLI on the machine and then ran View PowerCLI; I had access to all my cmdlets! Your script also was a tremendous help. I need to tweak it just a little for the snapshot path, but it really made my life easier. Thanks again!

0 Kudos