this small guide was written to help you with your first Veeam PowerShell script based on a "start (backup) job" example.
1. Install Backup & Replication on a Windows machine
2. Install Backup & Replication Console on the Windows Server/Client where you want to start the PowerShell scripts.
By default, the Console is installed with other components on the Backup & Replication Server, but you can install it on additional servers or workstations (Windows OS only).
Together with the Console, Veeam will automatically install the Veeam PowerShell Module which enables you to use the Veeam Powershell commands.
- Start the console (Windows UI) and login to your installed backup and replication server (you must type in the name of the B&R Server)
- Add vCenter, Hyper-V or Agents at the Backup Infrastructure tab.
- Create a Backup Job with name "test" and run the backup. Take care that everything runs fine.
3. Start PowerShell editor
- Together with actual Windows Servers and Clients Microsoft delivers a PowerShell editor named "Windows PowerShell ISE". At some Windows Versions, you need to add it in the server manager under "features".
- At newer windows versions the PowerShell ISE view only the console but not the editor window. You drop down the editor window by pressing the arrow next to the "script" button (upper right side of the blue area).
You can now type in PowerShell commands directly on the blue area or use the editor area to work on a .ps1 PowerShell file that you can now save. You can execute the code from the white area by hitting the "play" button at the top menue. The output will be written to the blue powershell console area.
4. Allow custom PowerShell script execution on the system that runs the powershell script
(If you already did that, please jump to the next step)
At least you should allow it, if you see the following error:
"... cannot be loaded because the execution of scripts is disabled on this system."
You can enable it by open a PowerShell Window (or the ISE) in Admin Mode (Right Click - "Run as Administrator")
and type in: Set-ExecutionPolicy RemoteSigned
Please read the Windows Warning carefully and use the mentioned link to inform yourself about the consequences.
Set-ExecutionPolicy (External Link)
5. Create a simple PowerShell script and test if it can run
- Write the following code to a new script
Code: Select all
write-host "Starting Veeam Job test"
At the bottom, you can see the output. If you see an error, please find the solution above in this text or use google to find a solution.
6.Write job name to a variable
$JobName = "test"
Code: Select all
write-host "Starting Veeam Job test"
$JobName = "test"
This is not needed if you run the script directly on the B&R Server.
To avoid problems with open connections from previous sessions close the connection first.
Disconnect-VBRServer
connect-vbrserver -server <servername, FQDN or IP> -user <domainname\username or servername\username> -password <password>
There are many standard powershell methods to encrypt the passwords for script usage please search the internet for examples.
Code: Select all
write-host "Starting Veeam Job test"
$JobName = "test"
Disconnect-VBRServer | out-null
connect-vbrserver -server vbr.test.local -user test\veeamuser1 -password xxx
If we will now try to start the job with this variable, we will run in an error, because we handover "only" the name of the job, but the command expect the handover of all job information’s (the whole job object).
So we need to read out the complete job object with:
Get-VBRJob | where {$_.Name -eq $JobName}
and write it to a new variable
$JobObject = Get-VBRJob | where {$_.Name -eq $JobName}
Code: Select all
write-host "Starting Veeam Job test"
$JobName = "test"
Disconnect-VBRServer | out-null
connect-vbrserver -server vbr.test.local -user test\veeamuser1 -password xxx
$JobObject = Get-VBRJob | where {$_.Name -eq $JobName}
Now we add the start job command itself:
Code: Select all
write-host "Starting Veeam Job test"
$JobName = "test"
Disconnect-VBRServer | out-null
connect-vbrserver -server vbr.test.local -user test\veeamuser1 -password xxx
$JobObject = Get-VBRJob | where {$_.Name -eq $JobName}
Start-VBRJob $JobObject
Add a disconnect at the end:
Code: Select all
write-host "Starting Veeam Job test"
$JobName = "test"
Disconnect-VBRServer | out-null
connect-vbrserver -server vbr.test.local -user test\veeamuser1 -password xxx
$JobObject = Get-VBRJob | where {$_.Name -eq $JobName}
Start-VBRJob $JobObject
Disconnect-VBRServer | out-null
Compatibility with Veeam Backup & Replication v10a and before - Load Veeam PowerShell module
Before v11, Veeam Backup & Replication used a PowerShell Plug-in that needed to be loaded before Veeam PowerShell commands can be used.
Add-PSSnapin -Name VeeamPSSnapIn
to silence any errors use
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
Code example for v10 and before
Code: Select all
write-host "Starting Veeam Job test"
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$JobName = "test"
Disconnect-VBRServer | out-null
connect-vbrserver -server vbr.test.local -user test\veeamuser1 -password xxx
$JobObject = Get-VBRJob | where {$_.Name -eq $JobName}
Start-VBRJob $JobObject
Disconnect-VBRServer | out-null
In older versions it sometime happened that the OS blocked the Veeam PowerShell registration, you can fix it by running
"C:\Program Files\Veeam\Backup and Replication\Install-VeeamToolkit.ps1" from an admin PowerShell window.
That’s it... have fun with our PowerShell Module.
You can find a lot of PowerShell examples in this forum section.
Best regards
Andreas