@@ -35,7 +35,10 @@ Function Connect-MonkeyCloud{
3535 #>
3636
3737 [CmdletBinding ()]
38- Param ()
38+ Param (
39+ [parameter (Mandatory = $false , HelpMessage = " Used when tokens are imported from init param" )]
40+ [Switch ]$Connected
41+ )
3942 # Using MSAL authentication
4043 If ($null -ne $O365Object.msal_application_args ){
4144 # Connect to MSGraph
@@ -47,7 +50,9 @@ Function Connect-MonkeyCloud{
4750 Tags = @ (' TokenRequestInfoMessage' );
4851 }
4952 Write-Information @msg
50- $O365Object.auth_tokens.MSGraph = Connect-MonkeyMSGraph
53+ If (! $Connected.IsPresent ){
54+ $O365Object.auth_tokens.MSGraph = Connect-MonkeyMSGraph
55+ }
5156 If ($null -ne $O365Object.auth_tokens.MSGraph ){
5257 # Check If valid TenantId
5358 If ($null -ne $O365Object.TenantId ){
@@ -108,14 +113,16 @@ Function Connect-MonkeyCloud{
108113 }
109114 Write-Information @msg
110115 # Connect to Resource management
111- $p = @ {
112- Resource = $O365Object.Environment.ResourceManager ;
113- AzureService = " AzurePowershell" ;
114- InformationAction = $O365Object.InformationAction ;
115- Verbose = $O365Object.verbose ;
116- Debug = $O365Object.debug ;
116+ If (! $Connected.IsPresent ){
117+ $p = @ {
118+ Resource = $O365Object.Environment.ResourceManager ;
119+ AzureService = " AzurePowershell" ;
120+ InformationAction = $O365Object.InformationAction ;
121+ Verbose = $O365Object.verbose ;
122+ Debug = $O365Object.debug ;
123+ }
124+ $O365Object.auth_tokens.ResourceManager = Connect-MonkeyGenericApplication @p
117125 }
118- $O365Object.auth_tokens.ResourceManager = Connect-MonkeyGenericApplication @p
119126 }
120127 # Select tenant
121128 If ($null -eq $O365Object.TenantId -and $null -ne $O365Object.auth_tokens.ResourceManager ){
@@ -145,14 +152,16 @@ Function Connect-MonkeyCloud{
145152 }
146153 Write-Information @msg
147154 # Connect to Microsoft legacy Graph
148- $p = @ {
149- Resource = $O365Object.Environment.Graph ;
150- AzureService = " AzurePowershell" ;
151- InformationAction = $O365Object.InformationAction ;
152- Verbose = $O365Object.verbose ;
153- Debug = $O365Object.debug ;
155+ If (! $Connected.IsPresent ){
156+ $p = @ {
157+ Resource = $O365Object.Environment.Graph ;
158+ AzureService = " AzurePowershell" ;
159+ InformationAction = $O365Object.InformationAction ;
160+ Verbose = $O365Object.verbose ;
161+ Debug = $O365Object.debug ;
162+ }
163+ $O365Object.auth_tokens.Graph = Connect-MonkeyGenericApplication @p
154164 }
155- $O365Object.auth_tokens.Graph = Connect-MonkeyGenericApplication @p
156165 # Connect to Azure Portal
157166 If ($O365Object.isConfidentialApp -eq $false -and $O365Object.IncludeEntraID ){
158167 $msg = @ {
@@ -163,24 +172,32 @@ Function Connect-MonkeyCloud{
163172 Tags = @ (' TokenRequestInfoMessage' );
164173 }
165174 Write-Information @msg
166- $p = @ {
167- Resource = (Get-WellKnownAzureService - AzureService AzurePortal);
168- AzureService = " AzurePowershell" ;
169- InformationAction = $O365Object.InformationAction ;
170- Verbose = $O365Object.verbose ;
171- Debug = $O365Object.debug ;
175+ If (! $Connected.IsPresent ){
176+ $p = @ {
177+ Resource = (Get-WellKnownAzureService - AzureService AzurePortal);
178+ AzureService = " AzurePowershell" ;
179+ InformationAction = $O365Object.InformationAction ;
180+ Verbose = $O365Object.verbose ;
181+ Debug = $O365Object.debug ;
182+ }
183+ $O365Object.auth_tokens.AzurePortal = Connect-MonkeyGenericApplication @p
172184 }
173- $O365Object.auth_tokens.AzurePortal = Connect-MonkeyGenericApplication @p
174- # $O365Object.auth_tokens.AzurePortal = Connect-MonkeyAzurePortal
175185 }
176- # Get Tenant Information
177- Get-TenantInformation
178186 }
187+ $p = @ {
188+ InformationAction = $O365Object.InformationAction ;
189+ Verbose = $O365Object.verbose ;
190+ Debug = $O365Object.Debug ;
191+ }
192+ # Get information about current identity
193+ $O365Object.me = Get-MonkeyMe @p
194+ # Get Tenant Information
195+ Get-TenantInformation
179196 # Check If Azure services is selected
180197 If ($O365Object.initParams.Instance -eq " Azure" ){
181- Connect-MonkeyAzure
198+ Connect-MonkeyAzure @PSBoundParameters
182199 # Set Azure connections to True If connection and subscription are present
183- If ($null -ne $O365Object.auth_tokens.ResourceManager -and $null -ne $O365Object.auth_tokens.Graph -and $null -ne $O365Object .auth_tokens. MSGraph -and $null -ne $O365Object.subscriptions ){
200+ If ($null -ne $O365Object.auth_tokens.ResourceManager -and $null -ne $O365Object.auth_tokens.MSGraph -and $null -ne $O365Object.subscriptions ){
184201 $O365Object.onlineServices.Azure = $True
185202 }
186203 Else {
@@ -190,20 +207,13 @@ Function Connect-MonkeyCloud{
190207 }
191208 # Check If Microsoft 365 is selected
192209 ElseIf ($O365Object.initParams.Instance -eq " Microsoft365" ){
193- Connect-MonkeyM365
210+ Connect-MonkeyM365 @PSBoundParameters
194211 }
195212 # Get licensing information
196213 $O365Object.Licensing = Get-MonkeySKUInfo
197214 # Check If current identity can request users and groups from Microsoft Graph
198- $p = @ {
199- InformationAction = $O365Object.InformationAction ;
200- Verbose = $O365Object.verbose ;
201- Debug = $O365Object.Debug ;
202- }
203215 $O365Object.canRequestUsersFromMsGraph = Test-CanRequestUser @p
204216 $O365Object.canRequestGroupsFromMsGraph = Test-CanRequestGroup @p
205- # Get information about current identity
206- $O365Object.me = Get-MonkeyMe @p
207217 # Check If connected to Azure AD
208218 If ($O365Object.canRequestUsersFromMsGraph -eq $false -and $null -eq $O365Object.Tenant.CompanyInfo ){
209219 $msg = @ {
@@ -227,7 +237,7 @@ Function Connect-MonkeyCloud{
227237 Else {
228238 $authObject = $O365Object.auth_tokens.GetEnumerator () | Where-Object {$null -ne $_.Value } | Select-Object - ExpandProperty Value - First 1
229239 If ($null -ne $authObject ){
230- $O365Object.userId = $authObject | Get-UserIdFromToken
240+ $O365Object.userId = $O365Object .me.id ;
231241 }
232242 }
233243 # Get Azure AD permissions
@@ -298,15 +308,16 @@ Function Connect-MonkeyCloud{
298308 }
299309 Write-Information @msg
300310 # Connect to PIM
301- $p = @ {
302- Resource = (Get-WellKnownAzureService - AzureService MSPIM);
303- AzureService = " AzurePowershell" ;
304- InformationAction = $O365Object.InformationAction ;
305- Verbose = $O365Object.verbose ;
306- Debug = $O365Object.debug ;
311+ If (! $Connected.IsPresent ){
312+ $p = @ {
313+ Resource = (Get-WellKnownAzureService - AzureService MSPIM);
314+ AzureService = " AzurePowershell" ;
315+ InformationAction = $O365Object.InformationAction ;
316+ Verbose = $O365Object.verbose ;
317+ Debug = $O365Object.debug ;
318+ }
319+ $O365Object.auth_tokens.MSPIM = Connect-MonkeyGenericApplication @p
307320 }
308- $O365Object.auth_tokens.MSPIM = Connect-MonkeyGenericApplication @p
309- # $O365Object.auth_tokens.MSPIM = Connect-MonkeyPIM
310321 }
311322 # Get collectors
312323 $p = @ {
0 commit comments