63044: Acronis Cyber Protect Cloud, Acronis Cyber Backup: How to run script inside virtual machine during agentless backup


use Google Translate

Operating Systems: 

Last update: 20-08-2021

This article provides instructions for running a pre-backup command on a virtual machine when performing agentless backup


This article describes how to prepare and run Pre-Post script inside a guest VM when performing agentless backup using Acronis Agent for Hyper-V. Script is executed using Powershell Direct.

More information about Powershell Direct can be found in Virtual Machine automation and management using PowerShell 

Operating system requirements:

Host: Windows 10, Windows Server 2016, or later running Hyper-V.
Guest/Virtual Machine: Windows 10, Windows Server 2016, or later.

System configuration used in this example:

  • Both Hyper-V host and guest VM have Windows Server 2016 installed
  • Guest VM is named “TestVM” and has a local Administrator account “TestVM\Administrator” 

(!) In some cases, security software/antivirus running inside the guest OS on VM may block the script execution. In case of such issues, check with the security software vendor how to configure the software to allow scripts to run.


1. On guest Virtual Machine create BAT file C:\Temp\script.bat with the commands that you want to run (freeze database operations, stop running applications, etc).

As an example, we will use the following sample script to test the execution of pre/post commands:

echo "Hello from %COMPUTERNAME%"
Echo "Script Launched" >> C:\Temp\log.txt

2. On Hyper-V Host create file C:\Temp\testPS.ps1 with the following content

$password = ConvertTo-SecureString 'MyPassword' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ('<VM\user>', $password)
Invoke-Command -VMName <virtual machine name> -ScriptBlock { path_to_the_bat_file_on_VM } -Credential $credential 

In our example the contents of file look like:

$password = ConvertTo-SecureString 'MyPassword' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ('TestVM\Administrator', $password)
Invoke-Command -VMName TestVM -ScriptBlock { C:\Temp\script.bat } -Credential $credential 

3. On Hyper-V Host create file C:\Temp\test.cmd with the following content 

powershell -command set-executionpolicy unrestricted
powershell -file "<Path_to_ps1_file>"
powershell -command set-executionpolicy default

In our example, using the file created at Step 2:

powershell -command set-executionpolicy unrestricted
powershell -file "C:\Temp\testPS.ps1"
powershell -command set-executionpolicy default

If you’re running a Hyper-V cluster, make sure that this script is placed on all nodes of the cluster. This will ensure successful execution of the script regardless of node where VM is currently running on.


4. Create a backup plan and apply it to the VM. In Backup Options, enable pre-backup command and specify the .cmd file created in Step 3 as the command to run:

5. Save and run the backup plan


If you used the script provided as example, check C:\Temp\log.txt on VM after running the backup plan. It should contain the words “Script launched”.