HI<\/p>\n
I am trying to add users with e3 license to a group call Microsoft E3 Licenses. but it fails. Here is the script. can some one help me. skulID is 1ict322:ems<\/p>\n
$msolUsers = Get-MsolUser -EnabledFilter EnabledOnly | Where-Object {($_.licenses).AccountSkuId -eq ‘1ICT322:EMS’}<\/p>\n
\n\nForEach ($user in $msolUsers) {
\ntry {
\n$ADUser = Get-ADUser -filter {UserPrincipalName -eq $user.UserPrincipalName} -ErrorAction stop<\/p>\nAdd-ADGroupMember -Identity Microsoft E3 Licenses -Members $ADUser -ErrorAction stop<\/p>\n
[PSCustomObject]@{
\nUserPrincipalName = $user.UserPrincipalName
\nMigrate = $true
\n}
\n}
\ncatch {
\n[PSCustomObject]@{
\nUserPrincipalName = $user.UserPrincipalName
\nMigrate = $false
\n}
\n}
\n}<\/p>\n<\/blockquote>\n<\/blockquote>\nUserPrincipalName Migrate<\/p>\n
\n[email protected] False<\/p>","upvoteCount":5,"answerCount":5,"datePublished":"2021-05-19T01:04:32.000Z","author":{"@type":"Person","name":"donarachchi2","url":"https://community.spiceworks.com/u/donarachchi2"},"acceptedAnswer":{"@type":"Answer","text":"
As Sean mentioned, make sure the groupname is right.<\/p>\n
‘get-aduser’ does not error out if you use a filter, so there is no need for try/catch<\/p>\n
try like so<\/p>\n
$msolUsers = \nGet-MsolUser -EnabledFilter EnabledOnly | \nWhere-Object { ($_.licenses).AccountSkuId -eq '1ICT322:EMS' }\n\nForEach ($user in $msolUsers) {\n $ADUser = Get-ADUser -filter { UserPrincipalName -eq $user.UserPrincipalName }\n try {\n Add-ADGroupMember -Identity \"Microsoft E3 Licenses\" -Members $ADUser -ErrorAction stop\n\n [PSCustomObject]@{\n UserPrincipalName = $user.UserPrincipalName\n Migrate = $true\n Status = 'Success'\n }\n }\n catch {\n [PSCustomObject]@{\n UserPrincipalName = $user.UserPrincipalName\n Migrate = $false\n Status = $error[0].exception.message\n }\n }\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-05-19T02:04:59.000Z","url":"https://community.spiceworks.com/t/powershell-add-users-to-a-group/800282/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"
HI<\/p>\n
I am trying to add users with e3 license to a group call Microsoft E3 Licenses. but it fails. Here is the script. can some one help me. skulID is 1ict322:ems<\/p>\n
$msolUsers = Get-MsolUser -EnabledFilter EnabledOnly | Where-Object {($_.licenses).AccountSkuId -eq ‘1ICT322:EMS’}<\/p>\n
\n\nForEach ($user in $msolUsers) {
\ntry {
\n$ADUser = Get-ADUser -filter {UserPrincipalName -eq $user.UserPrincipalName} -ErrorAction stop<\/p>\nAdd-ADGroupMember -Identity Microsoft E3 Licenses -Members $ADUser -ErrorAction stop<\/p>\n
[PSCustomObject]@{
\nUserPrincipalName = $user.UserPrincipalName
\nMigrate = $true
\n}
\n}
\ncatch {
\n[PSCustomObject]@{
\nUserPrincipalName = $user.UserPrincipalName
\nMigrate = $false
\n}
\n}
\n}<\/p>\n<\/blockquote>\n<\/blockquote>\nUserPrincipalName Migrate<\/p>\n
\n[email protected] False<\/p>","upvoteCount":5,"datePublished":"2021-05-19T01:04:32.000Z","url":"https://community.spiceworks.com/t/powershell-add-users-to-a-group/800282/1","author":{"@type":"Person","name":"donarachchi2","url":"https://community.spiceworks.com/u/donarachchi2"}},{"@type":"Answer","text":"
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n