PowerShell script exchange
Post Reply
Andreas Neufert
Veeam Software
Posts: 3701
Liked: 652 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Veeam Powershell Scripts - Beginners guide

Post by Andreas Neufert » Sep 12, 2012 1:50 pm 6 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 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"
- 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. 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
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.

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 
9. 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 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}
10. Start job
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
That’s it... have fun with our PowerShell.
You can find a lot of PowerShell examples in this forum.
CU Andy

Andreas Neufert
Veeam Software
Posts: 3701
Liked: 652 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 » Oct 25, 2012 7:43 am

Added PSSnapIn resgistration error solution to Point 3

Andreas Neufert
Veeam Software
Posts: 3701
Liked: 652 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 » Feb 20, 2013 9:17 pm 1 person likes this post

Successfull tested with 6.5

Andreas Neufert
Veeam Software
Posts: 3701
Liked: 652 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 » Mar 08, 2015 10:05 pm 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
Veeam Software
Posts: 3701
Liked: 652 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 » Oct 21, 2016 8:08 pm

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 » Nov 07, 2016 1:31 pm

This is an awesome write up!

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

Andreas Neufert
Veeam Software
Posts: 3701
Liked: 652 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 » Nov 07, 2016 1:32 pm

Thanks. Have fun with our Poweshell SDK

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests