Skip to content

Commit c3a5a64

Browse files
committed
update tenant info logic
1 parent 3cf4cad commit c3a5a64

1 file changed

Lines changed: 95 additions & 106 deletions

File tree

core/tenant/Get-TenantInformation.ps1

Lines changed: 95 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -35,133 +35,122 @@ Function Get-TenantInformation{
3535
#>
3636
[CmdletBinding()]
3737
Param()
38-
#Create hashtable
39-
$tenantInfo= [PsCustomObject]@{
40-
tenantName = $null;
41-
tenantId = $null;
42-
companyInfo = $null;
43-
sku = $null;
44-
domains = $null;
45-
myDomain = $null;
46-
licensing = $null;
47-
}
48-
If($O365Object.auth_tokens.MSGraph -and $O365Object.TenantId){
38+
Process{
4939
Try{
50-
#Write message
51-
$msg = @{
52-
MessageData = ($message.AADTenantInfoMessage -f $O365Object.TenantId);
53-
callStack = (Get-PSCallStack | Select-Object -First 1);
54-
logLevel = 'verbose';
55-
Tags = @('AADTenantInfo');
56-
}
57-
Write-Verbose @msg
58-
#Get Auth from old graph
59-
$msgraph_auth = $O365Object.auth_tokens.MSGraph
60-
#Get Tenant Origin
61-
If($O365Object.isValidTenantGuid -eq $false){
62-
$tid = Read-JWTtoken -token $O365Object.auth_tokens.MSGraph.AccessToken | Select-Object -ExpandProperty tid -ErrorAction Ignore
63-
}
64-
Else{
65-
$tid = $O365Object.TenantId
66-
}
67-
#Get tenant details
68-
$Tenant = Get-MonkeyMSGraphOrganization -TenantId $tid
69-
If($Tenant){
70-
#Set Tenant info var
71-
Set-Variable Tenant -Value $Tenant -Scope Script -Force
72-
If($O365Object.isConfidentialApp){
73-
#Set Userprincipalname var
74-
If($msgraph_auth.psobject.Properties.Item('clientId')){
75-
Set-Variable userPrincipalName -Value $msgraph_auth.clientId.ToString() -Scope Script -Force
76-
$O365Object.userPrincipalName = $msgraph_auth.clientId.ToString()
77-
}
78-
Else{
79-
$msg = @{
80-
MessageData = $message.AADUserErrorMessage;
81-
callStack = (Get-PSCallStack | Select-Object -First 1);
82-
logLevel = 'warning';
83-
Tags = @('AADUserPrincipalNameError');
84-
}
85-
Write-Warning @msg
86-
Set-Variable userPrincipalName -Value $null -Scope Script -Force
87-
$O365Object.userPrincipalName = $O365Object.initParams.ClientId
88-
}
40+
If($O365Object.auth_tokens.MSGraph -and $O365Object.TenantId){
41+
#Write message
42+
$msg = @{
43+
MessageData = ($message.AADTenantInfoMessage -f $O365Object.TenantId);
44+
callStack = (Get-PSCallStack | Select-Object -First 1);
45+
logLevel = 'verbose';
46+
Tags = @('EIDTenantInfo');
47+
}
48+
Write-Verbose @msg
49+
#Get Tenant Origin
50+
If($O365Object.isValidTenantGuid -eq $false){
51+
$tid = Read-JWTtoken -token $O365Object.auth_tokens.MSGraph.AccessToken | Select-Object -ExpandProperty tid -ErrorAction Ignore
8952
}
9053
Else{
91-
#Set Userprincipalname var
92-
If($msgraph_auth.psobject.Properties.Item('UserInfo')){
93-
$O365Object.userPrincipalName = $msgraph_auth.UserInfo.DisplayableId.ToString()
94-
Set-Variable userPrincipalName -Value $msgraph_auth.UserInfo.DisplayableId.ToString() -Scope Script -Force
95-
}
96-
ElseIf($msgraph_auth.psobject.Properties.Item('userPrincipalName')){
97-
$O365Object.userPrincipalName = $msgraph_auth.userPrincipalName
98-
Set-Variable userPrincipalName -Value $msgraph_auth.userPrincipalName -Scope Script -Force
99-
}
100-
ElseIf($msgraph_auth.psobject.Properties.Item('Account')){
101-
$O365Object.userPrincipalName = $msgraph_auth.Account.Username
102-
Set-Variable userPrincipalName -Value $msgraph_auth.Account.Username -Scope Script -Force
54+
$tid = $O365Object.TenantId
55+
}
56+
#Get tenant details
57+
$Tenant = Get-MonkeyMSGraphOrganization -TenantId $tid
58+
If($Tenant){
59+
#Set Tenant info var
60+
Set-Variable Tenant -Value $Tenant -Scope Script -Force
61+
If($O365Object.isConfidentialApp){
62+
#Set Userprincipalname var
63+
If($O365Object.auth_tokens.MSGraph.psobject.Properties.Item('clientId')){
64+
Set-Variable userPrincipalName -Value $O365Object.auth_tokens.MSGraph.clientId.ToString() -Scope Script -Force
65+
$O365Object.userPrincipalName = $O365Object.auth_tokens.MSGraph.clientId.ToString()
66+
}
67+
Else{
68+
$msg = @{
69+
MessageData = $message.AADUserErrorMessage;
70+
callStack = (Get-PSCallStack | Select-Object -First 1);
71+
logLevel = 'warning';
72+
Tags = @('EIDUserPrincipalNameError');
73+
}
74+
Write-Warning @msg
75+
Set-Variable userPrincipalName -Value $null -Scope Script -Force
76+
$O365Object.userPrincipalName = $O365Object.initParams.ClientId
77+
}
10378
}
10479
Else{
105-
$msg = @{
106-
MessageData = $message.AADUserErrorMessage;
107-
callStack = (Get-PSCallStack | Select-Object -First 1);
108-
logLevel = 'warning';
109-
Tags = @('AADUserPrincipalNameError');
80+
#Set Userprincipalname var
81+
If($O365Object.auth_tokens.MSGraph.psobject.Properties.Item('UserInfo')){
82+
$O365Object.userPrincipalName = $O365Object.auth_tokens.MSGraph.UserInfo.DisplayableId.ToString()
83+
Set-Variable userPrincipalName -Value $O365Object.auth_tokens.MSGraph.UserInfo.DisplayableId.ToString() -Scope Script -Force
84+
}
85+
ElseIf($O365Object.auth_tokens.MSGraph.psobject.Properties.Item('userPrincipalName')){
86+
$O365Object.userPrincipalName = $O365Object.auth_tokens.MSGraph.userPrincipalName
87+
Set-Variable userPrincipalName -Value $O365Object.auth_tokens.MSGraph.userPrincipalName -Scope Script -Force
88+
}
89+
ElseIf($O365Object.auth_tokens.MSGraph.psobject.Properties.Item('Account')){
90+
$O365Object.userPrincipalName = $O365Object.auth_tokens.MSGraph.Account.Username
91+
Set-Variable userPrincipalName -Value $O365Object.auth_tokens.MSGraph.Account.Username -Scope Script -Force
92+
}
93+
Else{
94+
$msg = @{
95+
MessageData = $message.AADUserErrorMessage;
96+
callStack = (Get-PSCallStack | Select-Object -First 1);
97+
logLevel = 'warning';
98+
Tags = @('EIDUserPrincipalNameError');
99+
}
100+
Write-Warning @msg
101+
Set-Variable userPrincipalName -Value $null -Scope Script -Force
102+
$O365Object.userPrincipalName = $null
110103
}
111-
Write-Warning @msg
112-
Set-Variable userPrincipalName -Value $null -Scope Script -Force
113-
$O365Object.userPrincipalName = $null
104+
}
105+
#Set properties
106+
$O365Object.Tenant.tenantName = $Tenant.displayName
107+
$O365Object.Tenant.companyInfo = $Tenant
108+
$O365Object.Tenant.tenantId = $Tenant.Id
109+
#Get subscribed SKUs
110+
$O365Object.Tenant.sku = Get-MonkeyMSGraphSuscribedSku
111+
If($null -ne $O365Object.Tenant.sku){
112+
#Get licensing info from current tenant
113+
$O365Object.Tenant.licensing = Get-TenantLicensingInfo -SKU $O365Object.Tenant.sku
114+
}
115+
#Get Domains
116+
$O365Object.Tenant.domains = Get-MonkeyMSGraphDomain
117+
If($null -ne $O365Object.Tenant.domains){
118+
#Set property
119+
$O365Object.Tenant.myDomain = $O365Object.Tenant.domains | Where-Object {$_.IsDefault -eq $true}
114120
}
115121
}
116-
#Set properties
117-
$tenantInfo.tenantName = $Tenant.displayName
118-
$tenantInfo.companyInfo = $Tenant
119-
$tenantInfo.tenantId = $Tenant.Id
120-
}
121-
#Get subscribed SKUs
122-
$SKus = Get-MonkeyMSGraphSuscribedSku
123-
If($SKus){
124-
#Set property
125-
$tenantInfo.sku = $SKus
126-
#Get licensing info from current tenant
127-
$licensingInfo = Get-TenantLicensingInfo -SKU $SKus
128-
#Set property
129-
$tenantInfo.licensing = $licensingInfo;
122+
Else{
123+
#Throw error
124+
throw ("[TenantError] {0}: {1}" -f "Unable to get tenant information",$_.Exception.Message)
125+
}
130126
}
131-
#Get Domains
132-
$tenantInfo.domains = Get-MonkeyMSGraphDomain
133-
If($null -ne $tenantInfo.domains){
134-
#Set property
135-
$tenantInfo.myDomain = $tenantInfo.domains | Where-Object {$_.IsDefault -eq $true}
127+
Else{
128+
$msg = @{
129+
MessageData = $message.O365TenantInfoError;
130+
callStack = (Get-PSCallStack | Select-Object -First 1);
131+
logLevel = 'warning';
132+
Tags = @('AADTenantError');
133+
}
134+
Write-Warning @msg
136135
}
137-
#return Obj
138-
return $tenantInfo
139136
}
140137
Catch{
141138
$msg = @{
142139
MessageData = $message.O365TenantInfoError;
143140
callStack = (Get-PSCallStack | Select-Object -First 1);
144141
logLevel = 'warning';
145-
Tags = @('AADTenantError');
142+
Tags = @('EIDTenantError');
146143
}
147144
Write-Warning @msg
148145
$msg = @{
149146
MessageData = $_;
150147
callStack = (Get-PSCallStack | Select-Object -First 1);
151-
logLevel = 'debug';
152-
Tags = @('AADTenantError');
148+
logLevel = 'verbose';
149+
Tags = @('EIDTenantError');
153150
}
154-
Write-Debug @msg
155-
}
156-
}
157-
Else{
158-
$msg = @{
159-
MessageData = $message.O365TenantInfoError;
160-
callStack = (Get-PSCallStack | Select-Object -First 1);
161-
logLevel = 'warning';
162-
Tags = @('AADTenantError');
151+
Write-Verbose @msg
152+
#Throw error
153+
throw ("[TenantError] {0}: {1}" -f "Unable to get tenant information",$_.Exception.Message)
163154
}
164-
Write-Warning @msg
165155
}
166-
}
167-
156+
}

0 commit comments

Comments
 (0)