Quantcast
Channel: Rob V IT
Viewing all articles
Browse latest Browse all 64

PowerShell Rest API programming

$
0
0

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.

[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

 


Viewing all articles
Browse latest Browse all 64

Trending Articles