PowerShell script exchange
Post Reply
Andreas Neufert
VP, Product Management
Posts: 5562
Liked: 1139 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Veeam Powershell Scripts - Beginners guide

Post by Andreas Neufert » 8 people like this post

Hello everybody,

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"
- Save it as "Test.ps1" and run it in the "Windows PowerShell ISE" (Play button).
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"
7. 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>

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 
8. Load job object
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}
9. Start job
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
10. Disconnect
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
If you run into the error: "VeeamPSSnapIn" isn´t installed please check Register VeeamPSSnapIn
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

Andreas Neufert
VP, Product Management
Posts: 5562
Liked: 1139 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Beginners guide

Post by Andreas Neufert »

Added PSSnapIn resgistration error solution to Point 3

Andreas Neufert
VP, Product Management
Posts: 5562
Liked: 1139 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Beginners guide *UPDATE1*

Post by Andreas Neufert » 1 person likes this post

Successfull tested with 6.5

Andreas Neufert
VP, Product Management
Posts: 5562
Liked: 1139 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Beginners guide *UPDATE2*

Post by Andreas Neufert » 2 people like this post

Some general updates to streamline the first steps and make them compatible with the B&R v7/v8 installer.

Andreas Neufert
VP, Product Management
Posts: 5562
Liked: 1139 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Veeam Powershell Scripts - Beginners guide *UPDATE3*

Post by Andreas Neufert »

General Update for v9, actual ISE implementation and remote console.

coreyfire
Novice
Posts: 4
Liked: never
Joined: Sep 30, 2016 11:26 pm
Full Name: Corey Bussard
Contact:

Re: Veeam Powershell Scripts - Beginners guide *UPDATE3*

Post by coreyfire »

This is an awesome write up!

thank you very much for all of this information! It has been very helpful for me!

Andreas Neufert
VP, Product Management
Posts: 5562
Liked: 1139 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Veeam Powershell Scripts - Beginners guide *UPDATE3*

Post by Andreas Neufert »

Thanks. Have fun with our Poweshell SDK

Andreas Neufert
VP, Product Management
Posts: 5562
Liked: 1139 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Veeam Powershell Scripts - Beginners guide *Update 4*

Post by Andreas Neufert »

Veeam Backup & Replication v11 and later uses now a PowerShell module instead of a Plug-in. The example above was changed to reflect this and v10 and before code example was added to the end.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests