Skip to content

Commit 167deb3

Browse files
committed
Updated Tutorial and fixed a few bugs
1 parent 9e79735 commit 167deb3

3 files changed

Lines changed: 59 additions & 35 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Show help for Cmdlet to connect to StorageGRID Management Server
3030
Connect to StorageGRID Management Server (use the `-Insecure` switch to skip checking the certificate of the server)
3131

3232
$Credential = Get-Credential
33-
Connect-SGWMgmtServer -Name myserver.mydomain.tld -Credential $Credential -Insecure
33+
$Server = "nms.mydomain.tld"
34+
Connect-SGWServer -Name $Server -Credential $Credential -Insecure
3435

3536
List all StorageGRID-Webscale Accounts
3637

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
# StorageGRID S3 Management PowerShell Cmdlet Tutorial
1+
# StorageGRID Webscale PowerShell Cmdlet Tutorial
22

3-
This tutorial will give an introduction to the StorageGRID S3 Management PowerShell Cmdlets
3+
This tutorial will give an introduction to the StorageGRID Webscale PowerShell Cmdlets
44

55
## Discovering the available Cmdlets
66

7-
Load the S3-Mgmt Module
7+
Load the StorageGRID-Webscale Module
88

99
```powershell
10-
Import-Module S3-Mgmt
10+
Import-Module StorageGRID-Webscale
1111
```
1212

13-
Show all available Cmdlets from the S3-Mgmt Module
13+
Show all available Cmdlets from the StorageGRID-Webscale Module
1414

1515
```powershell
16-
Get-Command -Module S3-Mgmt
16+
Get-Command -Module StorageGRID-Webscale
1717
```
1818

19-
Show the syntax of all Cmdlets from the S3-Mgmt Module
19+
Show the syntax of all Cmdlets from the StorageGRID-Webscale Module
2020

2121
```powershell
22-
Get-Command -Module S3-Mgmt
22+
Get-Command -Module StorageGRID-Webscale -Syntax
2323
```
2424

25-
To get detailed help including examples for a specific Cmdlet (e.g. for Connect-S3MgmtServer) run
25+
To get detailed help including examples for a specific Cmdlet (e.g. for Connect-SGWServer) run
2626

2727
```powershell
28-
Get-Help Connect-S3MgmtServer -Detailed
28+
Get-Help Connect-SGWServer -Detailed
2929
```
3030

3131
## Connecting to a StorageGRID Management Server
3232

33-
For data retrieval a connection to the StorageGRID Management Server is required. The Connect-S3MgmtServer Cmdlet expects the hostname or IP and the credentials for authentication
33+
For data retrieval a connection to the StorageGRID Management Server is required. The Connect-SGWServer Cmdlet expects the hostname or IP and the credentials for authentication
3434

3535
```powershell
36-
$ServerName = 'nms.stroagegrid.example.com'
36+
$Server = "nms.mydomain.tld"
3737
$Credential = Get-Credential
3838
Connect-S3MgmtServer -Name $ServerName -Credential $Credential
3939
```

src/StorageGRID-Webscale.psm1

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ function global:Connect-SGWServer {
4141
HelpMessage="Specify -Transient to not set the global variable `$CurrentOciServer.")][Switch]$Transient
4242
)
4343

44-
$body = @"
45-
{
46-
"username": "$($Credential.UserName)",
47-
"password": "$($Credential.GetNetworkCredential().Password)"
48-
}
49-
"@
50-
5144
# check if untrusted SSL certificates should be ignored
5245
if ($Insecure) {
5346
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
@@ -66,58 +59,88 @@ function global:Connect-SGWServer {
6659
$Server = New-Object -TypeName PSCustomObject
6760
$Server | Add-Member -MemberType NoteProperty -Name Name -Value $Name
6861
$Server | Add-Member -MemberType NoteProperty -Name Credential -Value $Credential
62+
63+
$Body = @"
64+
{
65+
"username": "$($Credential.UserName)",
66+
"password": "$($Credential.GetNetworkCredential().Password)"
67+
}
68+
"@
6969

7070
if ($HTTPS -or !$HTTP) {
7171
Try {
7272
$Server | Add-Member -MemberType NoteProperty -Name BaseURI -Value "https://$Name"
73-
$Response = Invoke-RestMethod -Method POST -Uri "$BaseURI/api/v1/authorize" -TimeoutSec 10 -ContentType "application/json" -Body $body
73+
$Response = Invoke-RestMethod -Method POST -Uri "$($Server.BaseURI)/api/v1/authorize" -TimeoutSec 10 -ContentType "application/json" -Body $body
7474
if ($Response.status -eq "success") {
7575
$Server | Add-Member -MemberType NoteProperty -Name APIVersion -Value $Response.apiVersion
7676
$Server | Add-Member -MemberType NoteProperty -Name Headers -Value @{"Authorization"="Bearer $($Response.data)"}
7777
}
78+
if (!$Transient) {
79+
Set-Variable -Name CurrentSGWServer -Value $Server -Scope Global
80+
}
81+
82+
return $Server
7883
}
7984
Catch {
8085
if ($_.Exception.Message -match "Unauthorized") {
81-
Write-Error "Authorization for $BaseURI/api/v1/authorize with user $($Credential.UserName) failed"
86+
Write-Error "Authorization for $($Server.BaseURI)/api/v1/authorize with user $($Credential.UserName) failed"
8287
return
8388
}
8489
else {
8590
if ($HTTPS) {
86-
Write-Error "Login to $BaseURI/api/v1/authorize failed via HTTPS protocol, but HTTPS was enforced"
91+
$result = $_.Exception.Response.GetResponseStream()
92+
$reader = New-Object System.IO.StreamReader($result)
93+
$reader.BaseStream.Position = 0
94+
$reader.DiscardBufferedData()
95+
$responseBody = $reader.ReadToEnd()
96+
if ($responseBody.StartsWith('{')) {
97+
$responseBody = $responseBody | ConvertFrom-Json | ConvertTo-Json
98+
}
99+
Write-Error "GET to $Uri failed with status code $($_.Exception.Response.StatusCode) and response body:`n$responseBody"
87100
return
88101
}
89102
else {
103+
Write-Warning "Login to $BaseURI/api/v1/authorize failed via HTTPS protocol"
90104
$HTTP = $True
91105
}
92106
}
93107
}
94108
}
95-
else {
109+
110+
if ($HTTP) {
96111
Try {
97-
$Server | Add-Member -MemberType NoteProperty -Name BaseURI -Value "http://$Name"
98-
$Response = Invoke-RestMethod -Method POST -Uri "$BaseURI/api/v1/authorize" -TimeoutSec 10 -ContentType "application/json" -Body $body
112+
$Server | Add-Member -MemberType NoteProperty -Name BaseURI -Value "http://$Name" -Force
113+
$Response = Invoke-RestMethod -Method POST -Uri "$($Server.BaseURI)/api/v1/authorize" -TimeoutSec 10 -ContentType "application/json" -Body $Body
99114
if ($Response.status -eq "success") {
100115
$Server | Add-Member -MemberType NoteProperty -Name APIVersion -Value $Response.apiVersion
101116
$Server | Add-Member -MemberType NoteProperty -Name Headers -Value @{"Authorization"="Bearer $($Response.data)"}
102117
}
118+
119+
if (!$Transient) {
120+
Set-Variable -Name CurrentSGWServer -Value $Server -Scope Global
121+
}
122+
123+
return $Server
103124
}
104125
Catch {
105126
if ($_.Exception.Message -match "Unauthorized") {
106-
Write-Error "Authorization for $BaseURI/api/v1/authorize with user $($Credential.UserName) failed"
127+
Write-Error "Authorization for $($Server.BaseURI)/api/v1/authorize with user $($Credential.UserName) failed"
107128
return
108129
}
109130
else {
110-
Write-Error "Login to $BaseURI/api/v1/authorize failed via HTTP protocol, but HTTP was enforced"
131+
$result = $_.Exception.Response.GetResponseStream()
132+
$reader = New-Object System.IO.StreamReader($result)
133+
$reader.BaseStream.Position = 0
134+
$reader.DiscardBufferedData()
135+
$responseBody = $reader.ReadToEnd()
136+
if ($responseBody.StartsWith('{')) {
137+
$responseBody = $responseBody | ConvertFrom-Json | ConvertTo-Json
138+
}
139+
Write-Error "GET to $Uri failed with status code $($_.Exception.Response.StatusCode) and response body:`n$responseBody"
111140
return
112141
}
113142
}
114143
}
115-
116-
if (!$Transient) {
117-
Set-Variable -Name CurrentSGWServer -Value $Server -Scope Global
118-
}
119-
120-
return $Server
121144
}
122145

123146
<#
@@ -1059,7 +1082,7 @@ function Global:Update-SGWIdentitySources {
10591082
[parameter(
10601083
Mandatory=$False,
10611084
Position=3,
1062-
HelpMessage="Identity Source Port")][Integer]$Port,
1085+
HelpMessage="Identity Source Port")][Int]$Port,
10631086
[parameter(
10641087
Mandatory=$False,
10651088
Position=4,

0 commit comments

Comments
 (0)