question

hitendersingh-1558 avatar image
0 Votes"
hitendersingh-1558 asked hitendersingh-1558 commented

O365 license removal script

Hello,

I am trying to run the below script but it fails with the error mentioned below.


 Start-Transcript -Path "C:\Script\trans.txt"
    
 [array]$wpausers = @(Import-Csv -Path "C:\Script\userswpa.csv")
  
 foreach ($wapuser in $wpausers) {
     try {
         $msuser = Get-MsolUser -UserPrincipalName $wpauser.email -ErrorAction stop
     }
     catch {
         Write-Host "User lookup failed for $($wpauser.email)"
     }   
     if ($msuser) {
         try {
             Set-MsolUserLicense -UserPrincipalName $msuser.Userprincipalname -RemoveLicenses “tenant2:DYN365_ENTERPRISE_P1_IW” -ErrorAction stop
             write-host "successfully removed the license for user" $msuser.Userprincipalname
         }
         catch {
             Write-Host "Failed to remove license for $($msuser.Userprincipalname)"
         }   
     }
 }
    
 stop-transcript


Would appreciate any help




$error shows
Set-MsolUserLicense : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
At line:3 char:48
+ Set-MsolUserLicense -UserPrincipalName $wpauser.email -Remove ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-MsolUserLicense], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Online.Administration.Automatio
n.SetUserLicense



This works:-

[array]$rusers = @(Import-Csv -Path "C:\Script\userswpa.csv")

foreach ($user in $rusers) {
Set-MsolUserLicense -UserPrincipalName $user.email -RemoveLicenses “tenant2:DYN365_ENTERPRISE_P1_IW”
}

azure-active-directorywindows-server-powershellazure-ad-licensing
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

michev avatar image
0 Votes"
michev answered hitendersingh-1558 commented

Your catch statement needs to terminate processing, or skip to the next user. Add a 'continue' as follows:

      try {
          $msuser = Get-MsolUser -UserPrincipalName $wpauser.email -ErrorAction stop
      }
      catch {
          Write-Host "User lookup failed for $($wpauser.email)"
          continue
      }   


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the response @michev , Not sure what am I doing wrong.


This is the Output
![121602-image.png][1]




If I simply run [array]$rusers = @(Import-Csv -Path "C:\Script\userswpa.csv") foreach ($user in $rusers) { get-MsolUser -UserPrincipalName $user.email }
I am getting a response with correct list of users.

CSV contains a header "email" with list of users.



Also, if I run the below code. it is successful.

 [array]$rusers = @(Import-Csv -Path "C:\Script\userswpa.csv")
    
 foreach ($user in $rusers) {
     Set-MsolUserLicense -UserPrincipalName $user.email -RemoveLicenses “thecloudpro2:DYN365_ENTERPRISE_P1_IW”
 }
0 Votes 0 ·
image.png (60.4 KiB)

There seems to be some display issue in terminal output.

below is the code that shows "user lookup failed" output.

 [array]$wpausers = @(Import-Csv -Path "C:\Script\userswpa.csv")
    
 foreach ($wapuser in $wpausers) {
     try {
         $msuser = Get-MsolUser -UserPrincipalName $wpauser.email -ErrorAction stop
     }
     catch {
         Write-Host "User lookup failed for $($wpauser.email)"
         continue
     }   
     if ($msuser) {
         try {
             Set-MsolUserLicense -UserPrincipalName $msuser.Userprincipalname -RemoveLicenses “tenant2:DYN365_ENTERPRISE_P1_IW” -ErrorAction stop
             write-host "successfully removed the license for user" $msuser.Userprincipalname
         }
         catch {
             Write-Host "Failed to remove license for $($msuser.Userprincipalname)"
         }   
     }
 }


0 Votes 0 ·
michev avatar image michev hitendersingh-1558 ·

Oh, you have a small typo there, $wapuser -> $wpauser.

1 Vote 1 ·
Show more comments