With the PowerShell invoke-restmethod command it’s possible to interact with a REST based API.
In the following script I’ve added a couple of examples how-to interact with methods as GET and POST.
Add VM to VEEAM Portal scope with PowerShell and the VEEAM Rest API
The steps taken in this script:
– Creating a new session with the given credentials.
– Search for the VMid (GET query).
– Search for the right account. (GET query)
– Add VM To portal scope (POST with XML body).
*note: No error/exception handling added, just a beginners script to show howto interact with the VEEAM Rest API with PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
[CmdletBinding()] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true)] [string[]]$vmname, [parameter(Mandatory=$true,ValueFromPipeline=$true)] [string[]]$account, [parameter(Mandatory=$true,ValueFromPipeline=$true)] [string[]]$uri ) #Example usage #PS_RESTAPI.PS1 -uri 0.0.0.0:9399 -vmname VMTST01 -account TST #Portal credentials $cred = get-credential #Create rest api powershell session $Auth = @{uri = "http://$uri/api/sessionMngr/?v=v1_2"; Method = 'POST'; } Write-host "Created VEEAM Rest API Powershell Session" -ForegroundColor Yellow #Searching for new VM ID wich can add to the new scope, this can take a wile... $AuthXML = Invoke-WebRequest @Auth -Credential $cred Write-host "Searching for VMID...." -ForegroundColor Yellow $Sessions = @{uri = "http://$uri/api/lookup?host=urn%3aveeam%3aHierarchyRoot%3a673b84ce-2d1d-466c-87bc-929694962897&name=$vmname&type=vm"; Method = 'GET'; Headers = @{'X-RestSvcSessionId' = $AuthXML.Headers['X-RestSvcSessionId']; } } $response = invoke-restmethod @Sessions -TimeoutSec 800 $Jobobject = $response.GetElementsByTagName("HierarchyItem") Write-host "Object $vmname with ID $($jobobject.objectref) is found" -ForegroundColor Yellow #search for the right user scope $accounts = @{uri = "http://$uri/api/security/accounts"; Method = 'GET'; Headers = @{'X-RestSvcSessionId' = $AuthXML.Headers['X-RestSvcSessionId']; } } [xml]$accountinv = invoke-restmethod @accounts $accounts = $accountinv.GetElementsByTagName("Ref") $finalaccount = $accounts | where{$_.Name -match $account} | select -ExpandProperty href $hieraobj = $Jobobject.objectref $body = @" <?xml version="1.0" encoding="utf-8"?> <HierarchyScopeCreateSpec xmlns="http://www.veeam.com/ent/v1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <HierarchyScopeItem> <HierarchyObjRef>$hieraobj</HierarchyObjRef> <ObjectName>$vmname</ObjectName> </HierarchyScopeItem> </HierarchyScopeCreateSpec> "@ $headers["Content-Type"] = "application/xml" $portalhref = $finalaccount + "/scopes" $addvm = @{Method = 'POST'; Headers = @{'X-RestSvcSessionId' = $AuthXML.Headers['X-RestSvcSessionId']; 'content-type' = 'application/xml'; } } invoke-restmethod @addvm -Body $body -uri $portalhref Write-host "Object $vmname is add to $($finalaccount.name) " -ForegroundColor Yellow |