PowerShell script exchange
Post Reply
C_NZ
Novice
Posts: 4
Liked: never
Joined: Apr 08, 2020 11:01 pm
Full Name: C_NZ
Contact:

Shutdown, backup then start VM via script

Post by C_NZ »

I'm currently trying to setup a backup job so that it makes use of pre-backup and post-backup scripts following the details shown here:

https://helpcenter.veeam.com/archive/ba ... ts_vm.html

On the affected server I've installed PowerCLI and confirmed I can reach the vCenter server and view a list of VMs when manually providing server details, credentials and running get-vm.

I'm looking for an example script, or something else I can use at a template to tell Veeam to shutdown two VMs before a backup and then start them back up after the backup.
I've found a few examples like this one here but it looks to be using an older script style and not PowerCLI:

vmware-vsphere-f24/how-to-shut-down-a-v ... 36993.html

My thinking is the PowerCLI/PowerShell script I call, will need to:
Connect to the vCenter, provide the relevant credentials and then issue the shutdown command.
(Plus then the reverse of the above after the backup)

Could anyone please provide me an example script, or point me in the direction of something that would help.
I may be able to do it from scratch, but any examples or templates would be really helpful to speed the process up.


Background:
The affected servers cannot be backed up due to OS level / VSS level issues.
Backing up while they are shutdown, is a workaround until these servers get replaced.
jhoughes
Veeam Vanguard
Posts: 282
Liked: 113 times
Joined: Apr 20, 2017 4:19 pm
Full Name: Joe Houghes
Location: Castle Rock, CO
Contact:

Re: Shutdown, backup then start VM via script

Post by jhoughes »

Those operations are covered with the Shutdown-VMGuest or Stop-VM cmdlets for shutting down the VM, then Start-VM for turning the VM back on. You can find all the details for the cmdlets within the PowerCLI documentation.

If you need a full sample script, I'd suggest running a Google search for those cmdlets, checking on the VMTN community, or looking at the VMware Code samples, as that is really basic PowerCLI usage and nothing specific to Veeam.
Husband, Father, Solutions Architect, Geek | @DenverVMUG & @DenverPSUG leader | International Speaker | Veeam Vanguard | vExpert (PRO) | Cisco Champion
C_NZ
Novice
Posts: 4
Liked: never
Joined: Apr 08, 2020 11:01 pm
Full Name: C_NZ
Contact:

Re: Shutdown, backup then start VM via script

Post by C_NZ »

Thanks jhoughes,
I'll take a look into those cmdlets.
C_NZ
Novice
Posts: 4
Liked: never
Joined: Apr 08, 2020 11:01 pm
Full Name: C_NZ
Contact:

Re: Shutdown, backup then start VM via script

Post by C_NZ »

I've gotten further but bumped into a new issue.

Running testing I created two PowerShell scripts which are running fine via PowerShell itself, but when calling them from Veeam I am just getting 'exit code 1' for the scripts with no real error message to tell me what is happening.

I've found some postings suggesting that using .ps1 files is problematic?
Could someone please advise on this, or explain how I can fix these to work within Veeam itself:

Scripts created so far:

ForceOffVMv1.ps1(Used if VM has no vmware tools / a forced stop is wanted)
Connect-VIServer -Server 123.456.789.101 -User administrator@vsphere.local -Password PasswordGoesHere
Get-VM MyTestVM | Stop-VM -confirm:$false

ShutdownVMv1.ps1 (Used to shutdown VM gracefully if vmware tools are available)
Connect-VIServer -Server 123.456.789.101 -User administrator@vsphere.local -Password PasswordGoesHere
Get-VM MyTestVM | Stop-VMguest -confirm:$false

StartVMv1.ps1
Connect-VIServer -Server 123.456.789.101 -User administrator@vsphere.local -Password PasswordGoesHere
Get-VM MyTestVM | Start-VM
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Shutdown, backup then start VM via script

Post by veremin »

Have you tried to execute the script first under veeam backup service account and see whether it runs successfully? Thanks!
C_NZ
Novice
Posts: 4
Liked: never
Joined: Apr 08, 2020 11:01 pm
Full Name: C_NZ
Contact:

Re: Shutdown, backup then start VM via script

Post by C_NZ »

Thank you to all for the replies.
The solution here was to avoid the pre and post script all together.

Instead a powershell script was written that makes use of both the PowerCLI module and the VeeamPSSnapin.
Using this, a script was written to shutdown the affected VM's and then start the Veeam backup job. Once done, the script then starts the VMs back up.

The script was set to run as a scheduled task. Part of this involved first calling a batch file to run PowerShell in unrestricted mode and then the script, changing the powershell mode back to RemoteSigned when done.

The service account idea was a good clue, this was 'system'.
For unknown reasons, it was found that the system account could not make use of the PowerCLI commands...but a normal user on the system could.
So by running a scheduled task, and being able to set which user the script runs as...bypassed this issue.
jhoughes
Veeam Vanguard
Posts: 282
Liked: 113 times
Joined: Apr 20, 2017 4:19 pm
Full Name: Joe Houghes
Location: Castle Rock, CO
Contact:

Re: Shutdown, backup then start VM via script

Post by jhoughes »

If you are running it as a scheduled task, it is easier to utilize "–ExecutionPolicy Bypass" for the command line run of the script.
Husband, Father, Solutions Architect, Geek | @DenverVMUG & @DenverPSUG leader | International Speaker | Veeam Vanguard | vExpert (PRO) | Cisco Champion
ITP-Stan
Expert
Posts: 214
Liked: 61 times
Joined: Feb 18, 2013 10:45 am
Full Name: Stan G
Contact:

Re: Shutdown, backup then start VM via script

Post by ITP-Stan » 2 people like this post

For others that have to poweroff a VM to back it up.

One-time instructions:
Install the PowerCLI module on the Backupserver for all users.

Code: Select all

Install-Module vmware.powercli -scope AllUsers -force -SkipPublisherCheck -AllowClobber
If you use self signed certificates on the ESXi or vCenter.

Code: Select all

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -scope AllUsers -Confirm:$false
Script: Stop-VM.ps1

Code: Select all

Import-module vmware.powercli
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer -Server 192.168.x.y -User xxxx -Password yyyyy
Get-VM "name" | Stop-VmGuest -Confirm:$false
Script: Start-VM.ps1

Code: Select all

Import-module vmware.powercli
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer -Server 192.168.x.y -User xxxx -Password yyyyy
Get-VM "name" | Start-Vm -Confirm:$false
You need to configure these scripts as Pre-Job and Post-Job in the advanced storage settings of the job.
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests