forked from Gerenios/AADInternals
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCloudShell_utils.ps1
108 lines (89 loc) · 2.76 KB
/
CloudShell_utils.ps1
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Creates a new Cloud Shell
# Sep 8th 2020
function New-CloudShell
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$PreferredLocation="westeurope"
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://management.core.windows.net/" -ClientId "c44b4083-3bb0-49c1-b47d-974e53cbdf3c"
$headers=@{
"x-ms-console-preferred-location" = $PreferredLocation
"Content-Type" = "application/json"
"Authorization" = "Bearer $AccessToken"
}
$body = '{"properties":{"osType":"linux"}}'
$response = Invoke-RestMethod -UseBasicParsing -Uri "https://management.azure.com/providers/Microsoft.Portal/consoles/default?api-version=2020-04-01-preview" -Method Put -Body $body -Headers $headers -ErrorAction SilentlyContinue
# return
return $response.properties
}
}
# Gets cloud shell authorization token
# Sep 8th 2020
function Get-CloudShellAuthToken
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$True)]
[String]$Url
)
Process
{
# Create headers
$headers=@{
"Content-Type" = "application/json"
"Authorization" = "Bearer $AccessToken"
}
# Empty body
$body = '{}'
# Fix the url
$url = $url.Replace(":443","")
$response = Invoke-RestMethod -UseBasicParsing -Uri "$url/authorize" -Method Post -Body $body -Headers $headers
# return
return $response.token
}
}
# Gets cloud shell settings
# Sep 8th 2020
function Get-CloudShellSettings
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$True)]
[String]$Url,
[Parameter(Mandatory=$False)]
[String]$Shell="pwsh"
)
Process
{
# Create headers
$headers=@{
"Content-Type" = "application/json"
"Authorization" = "Bearer $AccessToken"
}
# Get the window size
$rows = [console]::WindowHeight
$cols = [console]::WindowWidth
if($Shell -ne "Bash")
{
$Shell = "pwsh"
}
# Empty body
$body = '{}'
# Fix the url
$url = $url.Replace(":443","")
$response = Invoke-RestMethod -UseBasicParsing -Uri "$url/terminals?cols=$cols&rows=$rows&version=2019-01-01&shell=$Shell" -Method Post -Body $body -Headers $headers
# return
return $response
}
}