Guillaume vM M
Guillaume vM M

Reputation: 13

Remove Azure Licence for user from a csv file

I don't know why my script work just for the first line and then I have :

Get-MsolUser : Impossible de convertir «System.Object[]» en type «System.String», requis par le paramètre «UserPrincipalName». La 
méthode spécifiée n'est pas prise en charge.
Au caractère Ligne:5 : 34
+ (get-MsolUser -UserPrincipalName $csv2).licenses.AccountSkuId |
+                                  ~~~~~
    + CategoryInfo          : InvalidArgument : (:) [Get-MsolUser], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Online.Administration.Automation.GetUser

My Code :

$csv2 = Import-Csv C:\Tools\LicencesToRemove.csv
for ($i=0; $i -lt $csv2.Count; $i++)
{
(get-MsolUser -UserPrincipalName $csv2).licenses.AccountSkuId |foreach{
Set-MsolUserLicense -UserPrincipalName $csv2 -RemoveLicenses $_
}
}

csv file :

[email protected]
[email protected]
[email protected]
[email protected]

Upvotes: 0

Views: 279

Answers (2)

Clint Oliveira
Clint Oliveira

Reputation: 702

Assuming that your .csv2 file has a header with "UserPrincipalName". You can use the script below.

    $csv2 = Import-Csv C:\Tools\LicencesToRemove.csv
    foreach($user in $csv2)
    {
    (get-MsolUser -UserPrincipalName $user.UserPrincipalName).licenses.AccountSkuId |foreach{
    Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -RemoveLicenses $_
    }
    }

Upvotes: 0

user459872
user459872

Reputation: 25019

The issue is here.

get-MsolUser -UserPrincipalName $csv2

The UserPrincipalName must be of type string where as here it is Object[]. I suspect you are intended to write the following within the for loop

get-MsolUser -UserPrincipalName $csv2[$i].YourCsvColumnName

Upvotes: 1

Related Questions