@@ -15,6 +15,8 @@ function Invoke-AddUserBulk {
1515 $TenantFilter = $Request.body.TenantFilter
1616
1717 $BulkUsers = $Request.Body.BulkUser
18+ $AssignedLicenses = $Request.Body.licenses
19+ $UsageLocation = $Request.Body.usageLocation
1820
1921 if (! $BulkUsers ) {
2022 $Body = @ {
@@ -47,6 +49,7 @@ function Invoke-AddUserBulk {
4749 state = ' error'
4850 })
4951 } else {
52+ Write-Information " ## Creating user for $ ( $Name ) - $ ( $User.mailNickName ) @$ ( $User.domain ) "
5053 # Create user body with required properties
5154 $Password = if ($User.password ) { $User.password } else { New-passwordString }
5255 $UserBody = @ {
@@ -60,17 +63,26 @@ function Invoke-AddUserBulk {
6063 }
6164 }
6265
63- # Add optional properties if not null or empty
64- if (! [string ]::IsNullOrEmpty($User.usageLocation )) {
65- $UserBody.usageLocation = $User.usageLocation.value ?? $User.usageLocation
66+ # Usage location and licensing
67+ if ($UsageLocation ) {
68+ $UserBody.usageLocation = $UsageLocation.value ?? $UsageLocation
69+ Write-Information " - Usage location set to $ ( $UsageLocation.label ?? $UsageLocation ) "
70+ if ($AssignedLicenses ) {
71+ $GuidPattern = ' ([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})'
72+ $LicenseSkus = $AssignedLicenses.value ?? $AssignedLicenses | Where-Object { $_ -match $GuidPattern }
73+ if (($LicenseSkus | Measure-Object ).Count -gt 0 ) {
74+ Write-Information " - Assigned licenses set to $ ( ($AssignedLicenses.label ?? $AssignedLicenses ) -join ' , ' ) "
75+ $UserBody.assignedLicenses = @ ($LicenseSkus )
76+ }
77+ }
6678 }
6779
6880 # Convert businessPhones to array if not null or empty
6981 if (! [string ]::IsNullOrEmpty($User.businessPhones )) {
7082 $UserBody.businessPhones = @ ($User.businessPhones )
7183 }
7284
73- # loop through rest of post body and add to user body if not null or empty or not already set in the user body
85+ # Add all other properties
7486 foreach ($key in $User.PSObject.Properties.Name ) {
7587 if ($key -notin @ (' displayName' , ' mailNickName' , ' domain' , ' password' , ' usageLocation' , ' businessPhones' )) {
7688 if (! [string ]::IsNullOrEmpty($User .$key ) -and $UserBody .$key -eq $null ) {
@@ -107,11 +119,10 @@ function Invoke-AddUserBulk {
107119
108120 if ($BulkRequests.Count -gt 0 ) {
109121 Write-Warning " We have $ ( $BulkRequests.Count ) users to import"
110- Write-Information ($BulkRequests | ConvertTo-Json - Depth 5 )
122+ # Write-Information ($BulkRequests | ConvertTo-Json -Depth 5)
111123 $BulkResults = New-GraphBulkRequest - tenantid $TenantFilter - Requests $BulkRequests
112124 Write-Warning " We have $ ( $BulkResults.Count ) results"
113- Write-Information ($BulkResults | ConvertTo-Json - Depth 10
114- )
125+ # Write-Information ($BulkResults | ConvertTo-Json -Depth 10)
115126 foreach ($BulkResult in $BulkResults ) {
116127 if ($BulkResult.status -ne 201 ) {
117128 Write-LogMessage - headers $Request.Headers - API $APINAME - tenant $ ($TenantFilter ) - message " Failed to create user $ ( $BulkResult.id ) . Error:$ ( $BulkResult.body.error.message ) " - Sev ' Error'
@@ -126,7 +137,6 @@ function Invoke-AddUserBulk {
126137 state = ' success'
127138 copyField = $Message.copyField
128139 username = $BulkResult.body.userPrincipalName
129- id = $BulkResult.id
130140 })
131141 }
132142 }
0 commit comments