-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path40_Set-DDUX.ps1
More file actions
80 lines (64 loc) · 2.89 KB
/
Copy path40_Set-DDUX.ps1
File metadata and controls
80 lines (64 loc) · 2.89 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<#
.SYNOPSIS
Enables MATLAB DDUX to allow MathWorks to gain insights into how this product is being used.
.DESCRIPTION
The script sets environment variables to enable MathWorks to gain insights into the usage of this product, aiding in the improvement of MATLAB.
It's important to note that your content and information within your files are not shared with MathWorks.
To opt out of this service, simply delete this file.
.EXAMPLE
Set-DDUX
.NOTES
Copyright 2020-2026 The MathWorks, Inc.
#>
function Test-AWSEndpoint {
param(
[Parameter(Mandatory)]
[string]$Endpoint,
[int]$Port = 443,
[int]$TimeoutSeconds = 5
)
try {
$TcpClient = New-Object System.Net.Sockets.TcpClient
$Connection = $TcpClient.ConnectAsync($Endpoint, $Port)
$Completed = $Connection.Wait($TimeoutSeconds * 1000)
if ($Completed -and -not $Connection.IsFaulted) {
return $true
}
return $false
} catch {
Write-Host "Error connecting to $Endpoint : $($_.Exception.Message)"
} finally {
if ($TcpClient) { $TcpClient.Dispose() }
}
return $false
}
function Set-DDUX {
Write-Output 'Starting Set-DDUX...'
[Environment]::SetEnvironmentVariable('MW_CONTEXT_TAGS', 'MATLAB:AWS:V1', [System.EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable('MW_DDUX_FORCE_ENABLE', $true, [System.EnvironmentVariableTarget]::Machine)
$Token = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "120"} -Method PUT -Uri http://169.254.169.254/latest/api/token
$ResponseInstanceId = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token} -Method GET -Uri http://169.254.169.254/latest/meta-data/instance-id
$ResponseRegion = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token} -Method GET -Uri http://169.254.169.254/latest/meta-data/placement/region
$AWSDomain = "amazonaws.com"
if ($ResponseRegion -like "cn-*") {
$AWSDomain = "amazonaws.com.cn"
}
$EC2Endpoint = "ec2.$ResponseRegion.$AWSDomain"
if (-not (Test-AWSEndpoint -Endpoint "$EC2Endpoint")) {
Write-Output "AWS EC2 Endpoint $EC2Endpoint is not reachable. Skipping DDUX tag check."
return
}
$MWAppTagGroup = aws ec2 describe-tags --filters "Name=resource-id,Values=$ResponseInstanceId" 'Name=key,Values=mw-app' --region "$ResponseRegion"
$MWAppTag = (Write-Output $MWAppTagGroup | ConvertFrom-Json).Tags.Value
if ( $MWAppTag -eq 'cloudcenter' ) {
[Environment]::SetEnvironmentVariable('MW_CONTEXT_TAGS', 'MATLAB:AWS:V1,MATLAB:CLOUD_CENTER:V1', [System.EnvironmentVariableTarget]::Machine)
}
Write-Output 'Done with Set-DDUX.'
}
try {
Set-DDUX
}
catch {
$ScriptPath = $MyInvocation.MyCommand.Path
Write-Output "WARNING - An error occurred while running script 'Set-DDUX': $ScriptPath. Error: $_"
}