Jump to content
Welcome to our new Citrix community!
  • 0

Automate xenserver 8 snapshots


Question

Hi All,

 

We want to automate our image update process, we are running Ivanti Automation, and we are running Xenserver 8 latest version. So I started using PowerShell.

What I would like to accomplish is the following. 

We have a template machine, called Template-2019 I'm trying to build a script which connects to Xenserver pool master, then selects this VM and then revert to the latest snapshot then  boot the machine 

I'm terribly stuck at the process of selecting the latest snapshot.

 

With powershell I cannot get a commandline to get the latest snapshot of a vm, and revert to that snapshot

With Xe i can get all snapshots  but then I need to get the latest snapshot put into a variable so I can use this in another command, to revert to the latest snapshot.

 

So in short what my ultimate goal is:

- connect xenserver pool master (works)

- get latest snapshot on my reference vm (help needed)

- revert to latest snapshot on the reference vm (help needed)

- start reverence vm (works)

- Ivanti automations does the updates required (works)

- stop reference vm (works)

- take snapshot of reverence vm (not created yet)

- publish snapshot to citrix cloud machine catalog (not created yet). 

 

Is there anybody out there who has already done such steps?

If this is not possible, I will go and build in a step into my automation to check if the person has manually reverted to the latest snapshot. 

 

Hope someone knows the answer, I tried ChatGPT ? but no luck, also all kinds of reverence materials do not help me.

 

Best regards,

Herwin Duinkerken

 

Link to comment

1 answer to this question

Recommended Posts

  • 0

I have solved this question myself, gothen help from different persons within my company. 

 

So here is the script. 

 

####Script###########

####Set Variables
$VMName = 'name of the VM'
$user = 'name of user'
$password = 'password'
$xenhost = 'xenhost'
$Snapshotpath = 'Snapshotpath'  
$SecureClientFile = 'path to file' 
###Snapshot path is gothen by powershell command Get-ProvScheme |Select-Object MasterImageVM, starting with XDHyp\Hostingunits\  select the complete name until vmname.vm after this only .snapshot names remain. 

 

# Import the XenServer module
Import-Module XenServerPSModule
Add-PSSnapin Citrix*

 

######Connect to Cloud
###### Cloud credentials gothen from Identity and access management\ Api Access within Citrix cloud portal if not set, create one
### Download the file and put it where this script can reach it. 
Set-XDCredentials -CustomerId "Customer ID" -SecureClientFile $SecureClientFile  -ProfileType CloudApi -StoreAs CloudAdmin
Get-XDAuthentication -ProfileName Cloudadmin

 

###Get latest snapshot of virtual machine
$latestSnapshot = Get-ChildItem -Recurse -Path $Snapshotpath | Sort-Object -Property CreationDate -Descending | Select-Object -Last 1

 


# Connect to XenServer, hij connecteert net zo lang tot de pool master geconnect wordt.
Try {
        $Session = Connect-XenServer -Url https://$Xenhost -UserName $user -Password $password -NoWarnCertificates -SetDefaultSession
    } Catch [XenAPI.Failure] {
        [string]$PoolMaster = $_.Exception.ErrorDescription[1]  
        Write-Host -ForegroundColor Red "$($Pools.$Pool) is slave, Master was identified as $PoolMaster, trying to connect"
        $Pools.Pool = $PoolMaster
        $Session = Connect-XenServer -url "http://$PoolMaster" -UserName $user -Password $password -NoWarnCertificates -SetDefaultSession
    }

 

# Get the VM by its name
$VM = Get-XenVM | Where {$_.is_a_template -eq $False -and $_.is_a_snapshot -eq $False -and $_.domid -ne 0 -and $_.name_label -contains $VMName}

 

#####Revert to latest snapshot before boot
### could not get this to work without XE.exe
 

##go to xe.exe path Xencenter needs to be installed on the machine
cd "C:\Program Files (x86)\Citrix\XenCenter"

 

##set variable to the name of the lastest snapshotname
$snap = $latestSnapshot.name

 

##create variable which containt UUID of the snapshot with name form above
$VM1 = .\xe.exe snapshot-list name-label=$snap params=uuid --minimal params=uuid --minimal -s $xenhost -u $user -pw $password

 

##revert VM to latest snapshot 
.\xe snapshot-revert snapshot-uuid=$Vm1 -s $xenhost -u $user -pw $password

 

####End Script######

 

 

So I have reached my Goal. Maybe not within the best way ? 

Be carefull when executing this script. 

And use it on your own risk. 

 

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...