PowerShell script exchange
Post Reply
frankU32
Enthusiast
Posts: 36
Liked: 2 times
Joined: May 02, 2018 11:56 am
Full Name: Frank Tighe
Contact:

List VM's from API using powershell

Post by frankU32 »

Hello,

I need to list all the VM's currently managed by Veeam from the API with I am accessing via Powershell. I have the following :

Code: Select all


$ValidToken = Getveeamtoken

$response = Invoke-WebRequest -Uri http://veeam-srv:9399/api/managedServers -Method "GET" -Headers @{"X-RestSvcSessionId" = $ValidToken} 

$response
The problem I am having is making sense of the response. I can see the information I want is there and It looks like it's in XML but I can't get anything in PowerShell to do anything with it. 'select-xml' can't parse it and I cant convert it to JSON either. It's like the response is malformed in some way, which I'm sure it isn't. I could just use something like "-match" to get the results I want but I would rather do it properly.

Can anyone point me in the right direction please.
veremin
Product Manager
Posts: 20282
Liked: 2257 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List VM's from API using powershell

Post by veremin »

Hmm, if backup server is added to Enteprise Manager, why not to interact with it directly via PowerShell, instead of doing it with rather complicated approach (PowerShell invocation of RESTful API)? Thanks.
frankU32
Enthusiast
Posts: 36
Liked: 2 times
Joined: May 02, 2018 11:56 am
Full Name: Frank Tighe
Contact:

Re: List VM's from API using powershell

Post by frankU32 »

I don't have access to the server via powershell, I only have access to the api
veremin
Product Manager
Posts: 20282
Liked: 2257 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List VM's from API using powershell

Post by veremin »

I have never done it personally, but I think you should declare XML variable and only then parse it, similar to this example. Thanks.
digitalexpl0it
Influencer
Posts: 18
Liked: 4 times
Joined: Jul 21, 2016 1:31 am
Full Name: Derrick Rose
Contact:

Re: List VM's from API using powershell

Post by digitalexpl0it »

frankU32 wrote: Jun 27, 2018 3:20 pm Hello,

I need to list all the VM's currently managed by Veeam from the API with I am accessing via Powershell. I have the following :

Code: Select all


$ValidToken = Getveeamtoken

$response = Invoke-WebRequest -Uri http://veeam-srv:9399/api/managedServers -Method "GET" -Headers @{"X-RestSvcSessionId" = $ValidToken} 

$response
The problem I am having is making sense of the response. I can see the information I want is there and It looks like it's in XML but I can't get anything in PowerShell to do anything with it. 'select-xml' can't parse it and I cant convert it to JSON either. It's like the response is malformed in some way, which I'm sure it isn't. I could just use something like "-match" to get the results I want but I would rather do it properly.

Can anyone point me in the right direction please.

you will need to drill in the Content and the XML file

Example:

Code: Select all

$response = Invoke-WebRequest -Uri http://veeam-srv:9399/api/managedServers -Method "GET" -Headers @{"X-RestSvcSessionId" = $ValidToken} 
$bn = ([xml]$response.Content)
$bn
you can dril into the XML keys by adding them to the end of $bn = ([xml]$response.Content)

Like

Code: Select all

$bn = ([xml]$response.Content).EntityReferences
$bn
you can keep drilling in with adding a period then the key name

here is an example I am writing

Code: Select all

function _Fetch_Replicas($Token,$Urli) {
	# Ref: https://helpcenter.veeam.com/docs/backup/rest/replicas.html?ver=95u4
	## Fetch Replicas
	write-host "Fetching Replica VMs..."  -foreground "yellow"
	$ResourceUri = $Urli + 'backupSessions'
	write-host "Using URL:" $ResourceUri -foreground "green"
	
	# Additional Headers
	$hdrs = @{
		'X-RestSvcSessionId' = $Token
		ContentType = "application/json"
	}
	
	# Paramenters to send
	$Params = @{
		URI = $ResourceUri
		Method = "GET"
		Headers = $hdrs
	}
	
	$res = Invoke-WebRequest @Params
	#$bn = ([xml]$res.Content).EntityReferences.Ref.Links.Link
	$bn = ([xml]$res.Content).EntityReferences.ref
	$bn
}
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests