PowerShell script exchange
Post Reply
EdwardPoll
Influencer
Posts: 17
Liked: never
Joined: Feb 09, 2011 9:03 am
Full Name: Edward Poll

set-vbrjobobject add VC folders

Post by EdwardPoll »

Hello..

Powershell question for you..

We are trying to create jobs and add VMs based on VC Folder we can get it to add individual machines and resource groups (only if they have unique names) – but we can’t get it to add folders, or at least we aren’t getting the syntax right to add them if it’s possible. It’s what we put in the variable $vbrobjects in the scripts below;

$vbrjobname = "Test"
$vbrjob = Get-VBRJob | Where {$_.Name -eq $vbrjobname}
$vbrserverVC = Get-VBRServer | Where {$_.Type -eq "VC"}
$vbrobjects = ANY IDEAS WHAT WE PUT HERE TO DEFINE THE FOLDER WE WANT TO USE??

finally running this
Add-VBRJobObject -Job $vbrjob -Server $vbrserverVC -Object $vbrobjects

We can add VMs based on folder quite happily from the GUI, but it'd take days to go through the whole environment by hand like that.

Any help would be much appreciated..

Cheers

Ed
Vitaliy S.
VP, Product Management
Posts: 27377
Liked: 2800 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: set-vbrjobobject add VC folders

Post by Vitaliy S. »

Edward,

The syntax for $vbrserverVC should look like this:

Code: Select all

$vbrserverVC = Get-VBRServer | where {$_.Name -eq "VC_Name"} | Find-VBRObject | Where {$_.Name -eq "VM_Name"}
Unfortunately, you cannot return any folder name while executing $vbrobjects, so to workaround this limitation, please follow the instuctions below:

For each object that has been returned by Find-VBRObject, execute FindParent cmdlet, this would allow you to find VMs in particular folder and then add those VMs to the backup job.

Code: Select all

Get-VBRServer | where {$_.Name -eq "VC_Name"} | Find-VBRObject | where {$_.FindParent("Folder").Name -eq "VM_Name"}
Hope it helps!
jkcouch
Enthusiast
Posts: 30
Liked: never
Joined: Jun 26, 2011 7:02 pm
Full Name: Jeff Couch
Contact:

Re: set-vbrjobobject add VC folders

Post by jkcouch »

Vitaliy S. wrote:Edward,

The syntax for $vbrserverVC should look like this:

Code: Select all

$vbrserverVC = Get-VBRServer | where {$_.Name -eq "VC_Name"} | Find-VBRObject | Where {$_.Name -eq "VM_Name"}
Unfortunately, you cannot return any folder name while executing $vbrobjects, so to workaround this limitation, please follow the instuctions below:

For each object that has been returned by Find-VBRObject, execute FindParent cmdlet, this would allow you to find VMs in particular folder and then add those VMs to the backup job.

Code: Select all

Get-VBRServer | where {$_.Name -eq "VC_Name"} | Find-VBRObject | where {$_.FindParent("Folder").Name -eq "VM_Name"}
Hope it helps!

Thanks for this. Is this still a limitation? Is there any other known workaround to assign folder objects rather than VMs from a known folder?
Vitaliy S.
VP, Product Management
Posts: 27377
Liked: 2800 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: set-vbrjobobject add VC folders

Post by Vitaliy S. »

No other workarounds for now, you should be using the instructions above. Thanks!
Sethbartlett
Veteran
Posts: 282
Liked: 26 times
Joined: Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett
Contact:

Re: set-vbrjobobject add VC folders

Post by Sethbartlett »

There actually is a way to add a folder into a job through powershell, but it is a little rough. Here is the following code:

Code: Select all

$Server = Get-VBRServer | ?{$_.name -eq "MyVC"}
$Job = Get-VBRJob | ?{$_.name -eq "MyJob"}
$FaV = ((New-Object -typename Veeam.Backup.Core.InfrastructureTree.HostsAndClustersLoader).LoadHierarchy($Server.Info)).FoldersAndVms()
$Entity = $FaV.GetChild((Find-VBRObject $Server | ?{$_.name -eq "MyVMName"})[0].VmFolder, 4, $true)

Add-VBRJobObject -Job $Job -Server $Server -Entities $Entity

For the following line, you just need to make sure you look up a VM that is in the folder you are wanting to add(You only need to do this once)

Code: Select all

$Entity = $FaV.GetChild((Find-VBRObject $Server | ?{$_.name -eq "MyVMName"})[0].VmFolder, 4, $true)
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
BarryCoombs
Enthusiast
Posts: 30
Liked: never
Joined: May 22, 2009 2:46 pm
Contact:

Powershell

Post by BarryCoombs »

[merged]

Has someone got a simple example of creating a Veeam job using powershell, I need to create the backups of VM Folders.

I imagine this should be an easy task but dont really seem to be getting very far.
BarryCoombs
Enthusiast
Posts: 30
Liked: never
Joined: May 22, 2009 2:46 pm
Contact:

Re: set-vbrjobobject add VC folders

Post by BarryCoombs »

Can someone just put this together into one script, I have tried merging the bits in this thread but am just getting errors. Can't beleive there isnt an example of how to do somehting as simple as this in the userguide
BarryCoombs
Enthusiast
Posts: 30
Liked: never
Joined: May 22, 2009 2:46 pm
Contact:

Re: set-vbrjobobject add VC folders

Post by BarryCoombs »

Sethbartlett wrote:There actually is a way to add a folder into a job through powershell, but it is a little rough. Here is the following code:

Code: Select all

$Server = Get-VBRServer | ?{$_.name -eq "MyVC"}
$Job = Get-VBRJob | ?{$_.name -eq "MyJob"}
$FaV = ((New-Object -typename Veeam.Backup.Core.InfrastructureTree.HostsAndClustersLoader).LoadHierarchy($Server.Info)).FoldersAndVms()
$Entity = $FaV.GetChild((Find-VBRObject $Server | ?{$_.name -eq "MyVMName"})[0].VmFolder, 4, $true)

Add-VBRJobObject -Job $Job -Server $Server -Entities $Entity

For the following line, you just need to make sure you look up a VM that is in the folder you are wanting to add(You only need to do this once)

Code: Select all

$Entity = $FaV.GetChild((Find-VBRObject $Server | ?{$_.name -eq "MyVMName"})[0].VmFolder, 4, $true)
When running this I get Parameter Job is null
Gostev
Chief Product Officer
Posts: 31806
Liked: 7300 times
Joined: Jan 01, 2006 1:01 am
Location: Baar, Switzerland
Contact:

Re: set-vbrjobobject add VC folders

Post by Gostev »

Barry, reading through the discussion above, there is no supported way to add folder to the job through PowerShell today. The workaround requires interacting with internal VBR data structures, which is unsupported.

Regarding missing examples, everyone wants something different - we cannot have all possible examples in the User Guide... it is already 200 pages long. This is why we have forums, so that everyone can share their own scripts they use. If you search this forum for PowerShell, you will find tons of those.

Thanks.
BarryCoombs
Enthusiast
Posts: 30
Liked: never
Joined: May 22, 2009 2:46 pm
Contact:

Re: set-vbrjobobject add VC folders

Post by BarryCoombs »

Thanks Gostev, as far as I can see there is no example of creating a backup using powershell in your manual, using folders or not, I would suggest the use of powershell in you product would be primarily to create backup jobs? Even the powershell help is poor tbh. Looks like I will need to create 60+ jobs manually
Gostev
Chief Product Officer
Posts: 31806
Liked: 7300 times
Joined: Jan 01, 2006 1:01 am
Location: Baar, Switzerland
Contact:

Re: set-vbrjobobject add VC folders

Post by Gostev »

Well, there is example of editing job settings in the User Guide, showing how to set job settings, so no big difference from creating the job. But, since you need to stuff your jobs with VM folders, you would have to perform this operation manually anyway, unfortunately.
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: set-vbrjobobject add VC folders

Post by tsightler »

As a small improvement to the unsupported code above, there's no real reason that you have to use a VM to look up a parent folder. If you know the name of the folder then something like this should would fine:

Code: Select all

$Server = Get-VBRServer -Name "<Your_vCenter_Server>"
$Job = Get-VBRJob -Name "<Job_to_Edit>"
$Folder = "<Folder_to_Add_to_Job>"

# Populate the top of the Folders and VMs Object View
$FaV = ((New-Object -typename Veeam.Backup.Core.InfrastructureTree.HostsAndClustersLoader).LoadHierarchy($Server.Info)).FoldersAndVms()

# Find the Folder Object for the Folder being added
$FolderObj = Find-VBRViEntity -Server $Server -VMsAndTemplates -Name $Folder

#Call GetChild with proper parameters to create the folder entity object
$Entity = $FaV.GetChild($FolderObj.Reference, "Folder", $true)

#Add object to job
Add-VBRJobObject -Job $Job -Server $Server -Entities $Entity
Even though the above code is "unsupported" it's pretty safe in that the only thing really not supported is the call to build the $FaV view. The default view populated by "Find-VBRObjects" is the "Host and Clusters" view which doesn't include the folder hierarchy thus calls to the "GetChild" method with the "Folder" parameter don't return useful info.

I tried to comment the code so that it makes more sense. Please feel free to ask questions if anything is not clear.
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: set-vbrjobobject add VC folders

Post by tsightler »

Vitaliy S. wrote:

Code: Select all

Get-VBRServer | where {$_.Name -eq "VC_Name"} | Find-VBRObject | where {$_.FindParent("Folder").Name -eq "VM_Name"}
BTW, this method doesn't appear to work, at least with 6.x, because the "FindParent("Folder") command returns the "Host" folder, not the vCenter folder. That's why the "unsupported" workaround that builds the VM and Folders view is required. It would be nice if Find-VBRObject was enhanced with a parameter to allow you to return the various views available in the GUI instead of always returning the Hosts and Clusters view.

If there is a way to make this work I'd sure like to know it.
Sethbartlett
Veteran
Posts: 282
Liked: 26 times
Joined: Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett
Contact:

Re: set-vbrjobobject add VC folders

Post by Sethbartlett »

There isn't and that was part of the problem. What's really crazy about it, is that FInd-VBRViEntity has -VMsAndTemplates, DatastoresAndHosts and a slew of other things to search for, but for some reason, FoldersAndVms isn't in that list.
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: set-vbrjobobject add VC folders

Post by tsightler »

Actually, -VMsAndTemplates is the correct view, and does have the object, but it returns a type of CViFolderItem, which is fine, and is perfectly usable for Add-VBRViJob, but doesn't work with Add-VBRJobObject because, while both use an option called "-Entity", they are two completely different data types. So another simple fix is to update Add-VBRJobObject, or perhaps create Add-VBRViJobObject that uses the same parameters as Add-VBRViJob.
Sethbartlett
Veteran
Posts: 282
Liked: 26 times
Joined: Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett
Contact:

Re: set-vbrjobobject add VC folders

Post by Sethbartlett »

I agree. This same issue occurs when trying to add a hyper-v object actually too. Just some object mismatches, makes for some fun workarounds ;)
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: set-vbrjobobject add VC folders

Post by tsightler »

Yep, for now the code I posted above, which leverages your hack to populate the VMs and Folders view, works fine and is pretty simple/safe. My only change was to show how you can add a folder without having to use a VM name, but just the actual folder name since I was getting this question a lot, and to try to break it out a little more to show how simple the code really is.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests