Jul 16 2020 06:25 PM - edited Jul 16 2020 06:25 PM
Hey Guys relatively new to Powershell but creating a termination script. all working well and good apart from this section to remove 365 licences.
This is what i want to be able to do but doesn't work:
$upn=Get-ADUser -Identity 365.test1 | Select Userprincipalname
(get-MsolUser -ObjectID $upn).licenses.AccountSkuId |
foreach{
Set-MsolUserLicense -UserPrincipalName $upn -RemoveLicenses $_
}
This Does work but doesnt work in this script:
$upn = '365.test1@domain.com'
(get-MsolUser -UserPrincipalName $upn).licenses.AccountSkuId |
foreach{
Set-MsolUserLicense -UserPrincipalName $upn -RemoveLicenses $_
}
Seems to be something to do with my first line but i cannot figure it out.
Thankyou in Advance
Jul 17 2020 12:40 AM
Any particular error? It looks OK to me.
Jul 17 2020 12:55 AM - edited Jul 17 2020 12:56 AM
tried several variations.
When i type in the UPN manually like above it works fine but when trying to get it by the Get-AdUser command it doesn't like it. Recognises its there though.
get-MsolUser : User Not Found. User: @{UserPrincipalName=365.test1@domain.com.au}.
At line:2 char:2
+ (get-MsolUser -UserPrincipalName $upn).licenses.AccountSkuId |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Get-MsolUser], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.GetUser
Jul 17 2020 08:46 AM
SolutionOh sorry, I thought it was the other way around, my bad. So basically, you need to get just the UPN value, not the object. Either use
$upn.UserPrincipalName
or set the value like this:
$upn = Get-AdUser blabla | select -ExpandProperty UserPrincipalName
Jul 20 2020 07:05 PM