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 SDK. That means you can start Veeam PowerShell scripts where you had installed the Console.
- 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 or Hyper-V 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 there came a free PowerShell editor named "Windows PowerShell ISE". At some Windows Versions, you should add it in the server manager under "features".
- At newer windows versions the PowerShell SE view only the console but not the editor window. You can view the editor window by pressing the arrow next to the "script" button (upper right side of the blue area).
You can type in PowerShell commands directly on the blue area or use the editor area to work on a .ps1 PowerShell file. You can execute the white code by pressing the "play" button at the top menue and you will see the output in the blue area.
4. Allow custom PowerShell script execution on your Backup & Replication Server
(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 "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. Load Veeam PowerShell module
- After the above script runs without problems it is time to implement the first Veeam commands.
- We load the Veeam PowerShell snap in with the command "Add-PSSnapin".
- If you run your script more then once in the editor, it says that VeeamPSSnapIn already loaded. You can prevent this if you add " -ErrorAction SilentlyContinue"
So the code is now
Code: Select all
write-host "Starting Veeam Job test"
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
"C:\Program Files\Veeam\Backup and Replication\Install-VeeamToolkit.ps1" from an admin PowerShell window.
If you run into the error: "VeeamPSSnapIn" isn´t installed please check Register VeeamPSSnapIn
7.Write job name to a variable
$JobName = "test"
Code: Select all
write-host "Starting Veeam Job test"
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$JobName = "test"
8. Connect to the Backup & Replication Server
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>
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
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 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"
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}
Now we add the start job command with:
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
You can find a lot of PowerShell examples in this forum.
CU Andy