Dear all, do someone have a sample Powershell script to add a Server in PB DataCenter with the RESTFUL API? I need help to create a PS Script for selfservice and don't get it :( Thanks in Advance
Andreas
Dear all, do someone have a sample Powershell script to add a Server in PB DataCenter with the RESTFUL API? I need help to create a PS Script for selfservice and don't get it :( Thanks in Advance
Andreas
Hello Andreas,
We have plans to release a PowerShell module on our development roadmap. The goal is to have something available in the next 60 days or so. When available, it will show up at: https://devops.profitbricks.com/tools/
In the meantime, here is some information that may help you roll your own script.
First you need to convert your credentials to Base64. You can do this a number of ways, but from inside PowerShell, this should work:
$CredText = ‘username@domain.tld:password’
$CredBytes = [System.Text.Encoding]::UTF8.GetBytes($CredText)
$EncodedCreds =[Convert]::ToBase64String($CredBytes)
$EncodedCreds
dXNlcm5hbWVAZG9tYWluLnRsZDpwYXNzd29yZA==
Now you can use those credentials with 'Invoke-RestMethod' to make calls against the REST API.
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", 'Basic dXNlcm5hbWVAZG9tYWluLnRsZDpwYXNzd29yZA==')
$response = Invoke-RestMethod 'https://api.profitbricks.com/rest/v2/datacenters' -Headers $headers
$response contains the results of the request:
id type href items
-- ---- ---- -----
datacenters collection https://api.profitbricks.com/rest/v2/datacenters {@{id=[removed] ...
To create a server, compose the JSON request string and put it into a variable:
$server = '{"properties":{"name":"PS_Server_1","ram":512,"cores":1}}'
Now make the POST request using a valid data center id under your account:
$response = Invoke-RestMethod 'https://api.profitbricks.com/rest/v2/datacenters/[your-data-center-id-here]/servers' -Method Post -Headers $headers -Body $server -ContentType 'application/vnd.profitbricks.resource+json'
$response will contain some info about your accepted request:
PS C:\> $response
id : [server-id]
type : server
href : https://api.profitbricks.com/rest/v2/datacenters/[data-center-id]/servers/[server-id]
metadata : @{createdDate=[date-time]; createdBy=[user-id];
etag=[etag]; lastModifiedDate=[date-time];
lastModifiedBy=[user-id]; state=BUSY}
properties : @{name=PS_Server_1; cores=1; ram=512; availabilityZone=; vmState=; bootCdrom=; bootVolume=; cpuFamily=}
That should be a decent starting point interacting with the REST API from PowerShell. If anybody else out there has solved this a different way, please feel free to comment.
Eric