PowerShell script example using Cloudlink REST API
概要: This file is for education / information purposes only. Can be used as a reference when writing Cloudlink automation scripts with PowerShell script language. Dell takes no responsibility for this file being executed as a PowerShell script in Dell Customer's environment The script shows a way to list machines registered with Cloudlink and their drives from a PowerShell script using Cloudlink RestAPI calls. The script doesn't alter state of any system, just displays information. Access to the Cloudlink node (indicated by IP address) is granted with credentials of a Cloudlink user of type 'client'. ...
この記事は次に適用されます:
この記事は次には適用されません:
この記事は、特定の製品に関連付けられていません。
すべての製品パージョンがこの記事に記載されているわけではありません。
現象
The script can be used to automate Cloudlink operations. In this example the script lists machines registered with Cloudlink Center.
原因
The script might be useful to customers who have several Cloudlink Centers or who want to manage their Cloudlink Center using a PowerShell script.
解決方法
# Copyright Dell © 2021, 2022
# This file is for education / information purposes only.
# Can be used as a reference when writing Cloudlink automation scripts with PowerShell script language.
# Dell takes no responsibility for this file being executed as a PowerShell script in Dell Customer's environment
# The script shows a way to list machines registered with Cloudlink and their drives from a PowerShell script
# using Cloudlink RestAPI calls.
# The script doesn't alter state of any system, just displays information.
# Access to the Cloudlink node (indicated by IP address) is granted with credentials of a Cloudlink user of type 'client'.
# The following part is mocking the certificate validation, certificate always valid
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore()
$server='<cloudlink node ip>'
$client_id='<client id>'
$client_secret='<client secret>'
if ($args[0] -ne $null)
{ $server=$args[0] }
if ($args[1] -ne $null)
{ $client_id=$args[1] }
if ($args[2] -ne $null)
{ $client_secret=$args[2] }
$serverport=$server+":443"
Write-Output "parameters: server=$server, client_id=$client_id, client_secret=$client_secret"
$responsee=Invoke-RestMethod -Uri "https://$server/cloudlink/oauth/token?grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret&scope=all" -Method Get -TimeoutSec 1800
Write-Output "The response received:"
Write-Output "$responsee"
if($responsee.access_token)
{
$mytoken=$responsee.access_token
$Type = "application/json"
$resolveSessionHeader = @{'Authorization' = "Bearer $mytoken"}
# Query for the machines list
$machine_list_query='https://$serverport/cloudlink/rest/securevm'
$machine_list=Invoke-WebRequest -Method Get -Uri $machine_list_query -Headers $resolveSessionHeader -ContentType $Type
# List the machines on the console
Write-Host "Machine list:" -ForegroundColor Yellow
$machine_list_txt = $machine_list | ConvertFrom-Json
$machine_list_txt | FT
#For each machine, list the drives
foreach ($machine in $machine_list_txt)
{
$drives_list = $($machine.resources)
Write-Host "$($machine.name) $($machine.ip_address) ($($machine.os_name)) group $($machine.group)" -ForegroundColor Yellow
$drives_list | FT
}
}
# This file is for education / information purposes only.
# Can be used as a reference when writing Cloudlink automation scripts with PowerShell script language.
# Dell takes no responsibility for this file being executed as a PowerShell script in Dell Customer's environment
# The script shows a way to list machines registered with Cloudlink and their drives from a PowerShell script
# using Cloudlink RestAPI calls.
# The script doesn't alter state of any system, just displays information.
# Access to the Cloudlink node (indicated by IP address) is granted with credentials of a Cloudlink user of type 'client'.
# The following part is mocking the certificate validation, certificate always valid
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore()
$server='<cloudlink node ip>'
$client_id='<client id>'
$client_secret='<client secret>'
if ($args[0] -ne $null)
{ $server=$args[0] }
if ($args[1] -ne $null)
{ $client_id=$args[1] }
if ($args[2] -ne $null)
{ $client_secret=$args[2] }
$serverport=$server+":443"
Write-Output "parameters: server=$server, client_id=$client_id, client_secret=$client_secret"
$responsee=Invoke-RestMethod -Uri "https://$server/cloudlink/oauth/token?grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret&scope=all" -Method Get -TimeoutSec 1800
Write-Output "The response received:"
Write-Output "$responsee"
if($responsee.access_token)
{
$mytoken=$responsee.access_token
$Type = "application/json"
$resolveSessionHeader = @{'Authorization' = "Bearer $mytoken"}
# Query for the machines list
$machine_list_query='https://$serverport/cloudlink/rest/securevm'
$machine_list=Invoke-WebRequest -Method Get -Uri $machine_list_query -Headers $resolveSessionHeader -ContentType $Type
# List the machines on the console
Write-Host "Machine list:" -ForegroundColor Yellow
$machine_list_txt = $machine_list | ConvertFrom-Json
$machine_list_txt | FT
#For each machine, list the drives
foreach ($machine in $machine_list_txt)
{
$drives_list = $($machine.resources)
Write-Host "$($machine.name) $($machine.ip_address) ($($machine.os_name)) group $($machine.group)" -ForegroundColor Yellow
$drives_list | FT
}
}
対象製品
CloudLink SecureVM, CloudLink文書のプロパティ
文書番号: 000202396
文書の種類: Solution
最終更新: 13 3月 2023
バージョン: 5
質問に対する他のDellユーザーからの回答を見つける
サポート サービス
お使いのデバイスがサポート サービスの対象かどうかを確認してください。